|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\PHPStan; |
| 13 | + |
| 14 | +use PhpParser\Node; |
| 15 | +use PhpParser\Node\Stmt\Declare_; |
| 16 | +use PhpParser\Node\Stmt\DeclareDeclare; |
| 17 | +use PHPStan\Analyser\Scope; |
| 18 | +use PHPStan\Rules\Rule; |
| 19 | +use PHPStan\Rules\RuleErrorBuilder; |
| 20 | + |
| 21 | +/** |
| 22 | + * PHPStan rule that forbids usage of declare(strict_types=1) statements. |
| 23 | + * |
| 24 | + * This rule enforces that strict_types declaration should not be used. |
| 25 | + * |
| 26 | + * @author Oskar Stark <[email protected]> |
| 27 | + * |
| 28 | + * @implements Rule<Declare_> |
| 29 | + */ |
| 30 | +final class ForbidDeclareStrictTypesRule implements Rule |
| 31 | +{ |
| 32 | + public function getNodeType(): string |
| 33 | + { |
| 34 | + return Declare_::class; |
| 35 | + } |
| 36 | + |
| 37 | + public function processNode(Node $node, Scope $scope): array |
| 38 | + { |
| 39 | + if (!$node instanceof Declare_) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + $errors = []; |
| 44 | + |
| 45 | + foreach ($node->declares as $declare) { |
| 46 | + if ($declare instanceof DeclareDeclare) { |
| 47 | + $key = $declare->key->toString(); |
| 48 | + if ('strict_types' === $key) { |
| 49 | + $errors[] = RuleErrorBuilder::message( |
| 50 | + 'Usage of declare(strict_types=1) is forbidden. Remove the declare statement.' |
| 51 | + ) |
| 52 | + ->line($node->getLine()) |
| 53 | + ->identifier('symfonyAi.forbidDeclareStrictTypes') |
| 54 | + ->tip('Remove the declare(strict_types=1) statement from the file.') |
| 55 | + ->build(); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return $errors; |
| 61 | + } |
| 62 | +} |
0 commit comments