- Notifications
You must be signed in to change notification settings - Fork4
Magic number rules for phpstan/phpstan.
License
sidz/phpstan-rules
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Provides additional rules forphpstan/phpstan
.
Run
composer require --dev sidz/phpstan-rules
If you usePHPStan extension installer, you're all set. If not, you need to manually register all the rules in yourphpstan.neon
:
includes:- vendor/sidz/phpstan-rules/rules.neon
Each rule by default ignores the following numbers:0
and1
. This can be configured by adding the following parameter to yourphpstan.neon
:
parameters:sidzIgnoreMagicNumbers: [0,1,100]
Each rule by default detects numeric strings like'12'
in source code. This behavior could be disabled via parameter:
parameters:sidzIgnoreNumericStrings:true
If you need to ignore particular rule, for exampleNoMagicNumberInComparisonOperatorRule
, you can do so by using built-inignoreErrors
parameter:
parameters:ignoreErrors:- '#^Do not use magic number in comparison operations\. Move to constant with a suitable name\.$#'
If you need to ignore this rule only for particular file or folder, this also can be done by usingignoreErrors
parameter:
parameters:ignoreErrors:-message:'#^Do not use magic number in comparison operations\. Move to constant with a suitable name\.$#'path:src/SomeFolder/*
And finally, if you want to ignore all the rules from this package for particular files or folders, add this tophpstan.neon
:
parameters:ignoreErrors:-message:'#Do not (use|return|assign) magic number (.)#'paths:- src/DataFixtures/*- tests/*
This package provides the following rules for use withphpstan/phpstan
:
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberAsFunctionArgumentRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberAssignedToPropertyRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInArithmeticOperatorRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInBitwiseOperatorRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInComparisonOperatorRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInDefaultParameterRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInLogicalOperatorRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInMatchRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInReturnStatementRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInSwitchCaseRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInTernaryOperatorRule
Sid\PHPStan\Rules\MagicNumber\NoMagicNumberVariableAssignmentRule
This rule reports an error when magic number is used as function argument:
<?phpsome_function(10);
This rule reports an error when magic number is assigned to class property:
<?phpclass Test{private$prop1 =10;private$prop2 = -5.5;}
This rule reports an error when magic number is used in various arithmetic operators:
<?php$var1 +2;$var2 -.3;$var3 *2.2;$var4 /2;$var5 %1000;$var6 **2;2 +$var1;1.1 -$var2;2 *$var3;-2 /$var4;1000 %$var5;
This rule reports an error when magic number is used in various bitwise operators:
<?php$a &1;$b |2;$c ^3;$a <<4;$b >>5;1 &$a;2 |$b;3 ^$c;4 <<$a;5 >>$b;6 >>7;
This rule reports an error when magic number is used in comparison operator:
<?php$var1 ===1;$var2 !==2;$var3 !==3;$var4 ===4.4;$var5 !== -5;$var6 <6;$var7 <=7;$var8 >.8;$var9 >=9;$var10 <=>0.1;$var11 ===11;
This rule reports an error when magic number is used as default parameter:
<?phpclass Test{publicfunctiontestMethod($param =3):string {return'string'; }}
This rule reports an error when magic number is used as part of logical operation:
<?php$aand1;1and$a;$bor2;2or$b;$cxor3;3xor$c;
This rule reports an error when magic number is used in arms or conditions:
<?phpmatch (3) {1 =>'Hi',2,4 =>'There',default =>thrownewLogicException(),};
This rule reports an error when magic number is used inreturn
statement:
<?phpclass Test{publicfunctiongetNegativeValue():float {return -20.5; }}
This rule reports an error when magic number is used in condition or cases:
<?phpswitch (100) {case5:break;}
This rule reports an error when magic number is used in ternary operator:
<?php$a =$b ?2 :'string';$c =$b ?: -3.5;$d =$b ?'string' :6;
This rule reports an error when magic number is assigned to some variable:
<?php$var1 =4;$var2 = -2;functiontest_func($var4 =3):void{$var5 =0;}$var6 =.1;$var7 =3.5;$var8 = -2.3;
This package is licensed using the MIT License.
Please have a look atLICENSE.md
.
About
Magic number rules for phpstan/phpstan.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.