Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignoreMagicNumbers configurable parameter #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
parameters:
sidz:
ignoreMagicNumbers: [0, 1]

parametersSchema:
sidz:
ignoreMagicNumbers: arrayOf(number())

rules:
- 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

services:
-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberAsFunctionArgumentRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberAssignedToPropertyRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInArithmeticOperatorRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInBitwiseOperatorRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInComparisonOperatorRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInDefaultParameterRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInLogicalOperatorRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInMatchRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInReturnStatementRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInSwitchCaseRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberInTernaryOperatorRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule

-
class: Sid\PHPStan\Rules\MagicNumber\NoMagicNumberVariableAssignmentRule
arguments:
ignoreMagicNumbers: %sidz.ignoreMagicNumbers%
tags:
- phpstan.rules.rule
25 changes: 24 additions & 1 deletion src/Rules/MagicNumber/AbstractMagicNumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,33 @@

abstract class AbstractMagicNumberRule implements Rule
{
/**
* @var list<numeric>
*/
private $ignoreMagicNumbers;

/**
* @param list<numeric> $ignoreMagicNumbers
*/
public function __construct(array $ignoreMagicNumbers = [])
{
$this->ignoreMagicNumbers = $ignoreMagicNumbers;
}

protected function isNumber(?Expr $expr): bool
{
return $expr instanceof LNumber
$isNumber = $expr instanceof LNumber
|| $expr instanceof DNumber
|| $expr instanceof Expr\UnaryMinus;

return $isNumber && !$this->ignoreNumber($expr);
}

/**
* @param LNumber|DNumber $scalar
*/
private function ignoreNumber(Expr $scalar): bool
{
return in_array($scalar->value, $this->ignoreMagicNumbers, true);
}
}