From 0df23ef82641ae44589f346abc5d14472c46ba1b Mon Sep 17 00:00:00 2001 From: Simon Bigelmayr Date: Thu, 14 Nov 2024 15:08:23 +0100 Subject: [PATCH] feat: Add PHPStan-Rule `MLL\Utils\PHPStan\Rules\VariableNameIdToIDRule` --- CHANGELOG.md | 6 +++ src/PHPStan/Rules/VariableNameIdToIDRule.php | 41 ++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/PHPStan/Rules/VariableNameIdToIDRule.php diff --git a/CHANGELOG.md b/CHANGELOG.md index d9ed358..79dbdaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ See [GitHub releases](https://github.com/mll-lab/php-utils/releases). ## Unreleased +## v5.7.0 + +### Added + +- Add PHPStan-Rule `MLL\Utils\PHPStan\Rules\VariableNameIdToIDRule` + ## v5.6.0 ### Added diff --git a/src/PHPStan/Rules/VariableNameIdToIDRule.php b/src/PHPStan/Rules/VariableNameIdToIDRule.php new file mode 100644 index 0000000..dfa0d25 --- /dev/null +++ b/src/PHPStan/Rules/VariableNameIdToIDRule.php @@ -0,0 +1,41 @@ + + */ +class VariableNameIdToIDRule implements Rule +{ + private const WHITELIST = ['Identifier']; + + public function getNodeType(): string + { + return Node\Expr\Variable::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if ( + $node instanceof Node\Expr\Variable + && is_string($node->name) + && \Safe\preg_match('/Id/', $node->name) + && ! Str::contains($node->name, self::WHITELIST) + ) { + return [ + RuleErrorBuilder::message(sprintf( + 'Variable name "$%s" should use "ID" instead of "Id".', + $node->name, + ))->build(), + ]; + } + + return []; + } +}