diff --git a/src/Rules/Symfony/NoFindTaggedServiceIdsCallRule.php b/src/Rules/Symfony/NoFindTaggedServiceIdsCallRule.php index abf9ddb3..16851495 100644 --- a/src/Rules/Symfony/NoFindTaggedServiceIdsCallRule.php +++ b/src/Rules/Symfony/NoFindTaggedServiceIdsCallRule.php @@ -12,6 +12,7 @@ use PHPStan\Rules\RuleErrorBuilder; use Symplify\PHPStanRules\Enum\RuleIdentifier\SymfonyRuleIdentifier; use Symplify\PHPStanRules\Helper\NamingHelper; +use Symplify\PHPStanRules\PHPUnit\TestClassDetector; /** * @implements Rule @@ -35,6 +36,11 @@ public function processNode(Node $node, Scope $scope): array return []; } + // tagged service ids are commonly used in tests to assert service registration + if (TestClassDetector::isTestClass($scope)) { + return []; + } + $identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE) ->identifier(SymfonyRuleIdentifier::NO_FIND_TAGGED_SERVICE_IDS_CALL) ->build(); diff --git a/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/Fixture/FindTaggedServiceIdsInPass.php b/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/Fixture/FindTaggedServiceIdsInPass.php new file mode 100644 index 00000000..ee33e1ff --- /dev/null +++ b/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/Fixture/FindTaggedServiceIdsInPass.php @@ -0,0 +1,16 @@ +findTaggedServiceIds('some_tag'); + } +} diff --git a/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/Fixture/SkipFindTaggedServiceIdsInContext.php b/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/Fixture/SkipFindTaggedServiceIdsInContext.php new file mode 100644 index 00000000..98527519 --- /dev/null +++ b/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/Fixture/SkipFindTaggedServiceIdsInContext.php @@ -0,0 +1,15 @@ +findTaggedServiceIds('some_tag'); + } +} diff --git a/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/NoFindTaggedServiceIdsCallRuleTest.php b/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/NoFindTaggedServiceIdsCallRuleTest.php new file mode 100644 index 00000000..f1a0b964 --- /dev/null +++ b/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/NoFindTaggedServiceIdsCallRuleTest.php @@ -0,0 +1,51 @@ + + */ +final class NoFindTaggedServiceIdsCallRuleTest extends RuleTestCase +{ + /** + * @param list $expectedErrorsWithLines + */ + #[DataProvider('provideData')] + public function testRule(string $filePath, array $expectedErrorsWithLines): void + { + $this->analyse([$filePath], $expectedErrorsWithLines); + } + + /** + * @return Iterator, mixed>> + */ + public static function provideData(): Iterator + { + yield [__DIR__ . '/Fixture/FindTaggedServiceIdsInPass.php', [[NoFindTaggedServiceIdsCallRule::ERROR_MESSAGE, 14]]]; + + yield [__DIR__ . '/Fixture/SkipFindTaggedServiceIdsInContext.php', []]; + } + + /** + * @return string[] + */ + #[Override] + public static function getAdditionalConfigFiles(): array + { + return [__DIR__ . '/config/configured_rule.neon']; + } + + protected function getRule(): Rule + { + return self::getContainer()->getByType(NoFindTaggedServiceIdsCallRule::class); + } +} diff --git a/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/config/configured_rule.neon b/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/config/configured_rule.neon new file mode 100644 index 00000000..1e73f0df --- /dev/null +++ b/tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/config/configured_rule.neon @@ -0,0 +1,5 @@ +includes: + - ../../../../config/included_services.neon + +rules: + - Symplify\PHPStanRules\Rules\Symfony\NoFindTaggedServiceIdsCallRule