From 04280331ca61ebf96f3503f2e7c29e9efd976f76 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 23 Sep 2026 14:18:32 +0200 Subject: [PATCH] [symfony] skip NoFindTaggedServiceIdsCallRule in test classes --- .../NoFindTaggedServiceIdsCallRule.php | 6 +++ .../Fixture/FindTaggedServiceIdsInPass.php | 16 ++++++ .../SkipFindTaggedServiceIdsInContext.php | 15 ++++++ .../NoFindTaggedServiceIdsCallRuleTest.php | 51 +++++++++++++++++++ .../config/configured_rule.neon | 5 ++ 5 files changed, 93 insertions(+) create mode 100644 tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/Fixture/FindTaggedServiceIdsInPass.php create mode 100644 tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/Fixture/SkipFindTaggedServiceIdsInContext.php create mode 100644 tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/NoFindTaggedServiceIdsCallRuleTest.php create mode 100644 tests/Rules/Symfony/NoFindTaggedServiceIdsCallRule/config/configured_rule.neon diff --git a/src/Rules/Symfony/NoFindTaggedServiceIdsCallRule.php b/src/Rules/Symfony/NoFindTaggedServiceIdsCallRule.php index abf9ddb37..168514953 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 000000000..ee33e1ff8 --- /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 000000000..985275197 --- /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 000000000..f1a0b9647 --- /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 000000000..1e73f0dfd --- /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