From f2e46bd7fddb6382e4889c557943750ee803e863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Tue, 14 May 2024 04:23:05 +0200 Subject: [PATCH] Add default error identifiers, used if not specified https://phpstan.org/blog/phpstan-1-11-errors-identifiers-phpstan-pro-reboot#error-identifiers Original support for optional identifiers added in #97, this adds a default one which you can still override as before. Previously, the identifier would be added, now it would override the default one. --- docs/custom-rules.md | 1 + src/Calls/EchoCalls.php | 7 +-- src/Calls/EmptyCalls.php | 7 +-- src/Calls/EvalCalls.php | 7 +-- src/Calls/ExitDieCalls.php | 7 +-- src/Calls/FunctionCalls.php | 7 +-- src/Calls/NewCalls.php | 3 +- src/Calls/PrintCalls.php | 7 +-- src/Calls/ShellExecCalls.php | 6 +-- src/Calls/StaticCalls.php | 1 - .../BreakControlStructure.php | 3 +- .../ContinueControlStructure.php | 3 +- .../DeclareControlStructure.php | 3 +- .../DoWhileControlStructure.php | 3 +- .../ElseControlStructure.php | 3 +- .../ElseIfControlStructure.php | 3 +- src/ControlStructures/ForControlStructure.php | 3 +- .../ForeachControlStructure.php | 3 +- .../GotoControlStructure.php | 3 +- src/ControlStructures/IfControlStructure.php | 3 +- .../MatchControlStructure.php | 3 +- .../RequireIncludeControlStructure.php | 9 +++- .../ReturnControlStructure.php | 3 +- .../SwitchControlStructure.php | 3 +- .../WhileControlStructure.php | 3 +- .../DisallowedAttributeRuleErrors.php | 8 ++-- src/RuleErrors/DisallowedCallsRuleErrors.php | 11 ++--- .../DisallowedConstantRuleErrors.php | 11 ++--- .../DisallowedControlStructureRuleErrors.php | 7 ++- src/RuleErrors/DisallowedMethodRuleErrors.php | 2 +- .../DisallowedNamespaceRuleErrors.php | 11 ++--- .../DisallowedVariableRuleErrors.php | 4 +- src/RuleErrors/ErrorIdentifiers.php | 46 +++++++++++++++++++ src/Usages/ClassConstantUsages.php | 3 +- src/Usages/ConstantUsages.php | 3 +- src/Usages/NamespaceUsages.php | 22 ++++++++- 36 files changed, 147 insertions(+), 85 deletions(-) create mode 100644 src/RuleErrors/ErrorIdentifiers.php diff --git a/docs/custom-rules.md b/docs/custom-rules.md index c563c3a..6735d6e 100644 --- a/docs/custom-rules.md +++ b/docs/custom-rules.md @@ -103,6 +103,7 @@ The optional `errorTip` key can be used to show an additional message prefixed w ### Error identifiers The `errorIdentifier` key is optional. It can be used to provide a unique identifier to the PHPStan error. +If not specified, a default identifier will be used, see the `\Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers` class for current list. ### Wildcards diff --git a/src/Calls/EchoCalls.php b/src/Calls/EchoCalls.php index a94caa0..92d70ab 100644 --- a/src/Calls/EchoCalls.php +++ b/src/Calls/EchoCalls.php @@ -7,11 +7,11 @@ use PhpParser\Node\Stmt\Echo_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on dynamically calling echo(). @@ -51,14 +51,11 @@ public function getNodeType(): string /** - * @param Echo_ $node - * @param Scope $scope - * @return list * @throws ShouldNotHappenException */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedCallsRuleErrors->get(null, $scope, 'echo', 'echo', null, $this->disallowedCalls); + return $this->disallowedCallsRuleErrors->get(null, $scope, 'echo', 'echo', null, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_ECHO); } } diff --git a/src/Calls/EmptyCalls.php b/src/Calls/EmptyCalls.php index af44bea..53c9d12 100644 --- a/src/Calls/EmptyCalls.php +++ b/src/Calls/EmptyCalls.php @@ -7,11 +7,11 @@ use PhpParser\Node\Expr\Empty_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on dynamically calling empty(). @@ -51,14 +51,11 @@ public function getNodeType(): string /** - * @param Empty_ $node - * @param Scope $scope - * @return list * @throws ShouldNotHappenException */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedCallsRuleErrors->get(null, $scope, 'empty', 'empty', null, $this->disallowedCalls); + return $this->disallowedCallsRuleErrors->get(null, $scope, 'empty', 'empty', null, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_EMPTY); } } diff --git a/src/Calls/EvalCalls.php b/src/Calls/EvalCalls.php index d630d5f..3841e8e 100644 --- a/src/Calls/EvalCalls.php +++ b/src/Calls/EvalCalls.php @@ -7,11 +7,11 @@ use PhpParser\Node\Expr\Eval_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on dynamically calling eval(). @@ -51,14 +51,11 @@ public function getNodeType(): string /** - * @param Eval_ $node - * @param Scope $scope - * @return list * @throws ShouldNotHappenException */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedCallsRuleErrors->get(null, $scope, 'eval', 'eval', null, $this->disallowedCalls); + return $this->disallowedCallsRuleErrors->get(null, $scope, 'eval', 'eval', null, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_EVAL); } } diff --git a/src/Calls/ExitDieCalls.php b/src/Calls/ExitDieCalls.php index 2afcfa9..b6dd090 100644 --- a/src/Calls/ExitDieCalls.php +++ b/src/Calls/ExitDieCalls.php @@ -7,11 +7,11 @@ use PhpParser\Node\Expr\Exit_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on dynamically calling exit() & die(). @@ -51,15 +51,12 @@ public function getNodeType(): string /** - * @param Exit_ $node - * @param Scope $scope - * @return list * @throws ShouldNotHappenException */ public function processNode(Node $node, Scope $scope): array { $kind = $node->getAttribute('kind', Exit_::KIND_DIE) === Exit_::KIND_EXIT ? 'exit' : 'die'; - return $this->disallowedCallsRuleErrors->get(null, $scope, $kind, $kind, null, $this->disallowedCalls); + return $this->disallowedCallsRuleErrors->get(null, $scope, $kind, $kind, null, $this->disallowedCalls, $kind === 'exit' ? ErrorIdentifiers::DISALLOWED_EXIT : ErrorIdentifiers::DISALLOWED_DIE); } } diff --git a/src/Calls/FunctionCalls.php b/src/Calls/FunctionCalls.php index b239047..db7cf7e 100644 --- a/src/Calls/FunctionCalls.php +++ b/src/Calls/FunctionCalls.php @@ -8,12 +8,13 @@ use PhpParser\Node\Name; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ReflectionProvider; +use PHPStan\Rules\IdentifierRuleError; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on dynamically calling a disallowed function. @@ -60,7 +61,7 @@ public function getNodeType(): string /** * @param FuncCall $node * @param Scope $scope - * @return list + * @return list * @throws ShouldNotHappenException */ public function processNode(Node $node, Scope $scope): array @@ -83,7 +84,7 @@ public function processNode(Node $node, Scope $scope): array } else { $definedIn = null; } - $message = $this->disallowedCallsRuleErrors->get($node, $scope, (string)$name, (string)($displayName ?? $node->name), $definedIn, $this->disallowedCalls); + $message = $this->disallowedCallsRuleErrors->get($node, $scope, (string)$name, (string)($displayName ?? $node->name), $definedIn, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_FUNCTION); if ($message) { return $message; } diff --git a/src/Calls/NewCalls.php b/src/Calls/NewCalls.php index 7596e66..31c2efc 100644 --- a/src/Calls/NewCalls.php +++ b/src/Calls/NewCalls.php @@ -14,6 +14,7 @@ use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on creating objects (calling constructors). @@ -92,7 +93,7 @@ public function processNode(Node $node, Scope $scope): array $name .= self::CONSTRUCT; $errors = array_merge( $errors, - $this->disallowedCallsRuleErrors->get($node, $scope, $name, $type->getClassName() . self::CONSTRUCT, $definedIn, $this->disallowedCalls) + $this->disallowedCallsRuleErrors->get($node, $scope, $name, $type->getClassName() . self::CONSTRUCT, $definedIn, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_NEW) ); } } diff --git a/src/Calls/PrintCalls.php b/src/Calls/PrintCalls.php index affdb0e..509fc43 100644 --- a/src/Calls/PrintCalls.php +++ b/src/Calls/PrintCalls.php @@ -7,11 +7,11 @@ use PhpParser\Node\Expr\Print_; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on dynamically calling print(). @@ -51,14 +51,11 @@ public function getNodeType(): string /** - * @param Print_ $node - * @param Scope $scope - * @return list * @throws ShouldNotHappenException */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedCallsRuleErrors->get(null, $scope, 'print', 'print', null, $this->disallowedCalls); + return $this->disallowedCallsRuleErrors->get(null, $scope, 'print', 'print', null, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_PRINT); } } diff --git a/src/Calls/ShellExecCalls.php b/src/Calls/ShellExecCalls.php index fa498bc..44e1217 100644 --- a/src/Calls/ShellExecCalls.php +++ b/src/Calls/ShellExecCalls.php @@ -7,11 +7,11 @@ use PhpParser\Node\Expr\ShellExec; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; -use PHPStan\Rules\RuleError; use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on dynamically using the execution backtick operator (`ls`). @@ -55,9 +55,6 @@ public function getNodeType(): string /** - * @param ShellExec $node - * @param Scope $scope - * @return list * @throws ShouldNotHappenException */ public function processNode(Node $node, Scope $scope): array @@ -69,6 +66,7 @@ public function processNode(Node $node, Scope $scope): array null, null, $this->disallowedCalls, + ErrorIdentifiers::DISALLOWED_BACKTICK, 'Using the backtick operator (`...`) is forbidden because shell_exec() is forbidden%2$s%3$s' ); } diff --git a/src/Calls/StaticCalls.php b/src/Calls/StaticCalls.php index 4f46764..b6c341b 100644 --- a/src/Calls/StaticCalls.php +++ b/src/Calls/StaticCalls.php @@ -60,7 +60,6 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - /** @var StaticCall $node */ return $this->disallowedMethodRuleErrors->get($node->class, $node, $scope, $this->disallowedCalls); } diff --git a/src/ControlStructures/BreakControlStructure.php b/src/ControlStructures/BreakControlStructure.php index a15b6f3..030ce1e 100644 --- a/src/ControlStructures/BreakControlStructure.php +++ b/src/ControlStructures/BreakControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the break control structure. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'break', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'break', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_BREAK); } } diff --git a/src/ControlStructures/ContinueControlStructure.php b/src/ControlStructures/ContinueControlStructure.php index 0b4f026..d91151a 100644 --- a/src/ControlStructures/ContinueControlStructure.php +++ b/src/ControlStructures/ContinueControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the continue control structure. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'continue', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'continue', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_CONTINUE); } } diff --git a/src/ControlStructures/DeclareControlStructure.php b/src/ControlStructures/DeclareControlStructure.php index 7772eea..775a551 100644 --- a/src/ControlStructures/DeclareControlStructure.php +++ b/src/ControlStructures/DeclareControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the declare control structure. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'declare', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'declare', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_DECLARE); } } diff --git a/src/ControlStructures/DoWhileControlStructure.php b/src/ControlStructures/DoWhileControlStructure.php index aa210a9..f92b06b 100644 --- a/src/ControlStructures/DoWhileControlStructure.php +++ b/src/ControlStructures/DoWhileControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the do-while loop. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'do-while', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'do-while', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_DO_WHILE); } } diff --git a/src/ControlStructures/ElseControlStructure.php b/src/ControlStructures/ElseControlStructure.php index a589975..1fb3750 100644 --- a/src/ControlStructures/ElseControlStructure.php +++ b/src/ControlStructures/ElseControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the else control structure. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'else', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'else', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_ELSE); } } diff --git a/src/ControlStructures/ElseIfControlStructure.php b/src/ControlStructures/ElseIfControlStructure.php index 57b3c92..f65bb2c 100644 --- a/src/ControlStructures/ElseIfControlStructure.php +++ b/src/ControlStructures/ElseIfControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the elseif control structure. @@ -54,7 +55,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'elseif', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'elseif', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_ELSE_IF); } } diff --git a/src/ControlStructures/ForControlStructure.php b/src/ControlStructures/ForControlStructure.php index 1c8f4af..361f739 100644 --- a/src/ControlStructures/ForControlStructure.php +++ b/src/ControlStructures/ForControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the for loop. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'for', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'for', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_FOR); } } diff --git a/src/ControlStructures/ForeachControlStructure.php b/src/ControlStructures/ForeachControlStructure.php index ee42d9b..dd31941 100644 --- a/src/ControlStructures/ForeachControlStructure.php +++ b/src/ControlStructures/ForeachControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the foreach loop. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'foreach', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'foreach', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_FOREACH); } } diff --git a/src/ControlStructures/GotoControlStructure.php b/src/ControlStructures/GotoControlStructure.php index c8f0a55..50886fc 100644 --- a/src/ControlStructures/GotoControlStructure.php +++ b/src/ControlStructures/GotoControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the goto control structure. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'goto', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'goto', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_GOTO); } } diff --git a/src/ControlStructures/IfControlStructure.php b/src/ControlStructures/IfControlStructure.php index a642df9..5a3dbfb 100644 --- a/src/ControlStructures/IfControlStructure.php +++ b/src/ControlStructures/IfControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the if control structure. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'if', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'if', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_IF); } } diff --git a/src/ControlStructures/MatchControlStructure.php b/src/ControlStructures/MatchControlStructure.php index 046ceed..03f916f 100644 --- a/src/ControlStructures/MatchControlStructure.php +++ b/src/ControlStructures/MatchControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the match control structure. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'match', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'match', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_MATCH); } } diff --git a/src/ControlStructures/RequireIncludeControlStructure.php b/src/ControlStructures/RequireIncludeControlStructure.php index 0cd8c68..ffa2edf 100644 --- a/src/ControlStructures/RequireIncludeControlStructure.php +++ b/src/ControlStructures/RequireIncludeControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the foreach loop. @@ -53,25 +54,29 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - $type = null; + $type = $identifier = null; switch ($node->type) { case Include_::TYPE_INCLUDE: $type = 'include'; + $identifier = ErrorIdentifiers::DISALLOWED_INCLUDE; break; case Include_::TYPE_REQUIRE: $type = 'require'; + $identifier = ErrorIdentifiers::DISALLOWED_REQUIRE; break; case Include_::TYPE_INCLUDE_ONCE: $type = 'include_once'; + $identifier = ErrorIdentifiers::DISALLOWED_INCLUDE_ONCE; break; case Include_::TYPE_REQUIRE_ONCE: $type = 'require_once'; + $identifier = ErrorIdentifiers::DISALLOWED_REQUIRE_ONCE; break; } if ($type === null) { return []; } - return $this->disallowedControlStructureRuleErrors->get($scope, $type, $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, $type, $this->disallowedControlStructures, $identifier); } } diff --git a/src/ControlStructures/ReturnControlStructure.php b/src/ControlStructures/ReturnControlStructure.php index 397a3d6..a599378 100644 --- a/src/ControlStructures/ReturnControlStructure.php +++ b/src/ControlStructures/ReturnControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the return control structure. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'return', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'return', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_RETURN); } } diff --git a/src/ControlStructures/SwitchControlStructure.php b/src/ControlStructures/SwitchControlStructure.php index ea3aa82..e1db270 100644 --- a/src/ControlStructures/SwitchControlStructure.php +++ b/src/ControlStructures/SwitchControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the switch control structure. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'switch', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'switch', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_SWITCH); } } diff --git a/src/ControlStructures/WhileControlStructure.php b/src/ControlStructures/WhileControlStructure.php index 8822677..7d6a4c7 100644 --- a/src/ControlStructures/WhileControlStructure.php +++ b/src/ControlStructures/WhileControlStructure.php @@ -11,6 +11,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on using the while loop. @@ -53,7 +54,7 @@ public function getNodeType(): string */ public function processNode(Node $node, Scope $scope): array { - return $this->disallowedControlStructureRuleErrors->get($scope, 'while', $this->disallowedControlStructures); + return $this->disallowedControlStructureRuleErrors->get($scope, 'while', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_WHILE); } } diff --git a/src/RuleErrors/DisallowedAttributeRuleErrors.php b/src/RuleErrors/DisallowedAttributeRuleErrors.php index 10b9b51..179080b 100644 --- a/src/RuleErrors/DisallowedAttributeRuleErrors.php +++ b/src/RuleErrors/DisallowedAttributeRuleErrors.php @@ -5,7 +5,7 @@ use PhpParser\Node\Attribute; use PHPStan\Analyser\Scope; -use PHPStan\Rules\RuleError; +use PHPStan\Rules\IdentifierRuleError; use PHPStan\Rules\RuleErrorBuilder; use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed; use Spaze\PHPStan\Rules\Disallowed\DisallowedAttribute; @@ -37,7 +37,7 @@ public function __construct(Allowed $allowed, Identifier $identifier, Formatter * @param Attribute $attribute * @param Scope $scope * @param list $disallowedAttributes - * @return list + * @return list */ public function get(Attribute $attribute, Scope $scope, array $disallowedAttributes): array { @@ -56,9 +56,7 @@ public function get(Attribute $attribute, Scope $scope, array $disallowedAttribu $this->formatter->formatDisallowedMessage($disallowedAttribute->getMessage()), $disallowedAttribute->getAttribute() !== $attributeName ? " [{$attributeName} matches {$disallowedAttribute->getAttribute()}]" : '' )); - if ($disallowedAttribute->getErrorIdentifier()) { - $errorBuilder->identifier($disallowedAttribute->getErrorIdentifier()); - } + $errorBuilder->identifier($disallowedAttribute->getErrorIdentifier() ?? ErrorIdentifiers::DISALLOWED_ATTRIBUTE); if ($disallowedAttribute->getErrorTip()) { $errorBuilder->tip($disallowedAttribute->getErrorTip()); } diff --git a/src/RuleErrors/DisallowedCallsRuleErrors.php b/src/RuleErrors/DisallowedCallsRuleErrors.php index 2a6ec44..3032a88 100644 --- a/src/RuleErrors/DisallowedCallsRuleErrors.php +++ b/src/RuleErrors/DisallowedCallsRuleErrors.php @@ -5,7 +5,7 @@ use PhpParser\Node\Expr\CallLike; use PHPStan\Analyser\Scope; -use PHPStan\Rules\RuleError; +use PHPStan\Rules\IdentifierRuleError; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed; @@ -46,11 +46,12 @@ public function __construct(Allowed $allowed, Identifier $identifier, FilePath $ * @param string|null $displayName * @param string|null $definedIn * @param list $disallowedCalls + * @param string $identifier * @param string|null $message - * @return list + * @return list * @throws ShouldNotHappenException */ - public function get(?CallLike $node, Scope $scope, string $name, ?string $displayName, ?string $definedIn, array $disallowedCalls, ?string $message = null): array + public function get(?CallLike $node, Scope $scope, string $name, ?string $displayName, ?string $definedIn, array $disallowedCalls, string $identifier, ?string $message = null): array { foreach ($disallowedCalls as $disallowedCall) { if ( @@ -64,9 +65,7 @@ public function get(?CallLike $node, Scope $scope, string $name, ?string $displa $this->formatter->formatDisallowedMessage($disallowedCall->getMessage()), $disallowedCall->getCall() !== $name ? " [{$name}() matches {$disallowedCall->getCall()}()]" : '' )); - if ($disallowedCall->getErrorIdentifier()) { - $errorBuilder->identifier($disallowedCall->getErrorIdentifier()); - } + $errorBuilder->identifier($disallowedCall->getErrorIdentifier() ?? $identifier); if ($disallowedCall->getErrorTip()) { $errorBuilder->tip($disallowedCall->getErrorTip()); } diff --git a/src/RuleErrors/DisallowedConstantRuleErrors.php b/src/RuleErrors/DisallowedConstantRuleErrors.php index 281d5a2..441d384 100644 --- a/src/RuleErrors/DisallowedConstantRuleErrors.php +++ b/src/RuleErrors/DisallowedConstantRuleErrors.php @@ -4,7 +4,7 @@ namespace Spaze\PHPStan\Rules\Disallowed\RuleErrors; use PHPStan\Analyser\Scope; -use PHPStan\Rules\RuleError; +use PHPStan\Rules\IdentifierRuleError; use PHPStan\Rules\RuleErrorBuilder; use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; @@ -34,10 +34,11 @@ public function __construct(AllowedPath $allowedPath, Formatter $formatter) * @param Scope $scope * @param string|null $displayName * @param list $disallowedConstants - * @return list + * @param string $identifier + * @return list * @throws ShouldNotHappenException */ - public function get(string $constant, Scope $scope, ?string $displayName, array $disallowedConstants): array + public function get(string $constant, Scope $scope, ?string $displayName, array $disallowedConstants, string $identifier): array { foreach ($disallowedConstants as $disallowedConstant) { if ($disallowedConstant->getConstant() === $constant && !$this->allowedPath->isAllowedPath($scope, $disallowedConstant)) { @@ -47,9 +48,7 @@ public function get(string $constant, Scope $scope, ?string $displayName, array $displayName && $displayName !== $disallowedConstant->getConstant() ? ' (as ' . $displayName . ')' : '', $this->formatter->formatDisallowedMessage($disallowedConstant->getMessage()) )); - if ($disallowedConstant->getErrorIdentifier()) { - $errorBuilder->identifier($disallowedConstant->getErrorIdentifier()); - } + $errorBuilder->identifier($disallowedConstant->getErrorIdentifier() ?? $identifier); if ($disallowedConstant->getErrorTip()) { $errorBuilder->tip($disallowedConstant->getErrorTip()); } diff --git a/src/RuleErrors/DisallowedControlStructureRuleErrors.php b/src/RuleErrors/DisallowedControlStructureRuleErrors.php index 113eeb6..da58e7b 100644 --- a/src/RuleErrors/DisallowedControlStructureRuleErrors.php +++ b/src/RuleErrors/DisallowedControlStructureRuleErrors.php @@ -32,10 +32,11 @@ public function __construct(AllowedPath $allowedPath, Formatter $formatter) * @param Scope $scope * @param string $controlStructure * @param list $disallowedControlStructures + * @param string $identifier * @return list * @throws ShouldNotHappenException */ - public function get(Scope $scope, string $controlStructure, array $disallowedControlStructures): array + public function get(Scope $scope, string $controlStructure, array $disallowedControlStructures, string $identifier): array { foreach ($disallowedControlStructures as $disallowedControlStructure) { if ( @@ -47,9 +48,7 @@ public function get(Scope $scope, string $controlStructure, array $disallowedCon $controlStructure, $this->formatter->formatDisallowedMessage($disallowedControlStructure->getMessage()) )); - if ($disallowedControlStructure->getErrorIdentifier()) { - $errorBuilder->identifier($disallowedControlStructure->getErrorIdentifier()); - } + $errorBuilder->identifier($disallowedControlStructure->getErrorIdentifier() ?? $identifier); if ($disallowedControlStructure->getErrorTip()) { $errorBuilder->tip($disallowedControlStructure->getErrorTip()); } diff --git a/src/RuleErrors/DisallowedMethodRuleErrors.php b/src/RuleErrors/DisallowedMethodRuleErrors.php index f7ee763..d151c3f 100644 --- a/src/RuleErrors/DisallowedMethodRuleErrors.php +++ b/src/RuleErrors/DisallowedMethodRuleErrors.php @@ -93,7 +93,7 @@ private function getRuleErrors(array $classes, MethodReflection $method, CallLik foreach ($classes as $class) { if ($class->hasMethod($method->getName())) { $declaredAs = $this->formatter->getFullyQualified($class->getDisplayName(false), $method); - $ruleErrors = $this->disallowedCallsRuleErrors->get($node, $scope, $declaredAs, $calledAs, $class->getFileName(), $disallowedCalls); + $ruleErrors = $this->disallowedCallsRuleErrors->get($node, $scope, $declaredAs, $calledAs, $class->getFileName(), $disallowedCalls, ErrorIdentifiers::DISALLOWED_METHOD); if ($ruleErrors) { return $ruleErrors; } diff --git a/src/RuleErrors/DisallowedNamespaceRuleErrors.php b/src/RuleErrors/DisallowedNamespaceRuleErrors.php index a756879..189b94a 100644 --- a/src/RuleErrors/DisallowedNamespaceRuleErrors.php +++ b/src/RuleErrors/DisallowedNamespaceRuleErrors.php @@ -4,7 +4,7 @@ namespace Spaze\PHPStan\Rules\Disallowed\RuleErrors; use PHPStan\Analyser\Scope; -use PHPStan\Rules\RuleError; +use PHPStan\Rules\IdentifierRuleError; use PHPStan\Rules\RuleErrorBuilder; use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedNamespace; @@ -37,9 +37,10 @@ public function __construct(AllowedPath $allowedPath, Identifier $identifier, Fo * @param string $description * @param Scope $scope * @param list $disallowedNamespaces - * @return list + * @param string $identifier + * @return list */ - public function getDisallowedMessage(string $namespace, string $description, Scope $scope, array $disallowedNamespaces): array + public function getDisallowedMessage(string $namespace, string $description, Scope $scope, array $disallowedNamespaces, string $identifier): array { foreach ($disallowedNamespaces as $disallowedNamespace) { if ($this->allowedPath->isAllowedPath($scope, $disallowedNamespace)) { @@ -57,9 +58,7 @@ public function getDisallowedMessage(string $namespace, string $description, Sco $this->formatter->formatDisallowedMessage($disallowedNamespace->getMessage()), $disallowedNamespace->getNamespace() !== $namespace ? " [{$namespace} matches {$disallowedNamespace->getNamespace()}]" : '' )); - if ($disallowedNamespace->getErrorIdentifier()) { - $errorBuilder->identifier($disallowedNamespace->getErrorIdentifier()); - } + $errorBuilder->identifier($disallowedNamespace->getErrorIdentifier() ?? $identifier); if ($disallowedNamespace->getErrorTip()) { $errorBuilder->tip($disallowedNamespace->getErrorTip()); } diff --git a/src/RuleErrors/DisallowedVariableRuleErrors.php b/src/RuleErrors/DisallowedVariableRuleErrors.php index 8754ac5..a69da88 100644 --- a/src/RuleErrors/DisallowedVariableRuleErrors.php +++ b/src/RuleErrors/DisallowedVariableRuleErrors.php @@ -44,9 +44,7 @@ public function get(string $variable, Scope $scope, array $disallowedVariables): $disallowedVariable->getVariable(), $this->formatter->formatDisallowedMessage($disallowedVariable->getMessage()) )); - if ($disallowedVariable->getErrorIdentifier()) { - $errorBuilder->identifier($disallowedVariable->getErrorIdentifier()); - } + $errorBuilder->identifier($disallowedVariable->getErrorIdentifier() ?? ErrorIdentifiers::DISALLOWED_VARIABLE); if ($disallowedVariable->getErrorTip()) { $errorBuilder->tip($disallowedVariable->getErrorTip()); } diff --git a/src/RuleErrors/ErrorIdentifiers.php b/src/RuleErrors/ErrorIdentifiers.php new file mode 100644 index 0000000..52a15e6 --- /dev/null +++ b/src/RuleErrors/ErrorIdentifiers.php @@ -0,0 +1,46 @@ +disallowedConstantRuleErrors->get($this->getFullyQualified($classNames, $constant), $scope, $displayName, $this->disallowedConstants); + return $this->disallowedConstantRuleErrors->get($this->getFullyQualified($classNames, $constant), $scope, $displayName, $this->disallowedConstants, ErrorIdentifiers::DISALLOWED_CLASS_CONSTANT); } diff --git a/src/Usages/ConstantUsages.php b/src/Usages/ConstantUsages.php index 69b4519..9e0c422 100644 --- a/src/Usages/ConstantUsages.php +++ b/src/Usages/ConstantUsages.php @@ -12,6 +12,7 @@ use Spaze\PHPStan\Rules\Disallowed\DisallowedConstant; use Spaze\PHPStan\Rules\Disallowed\DisallowedConstantFactory; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedConstantRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * Reports on constant usage. @@ -57,7 +58,7 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { /** @var ConstFetch $node */ - return $this->disallowedConstantRuleError->get((string)$node->name, $scope, null, $this->disallowedConstants); + return $this->disallowedConstantRuleError->get((string)$node->name, $scope, null, $this->disallowedConstants, ErrorIdentifiers::DISALLOWED_CONSTANT); } } diff --git a/src/Usages/NamespaceUsages.php b/src/Usages/NamespaceUsages.php index b3e1ee5..94b8d49 100644 --- a/src/Usages/NamespaceUsages.php +++ b/src/Usages/NamespaceUsages.php @@ -22,6 +22,7 @@ use Spaze\PHPStan\Rules\Disallowed\DisallowedNamespaceFactory; use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer; use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedNamespaceRuleErrors; +use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers; /** * @implements Rule @@ -72,12 +73,15 @@ public function processNode(Node $node, Scope $scope): array { if ($node instanceof FullyQualified) { $description = 'Class'; + $identifier = ErrorIdentifiers::DISALLOWED_CLASS; $namespaces = [$node->toString()]; } elseif ($node instanceof NullableType && $node->type instanceof FullyQualified) { $description = 'Class'; + $identifier = ErrorIdentifiers::DISALLOWED_CLASS; $namespaces = [$node->type->toString()]; } elseif ($node instanceof UnionType || $node instanceof IntersectionType) { $description = 'Class'; + $identifier = ErrorIdentifiers::DISALLOWED_CLASS; $namespaces = []; foreach ($node->types as $type) { if ($type instanceof FullyQualified) { @@ -90,7 +94,13 @@ public function processNode(Node $node, Scope $scope): array $namespaces = [$node->class->toString()]; } elseif ($node instanceof ClassConstFetch && $node->class instanceof Name) { $classReflection = $scope->resolveTypeByName($node->class)->getClassReflection(); - $description = $classReflection && $classReflection->isEnum() ? 'Enum' : 'Class'; + if ($classReflection && $classReflection->isEnum()) { + $description = 'Enum'; + $identifier = ErrorIdentifiers::DISALLOWED_ENUM; + } else { + $description = 'Class'; + $identifier = ErrorIdentifiers::DISALLOWED_CLASS; + } $namespaces = [$node->class->toString()]; } elseif ($node instanceof Class_ && ($node->extends !== null || count($node->implements) > 0)) { $namespaces = []; @@ -104,9 +114,11 @@ public function processNode(Node $node, Scope $scope): array } } elseif ($node instanceof New_ && $node->class instanceof Name) { $description = 'Class'; + $identifier = ErrorIdentifiers::DISALLOWED_CLASS; $namespaces = [$node->class->toString()]; } elseif ($node instanceof TraitUse) { $description = 'Trait'; + $identifier = ErrorIdentifiers::DISALLOWED_TRAIT; $namespaces = []; foreach ($node->traits as $trait) { $namespaces[] = $trait->toString(); @@ -119,7 +131,13 @@ public function processNode(Node $node, Scope $scope): array foreach ($namespaces as $namespace) { $errors = array_merge( $errors, - $this->disallowedNamespaceRuleErrors->getDisallowedMessage($this->normalizer->normalizeNamespace($namespace), $description ?? 'Namespace', $scope, $this->disallowedNamespace) + $this->disallowedNamespaceRuleErrors->getDisallowedMessage( + $this->normalizer->normalizeNamespace($namespace), + $description ?? 'Namespace', + $scope, + $this->disallowedNamespace, + $identifier ?? $identifier = ErrorIdentifiers::DISALLOWED_NAMESPACE + ) ); }