Skip to content

Commit 286b971

Browse files
authored
[symfony] Add NoFindTaggedServiceIdsCallRule (#174)
1 parent b4ff570 commit 286b971

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,18 @@ class SomeController
13671367

13681368
<br>
13691369

1370+
1371+
### NoFindTaggedServiceIdsCallRule
1372+
1373+
Instead of "$this->findTaggedServiceIds()" use more reliable registerForAutoconfiguration() and tagged iterator attribute. Those work outside any configuration and avoid missed tag errors
1374+
1375+
```yaml
1376+
rules:
1377+
- Symplify\PHPStanRules\Rules\Symfony\NoFindTaggedServiceIdsCallRule
1378+
```
1379+
1380+
<br>
1381+
13701382
### NoRequiredOutsideClassRule
13711383

13721384
Symfony #[Require]/@required should be used only in classes to avoid misuse

config/symfony-rules.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ rules:
1414
# dependency injection
1515
- Symplify\PHPStanRules\Rules\Symfony\NoGetDoctrineInControllerRule
1616
- Symplify\PHPStanRules\Rules\Symfony\NoGetInControllerRule
17+
- Symplify\PHPStanRules\Rules\Symfony\NoFindTaggedServiceIdsCallRule
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Rules\Symfony;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\MethodCall;
9+
use PhpParser\Node\Identifier;
10+
use PHPStan\Analyser\Scope;
11+
use PHPStan\Rules\IdentifierRuleError;
12+
use PHPStan\Rules\Rule;
13+
use PHPStan\Rules\RuleErrorBuilder;
14+
use Symplify\PHPStanRules\Enum\SymfonyRuleIdentifier;
15+
16+
/**
17+
* @implements Rule<MethodCall>
18+
*/
19+
final class NoFindTaggedServiceIdsCallRule implements Rule
20+
{
21+
/**
22+
* @var string
23+
*/
24+
public const ERROR_MESSAGE = 'Instead of "$this->findTaggedServiceIds()" use more reliable registerForAutoconfiguration() and tagged iterator attribute. Those work outside any configuration and avoid missed tag errors';
25+
26+
public function getNodeType(): string
27+
{
28+
return MethodCall::class;
29+
}
30+
31+
/**
32+
* @param MethodCall $node
33+
* @return IdentifierRuleError[]
34+
*/
35+
public function processNode(Node $node, Scope $scope): array
36+
{
37+
if (! $node->name instanceof Identifier) {
38+
return [];
39+
}
40+
41+
if ($node->name->toString() !== 'findTaggedServiceIds') {
42+
return [];
43+
}
44+
45+
$identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
46+
->identifier(SymfonyRuleIdentifier::class)
47+
->build();
48+
49+
return [
50+
$identifierRuleError,
51+
];
52+
}
53+
}

0 commit comments

Comments
 (0)