Skip to content

Commit f2e46bd

Browse files
committed
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.
1 parent d58806c commit f2e46bd

36 files changed

+147
-85
lines changed

docs/custom-rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ The optional `errorTip` key can be used to show an additional message prefixed w
103103
### Error identifiers
104104

105105
The `errorIdentifier` key is optional. It can be used to provide a unique identifier to the PHPStan error.
106+
If not specified, a default identifier will be used, see the `\Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers` class for current list.
106107

107108
### Wildcards
108109

src/Calls/EchoCalls.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use PhpParser\Node\Stmt\Echo_;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Rules\Rule;
10-
use PHPStan\Rules\RuleError;
1110
use PHPStan\ShouldNotHappenException;
1211
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall;
1312
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
1413
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1515

1616
/**
1717
* Reports on dynamically calling echo().
@@ -51,14 +51,11 @@ public function getNodeType(): string
5151

5252

5353
/**
54-
* @param Echo_ $node
55-
* @param Scope $scope
56-
* @return list<RuleError>
5754
* @throws ShouldNotHappenException
5855
*/
5956
public function processNode(Node $node, Scope $scope): array
6057
{
61-
return $this->disallowedCallsRuleErrors->get(null, $scope, 'echo', 'echo', null, $this->disallowedCalls);
58+
return $this->disallowedCallsRuleErrors->get(null, $scope, 'echo', 'echo', null, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_ECHO);
6259
}
6360

6461
}

src/Calls/EmptyCalls.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use PhpParser\Node\Expr\Empty_;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Rules\Rule;
10-
use PHPStan\Rules\RuleError;
1110
use PHPStan\ShouldNotHappenException;
1211
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall;
1312
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
1413
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1515

1616
/**
1717
* Reports on dynamically calling empty().
@@ -51,14 +51,11 @@ public function getNodeType(): string
5151

5252

5353
/**
54-
* @param Empty_ $node
55-
* @param Scope $scope
56-
* @return list<RuleError>
5754
* @throws ShouldNotHappenException
5855
*/
5956
public function processNode(Node $node, Scope $scope): array
6057
{
61-
return $this->disallowedCallsRuleErrors->get(null, $scope, 'empty', 'empty', null, $this->disallowedCalls);
58+
return $this->disallowedCallsRuleErrors->get(null, $scope, 'empty', 'empty', null, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_EMPTY);
6259
}
6360

6461
}

src/Calls/EvalCalls.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use PhpParser\Node\Expr\Eval_;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Rules\Rule;
10-
use PHPStan\Rules\RuleError;
1110
use PHPStan\ShouldNotHappenException;
1211
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall;
1312
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
1413
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1515

1616
/**
1717
* Reports on dynamically calling eval().
@@ -51,14 +51,11 @@ public function getNodeType(): string
5151

5252

5353
/**
54-
* @param Eval_ $node
55-
* @param Scope $scope
56-
* @return list<RuleError>
5754
* @throws ShouldNotHappenException
5855
*/
5956
public function processNode(Node $node, Scope $scope): array
6057
{
61-
return $this->disallowedCallsRuleErrors->get(null, $scope, 'eval', 'eval', null, $this->disallowedCalls);
58+
return $this->disallowedCallsRuleErrors->get(null, $scope, 'eval', 'eval', null, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_EVAL);
6259
}
6360

6461
}

src/Calls/ExitDieCalls.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use PhpParser\Node\Expr\Exit_;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Rules\Rule;
10-
use PHPStan\Rules\RuleError;
1110
use PHPStan\ShouldNotHappenException;
1211
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall;
1312
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
1413
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1515

1616
/**
1717
* Reports on dynamically calling exit() & die().
@@ -51,15 +51,12 @@ public function getNodeType(): string
5151

5252

5353
/**
54-
* @param Exit_ $node
55-
* @param Scope $scope
56-
* @return list<RuleError>
5754
* @throws ShouldNotHappenException
5855
*/
5956
public function processNode(Node $node, Scope $scope): array
6057
{
6158
$kind = $node->getAttribute('kind', Exit_::KIND_DIE) === Exit_::KIND_EXIT ? 'exit' : 'die';
62-
return $this->disallowedCallsRuleErrors->get(null, $scope, $kind, $kind, null, $this->disallowedCalls);
59+
return $this->disallowedCallsRuleErrors->get(null, $scope, $kind, $kind, null, $this->disallowedCalls, $kind === 'exit' ? ErrorIdentifiers::DISALLOWED_EXIT : ErrorIdentifiers::DISALLOWED_DIE);
6360
}
6461

6562
}

src/Calls/FunctionCalls.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
use PhpParser\Node\Name;
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Reflection\ReflectionProvider;
11+
use PHPStan\Rules\IdentifierRuleError;
1112
use PHPStan\Rules\Rule;
12-
use PHPStan\Rules\RuleError;
1313
use PHPStan\ShouldNotHappenException;
1414
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall;
1515
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
1616
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;
17+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1718

1819
/**
1920
* Reports on dynamically calling a disallowed function.
@@ -60,7 +61,7 @@ public function getNodeType(): string
6061
/**
6162
* @param FuncCall $node
6263
* @param Scope $scope
63-
* @return list<RuleError>
64+
* @return list<IdentifierRuleError>
6465
* @throws ShouldNotHappenException
6566
*/
6667
public function processNode(Node $node, Scope $scope): array
@@ -83,7 +84,7 @@ public function processNode(Node $node, Scope $scope): array
8384
} else {
8485
$definedIn = null;
8586
}
86-
$message = $this->disallowedCallsRuleErrors->get($node, $scope, (string)$name, (string)($displayName ?? $node->name), $definedIn, $this->disallowedCalls);
87+
$message = $this->disallowedCallsRuleErrors->get($node, $scope, (string)$name, (string)($displayName ?? $node->name), $definedIn, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_FUNCTION);
8788
if ($message) {
8889
return $message;
8990
}

src/Calls/NewCalls.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall;
1515
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
1616
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;
17+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1718

1819
/**
1920
* Reports on creating objects (calling constructors).
@@ -92,7 +93,7 @@ public function processNode(Node $node, Scope $scope): array
9293
$name .= self::CONSTRUCT;
9394
$errors = array_merge(
9495
$errors,
95-
$this->disallowedCallsRuleErrors->get($node, $scope, $name, $type->getClassName() . self::CONSTRUCT, $definedIn, $this->disallowedCalls)
96+
$this->disallowedCallsRuleErrors->get($node, $scope, $name, $type->getClassName() . self::CONSTRUCT, $definedIn, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_NEW)
9697
);
9798
}
9899
}

src/Calls/PrintCalls.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use PhpParser\Node\Expr\Print_;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Rules\Rule;
10-
use PHPStan\Rules\RuleError;
1110
use PHPStan\ShouldNotHappenException;
1211
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall;
1312
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
1413
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1515

1616
/**
1717
* Reports on dynamically calling print().
@@ -51,14 +51,11 @@ public function getNodeType(): string
5151

5252

5353
/**
54-
* @param Print_ $node
55-
* @param Scope $scope
56-
* @return list<RuleError>
5754
* @throws ShouldNotHappenException
5855
*/
5956
public function processNode(Node $node, Scope $scope): array
6057
{
61-
return $this->disallowedCallsRuleErrors->get(null, $scope, 'print', 'print', null, $this->disallowedCalls);
58+
return $this->disallowedCallsRuleErrors->get(null, $scope, 'print', 'print', null, $this->disallowedCalls, ErrorIdentifiers::DISALLOWED_PRINT);
6259
}
6360

6461
}

src/Calls/ShellExecCalls.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use PhpParser\Node\Expr\ShellExec;
88
use PHPStan\Analyser\Scope;
99
use PHPStan\Rules\Rule;
10-
use PHPStan\Rules\RuleError;
1110
use PHPStan\ShouldNotHappenException;
1211
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall;
1312
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
1413
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1515

1616
/**
1717
* Reports on dynamically using the execution backtick operator (<code>`ls`</code>).
@@ -55,9 +55,6 @@ public function getNodeType(): string
5555

5656

5757
/**
58-
* @param ShellExec $node
59-
* @param Scope $scope
60-
* @return list<RuleError>
6158
* @throws ShouldNotHappenException
6259
*/
6360
public function processNode(Node $node, Scope $scope): array
@@ -69,6 +66,7 @@ public function processNode(Node $node, Scope $scope): array
6966
null,
7067
null,
7168
$this->disallowedCalls,
69+
ErrorIdentifiers::DISALLOWED_BACKTICK,
7270
'Using the backtick operator (`...`) is forbidden because shell_exec() is forbidden%2$s%3$s'
7371
);
7472
}

src/Calls/StaticCalls.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public function getNodeType(): string
6060
*/
6161
public function processNode(Node $node, Scope $scope): array
6262
{
63-
/** @var StaticCall $node */
6463
return $this->disallowedMethodRuleErrors->get($node->class, $node, $scope, $this->disallowedCalls);
6564
}
6665

src/ControlStructures/BreakControlStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\ShouldNotHappenException;
1212
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure;
1313
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1415

1516
/**
1617
* Reports on using the break control structure.
@@ -53,7 +54,7 @@ public function getNodeType(): string
5354
*/
5455
public function processNode(Node $node, Scope $scope): array
5556
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'break', $this->disallowedControlStructures);
57+
return $this->disallowedControlStructureRuleErrors->get($scope, 'break', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_BREAK);
5758
}
5859

5960
}

src/ControlStructures/ContinueControlStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\ShouldNotHappenException;
1212
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure;
1313
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1415

1516
/**
1617
* Reports on using the continue control structure.
@@ -53,7 +54,7 @@ public function getNodeType(): string
5354
*/
5455
public function processNode(Node $node, Scope $scope): array
5556
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'continue', $this->disallowedControlStructures);
57+
return $this->disallowedControlStructureRuleErrors->get($scope, 'continue', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_CONTINUE);
5758
}
5859

5960
}

src/ControlStructures/DeclareControlStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\ShouldNotHappenException;
1212
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure;
1313
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1415

1516
/**
1617
* Reports on using the declare control structure.
@@ -53,7 +54,7 @@ public function getNodeType(): string
5354
*/
5455
public function processNode(Node $node, Scope $scope): array
5556
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'declare', $this->disallowedControlStructures);
57+
return $this->disallowedControlStructureRuleErrors->get($scope, 'declare', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_DECLARE);
5758
}
5859

5960
}

src/ControlStructures/DoWhileControlStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\ShouldNotHappenException;
1212
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure;
1313
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1415

1516
/**
1617
* Reports on using the do-while loop.
@@ -53,7 +54,7 @@ public function getNodeType(): string
5354
*/
5455
public function processNode(Node $node, Scope $scope): array
5556
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'do-while', $this->disallowedControlStructures);
57+
return $this->disallowedControlStructureRuleErrors->get($scope, 'do-while', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_DO_WHILE);
5758
}
5859

5960
}

src/ControlStructures/ElseControlStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\ShouldNotHappenException;
1212
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure;
1313
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1415

1516
/**
1617
* Reports on using the else control structure.
@@ -53,7 +54,7 @@ public function getNodeType(): string
5354
*/
5455
public function processNode(Node $node, Scope $scope): array
5556
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'else', $this->disallowedControlStructures);
57+
return $this->disallowedControlStructureRuleErrors->get($scope, 'else', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_ELSE);
5758
}
5859

5960
}

src/ControlStructures/ElseIfControlStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\ShouldNotHappenException;
1212
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure;
1313
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1415

1516
/**
1617
* Reports on using the elseif control structure.
@@ -54,7 +55,7 @@ public function getNodeType(): string
5455
*/
5556
public function processNode(Node $node, Scope $scope): array
5657
{
57-
return $this->disallowedControlStructureRuleErrors->get($scope, 'elseif', $this->disallowedControlStructures);
58+
return $this->disallowedControlStructureRuleErrors->get($scope, 'elseif', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_ELSE_IF);
5859
}
5960

6061
}

src/ControlStructures/ForControlStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\ShouldNotHappenException;
1212
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure;
1313
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1415

1516
/**
1617
* Reports on using the for loop.
@@ -53,7 +54,7 @@ public function getNodeType(): string
5354
*/
5455
public function processNode(Node $node, Scope $scope): array
5556
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'for', $this->disallowedControlStructures);
57+
return $this->disallowedControlStructureRuleErrors->get($scope, 'for', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_FOR);
5758
}
5859

5960
}

src/ControlStructures/ForeachControlStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\ShouldNotHappenException;
1212
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure;
1313
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1415

1516
/**
1617
* Reports on using the foreach loop.
@@ -53,7 +54,7 @@ public function getNodeType(): string
5354
*/
5455
public function processNode(Node $node, Scope $scope): array
5556
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'foreach', $this->disallowedControlStructures);
57+
return $this->disallowedControlStructureRuleErrors->get($scope, 'foreach', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_FOREACH);
5758
}
5859

5960
}

src/ControlStructures/GotoControlStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\ShouldNotHappenException;
1212
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure;
1313
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors;
14+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\ErrorIdentifiers;
1415

1516
/**
1617
* Reports on using the goto control structure.
@@ -53,7 +54,7 @@ public function getNodeType(): string
5354
*/
5455
public function processNode(Node $node, Scope $scope): array
5556
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'goto', $this->disallowedControlStructures);
57+
return $this->disallowedControlStructureRuleErrors->get($scope, 'goto', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_GOTO);
5758
}
5859

5960
}

0 commit comments

Comments
 (0)