From 13e432cc8a1cab9c9bbc1ff4ea9fba98a1e76ae1 Mon Sep 17 00:00:00 2001 From: Sebastien Monterisi Date: Fri, 18 Apr 2025 16:15:43 +0200 Subject: [PATCH] Rules to check migrations (POC) --- .../Rules/ForbidGlobalFunctionsRule.php | 69 +++++++++++++++++++ .../Rules/ForbidStaticDbFunctionsRule.php | 69 +++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 src/PHPStan/Rules/ForbidGlobalFunctionsRule.php create mode 100644 src/PHPStan/Rules/ForbidStaticDbFunctionsRule.php diff --git a/src/PHPStan/Rules/ForbidGlobalFunctionsRule.php b/src/PHPStan/Rules/ForbidGlobalFunctionsRule.php new file mode 100644 index 0000000..6168a44 --- /dev/null +++ b/src/PHPStan/Rules/ForbidGlobalFunctionsRule.php @@ -0,0 +1,69 @@ +. + * + * --------------------------------------------------------------------- + */ + +namespace GlpiProject\Tools\PHPStan\Rules; + +use PhpParser\Comment\Doc; +use PhpParser\Node; +use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Stmt; +use PhpParser\Node\Stmt\Global_; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleErrorBuilder; + +class ForbidGlobalFunctionsRule implements Rule +{ + private const FORBIDDEN_GLOBAL_FUNCTIONS = [ + 'getItemTypeForTable', + 'getForeignKeyFieldForItemType' + ]; + + public function getNodeType(): string + { + return Node\Expr\FuncCall::class; + } + + public function processNode(Node $node, Scope $scope): array + { + $errors = []; + + foreach (self::FORBIDDEN_GLOBAL_FUNCTIONS as $function) { + if ($node instanceof Node\Expr\FuncCall && $node->name->toString() === $function) { + $errors[] = RuleErrorBuilder::message(sprintf('L\'utilisation de la fonction globale %s() est interdite.', $function))->build(); + } + } + + return $errors; + } +} diff --git a/src/PHPStan/Rules/ForbidStaticDbFunctionsRule.php b/src/PHPStan/Rules/ForbidStaticDbFunctionsRule.php new file mode 100644 index 0000000..101e7f2 --- /dev/null +++ b/src/PHPStan/Rules/ForbidStaticDbFunctionsRule.php @@ -0,0 +1,69 @@ +. + * + * --------------------------------------------------------------------- + */ + +namespace GlpiProject\Tools\PHPStan\Rules; + +use PhpParser\Comment\Doc; +use PhpParser\Node; +use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Stmt; +use PhpParser\Node\Stmt\Global_; +use PHPStan\Analyser\Scope; +use PHPStan\Rules\Rule; +use PHPStan\Rules\RuleErrorBuilder; +use PHPStan\Type\FileTypeMapper; + +class ForbidStaticDbFunctionsRule implements Rule +{ + private const FORBIDDEN_STATIC_METHODS = [ + 'getTable', + 'getForeignKeyField', + ]; + + public function getNodeType(): string + { + return Node\Expr\StaticCall::class; + } + + public function processNode(Node $node, Scope $scope): array + { + $errors = []; + + foreach (self::FORBIDDEN_STATIC_METHODS as $method) { + if ($node instanceof Node\Expr\StaticCall && $node->name->toString() === $method) { + $errors[] = RuleErrorBuilder::message(sprintf('L\'utilisation de la méthode %s() est interdite.', $method))->build(); + } + } + return $errors; + } +}