Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

DIC compiler pass has() false positive

Đang mở
#313 2 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
php, symfony
Lĩnh vực
devtools

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
PHP
Star
806
Fork
104
Merge trung bình
27 phút
Pull request đã merge (30 ngày)
3

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của phpstan/phpstan-symfony

Tất cả issue của phpstan/phpstan-symfony

Issue tương tự

Thêm issue về PHP

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.