Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Rules/Symfony/NoFindTaggedServiceIdsCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<MethodCall>
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoFindTaggedServiceIdsCallRule\Fixture;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class FindTaggedServiceIdsInPass implements CompilerPassInterface
{
public function process(ContainerBuilder $containerBuilder): void
{
$taggedServiceIds = $containerBuilder->findTaggedServiceIds('some_tag');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoFindTaggedServiceIdsCallRule\Fixture;

use Symfony\Component\DependencyInjection\ContainerBuilder;

final class SkipFindTaggedServiceIdsInContext
{
public function test(ContainerBuilder $containerBuilder): void
{
$taggedServiceIds = $containerBuilder->findTaggedServiceIds('some_tag');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoFindTaggedServiceIdsCallRule;

use Iterator;
use Override;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symplify\PHPStanRules\Rules\Symfony\NoFindTaggedServiceIdsCallRule;

/**
* @extends RuleTestCase<NoFindTaggedServiceIdsCallRule>
*/
final class NoFindTaggedServiceIdsCallRuleTest extends RuleTestCase
{
/**
* @param list<array{0: string, 1: int}> $expectedErrorsWithLines
*/
#[DataProvider('provideData')]
public function testRule(string $filePath, array $expectedErrorsWithLines): void
{
$this->analyse([$filePath], $expectedErrorsWithLines);
}

/**
* @return Iterator<array<array<int, mixed>, 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
includes:
- ../../../../config/included_services.neon

rules:
- Symplify\PHPStanRules\Rules\Symfony\NoFindTaggedServiceIdsCallRule
Loading