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; + } +}