Skip to content

Commit

Permalink
Add default error identifiers, used if not specified
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spaze committed May 14, 2024
1 parent d58806c commit f2e46bd
Show file tree
Hide file tree
Showing 36 changed files with 147 additions and 85 deletions.
1 change: 1 addition & 0 deletions docs/custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 2 additions & 5 deletions src/Calls/EchoCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -51,14 +51,11 @@ public function getNodeType(): string


/**
* @param Echo_ $node
* @param Scope $scope
* @return list<RuleError>
* @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);
}

}
7 changes: 2 additions & 5 deletions src/Calls/EmptyCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -51,14 +51,11 @@ public function getNodeType(): string


/**
* @param Empty_ $node
* @param Scope $scope
* @return list<RuleError>
* @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);
}

}
7 changes: 2 additions & 5 deletions src/Calls/EvalCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -51,14 +51,11 @@ public function getNodeType(): string


/**
* @param Eval_ $node
* @param Scope $scope
* @return list<RuleError>
* @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);
}

}
7 changes: 2 additions & 5 deletions src/Calls/ExitDieCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -51,15 +51,12 @@ public function getNodeType(): string


/**
* @param Exit_ $node
* @param Scope $scope
* @return list<RuleError>
* @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);
}

}
7 changes: 4 additions & 3 deletions src/Calls/FunctionCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -60,7 +61,7 @@ public function getNodeType(): string
/**
* @param FuncCall $node
* @param Scope $scope
* @return list<RuleError>
* @return list<IdentifierRuleError>
* @throws ShouldNotHappenException
*/
public function processNode(Node $node, Scope $scope): array
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Calls/NewCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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)
);
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/Calls/PrintCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -51,14 +51,11 @@ public function getNodeType(): string


/**
* @param Print_ $node
* @param Scope $scope
* @return list<RuleError>
* @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);
}

}
6 changes: 2 additions & 4 deletions src/Calls/ShellExecCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<code>`ls`</code>).
Expand Down Expand Up @@ -55,9 +55,6 @@ public function getNodeType(): string


/**
* @param ShellExec $node
* @param Scope $scope
* @return list<RuleError>
* @throws ShouldNotHappenException
*/
public function processNode(Node $node, Scope $scope): array
Expand All @@ -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'
);
}
Expand Down
1 change: 0 additions & 1 deletion src/Calls/StaticCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion src/ControlStructures/BreakControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}
3 changes: 2 additions & 1 deletion src/ControlStructures/ContinueControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}
3 changes: 2 additions & 1 deletion src/ControlStructures/DeclareControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}
3 changes: 2 additions & 1 deletion src/ControlStructures/DoWhileControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}
3 changes: 2 additions & 1 deletion src/ControlStructures/ElseControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}
3 changes: 2 additions & 1 deletion src/ControlStructures/ElseIfControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}
3 changes: 2 additions & 1 deletion src/ControlStructures/ForControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}
3 changes: 2 additions & 1 deletion src/ControlStructures/ForeachControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}
3 changes: 2 additions & 1 deletion src/ControlStructures/GotoControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

}
Loading

0 comments on commit f2e46bd

Please sign in to comment.