Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

DIC compiler pass has() false positive

Abierto
#313 2 comentarios 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
php, symfony
Área
devtools

Línea de trabajo

Start with DependencyInjection/Compiler/AddCustomFakerGeneratorProvidersPass.php and reproduce the report using the shown PHPStan configuration and Symfony containerXmlPath. Inspect how the Symfony extension handles ContainerBuilder::has() against the generated container XML, then add a regression test showing that a valid guard does not produce unreachable-code warnings.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
PHP
Estrellas
805
Forks
104
Merge medio
27 min
PR fusionados (30 d)
3

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de phpstan/phpstan-symfony

Todos los issues de phpstan/phpstan-symfony

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.