Skip to content

Commit 3645393

Browse files
committed
minor #569 Forbid declare strict types (OskarStark)
This PR was squashed before being merged into the main branch. Discussion ---------- Forbid declare strict types | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | -- | License | MIT Commits ------- fe9ce13 Forbid declare strict types
2 parents 00c52a8 + fe9ce13 commit 3645393

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

.phpstan/extension.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
rules:
2+
- Symfony\AI\PHPStan\ForbidDeclareStrictTypesRule
23
- Symfony\AI\PHPStan\ForbidNativeExceptionRule

0 commit comments

Comments
 (0)