phpstan-symfony
phpstan-symfony copied to clipboard
DIC compiler pass has() false positive
I have a simple compiler pass like below:
<?php
declare(strict_types=1);
namespace App\DependencyInjection\Compiler;
use Faker\Generator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
final class AddCustomFakerGeneratorProvidersPass implements CompilerPassInterface
{
public const TAG = 'app.faker.generator_provider';
public function process(ContainerBuilder $container): void
{
if (!$container->has(Generator::class)) {
return;
}
$svc = $container->findDefinition(Generator::class);
$taggedDeps = $container->findTaggedServiceIds(self::TAG);
foreach ($taggedDeps as $id => $tags) {
$svc->addMethodCall('addProvider', [new Reference($id)]);
}
}
}
PHPStan with Symfony extension reports that has()
will always return true:
------ -----------------------------------------------------------------------
Line DependencyInjection/Compiler/AddCustomFakerGeneratorProvidersPass.php
------ -----------------------------------------------------------------------
17 Negated boolean expression is always true.
21 Unreachable statement - code above always terminates.
------ -----------------------------------------------------------------------
This is taken straight from Symfony docs at https://symfony.com/doc/current/service_container/tags.html#create-a-compiler-pass and they even add a separate warning stating "// always first check if the primary service is defined". Thus, I think this is a bug in PHPStan Symfony extension.
Here's my full PHPStan config:
includes:
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
parameters:
level: 7
tmpDir: var/cache/_phpstan
parallel:
jobSize: 20
maximumNumberOfProcesses: 16
minimumNumberOfJobsPerProcess: 2
processTimeout: 30.0
symfony:
containerXmlPath: var/cache/dev/App_KernelDevDebugContainer.xml
excludePaths:
- %rootDir%/../../../src/DataFixtures/*
- %rootDir%/../../../src/Migrations/*
universalObjectCratesClasses:
- Faker\Generator
ignoreErrors:
- '#^Call to an undefined method Faker\\Generator::#'
tipsOfTheDay: false
polluteScopeWithLoopInitialAssignments: false
polluteScopeWithAlwaysIterableForeach: false
checkAlwaysTrueCheckTypeFunctionCall: true
checkAlwaysTrueInstanceof: true
checkAlwaysTrueStrictComparison: true
checkExplicitMixedMissingReturn: true
checkFunctionNameCase: true
reportMaybesInMethodSignatures: true
reportStaticMethodSignatures: true
checkTooWideReturnTypesInProtectedAndPublicMethods: true
treatPhpDocTypesAsCertain: false
checkMissingIterableValueType: false # handled by PHPCs with more granularity
Indeed, same problem for me.