2024-10-19 17:22:03 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\PHPStan;
|
|
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2025-09-24 13:34:19 +02:00
|
|
|
use PhpParser\Node\Name;
|
2024-10-19 17:22:03 -04:00
|
|
|
use PHPStan\Analyser\Scope;
|
|
|
|
|
use PHPStan\Rules\Rule;
|
2025-01-16 14:53:50 -05:00
|
|
|
use PHPStan\Rules\RuleErrorBuilder;
|
2024-10-19 17:22:03 -04:00
|
|
|
|
|
|
|
|
class ForbiddenGlobalFunctionsRule implements Rule
|
|
|
|
|
{
|
2025-03-03 14:41:19 -05:00
|
|
|
/** @var string[] */
|
|
|
|
|
public const FORBIDDEN_FUNCTIONS = ['app', 'resolve'];
|
2024-10-19 17:22:03 -04:00
|
|
|
|
|
|
|
|
public function getNodeType(): string
|
|
|
|
|
{
|
|
|
|
|
return FuncCall::class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function processNode(Node $node, Scope $scope): array
|
|
|
|
|
{
|
|
|
|
|
/** @var FuncCall $node */
|
2025-09-08 13:12:33 -04:00
|
|
|
if ($node->name instanceof Name) {
|
2024-10-19 17:22:03 -04:00
|
|
|
$functionName = (string) $node->name;
|
2025-03-03 14:41:19 -05:00
|
|
|
if (in_array($functionName, self::FORBIDDEN_FUNCTIONS, true)) {
|
2024-10-19 17:22:03 -04:00
|
|
|
return [
|
2025-01-16 14:53:50 -05:00
|
|
|
RuleErrorBuilder::message(sprintf(
|
|
|
|
|
'Usage of global function "%s" is forbidden.',
|
|
|
|
|
$functionName,
|
|
|
|
|
))->identifier('myCustomRules.forbiddenGlobalFunctions')->build(),
|
2024-10-19 17:22:03 -04:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|