Skip to content

Kernel::getAllowedEnvs method is considered unused in Symfony 8.1 (level max) even if used inside a trait #14773

@shulard

Description

@shulard

Bug report

Hello !

I've just installed a new Symfony project (Symfony 8.1) with phpstan (level max).

I've this code which was generated by the installer:

<?php

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    /**
     * @return list<string> An array of allowed values for APP_ENV
     */
    private function getAllowedEnvs(): array
    {
        return ['prod', 'dev', 'test'];
    }
}

The getAllowedEnvs() method is defined in the KernelTrait:

namespace Symfony\Component\DependencyInjection\Kernel;

…

trait KernelTrait
{
    …
    /**
     * Returns the allowed environment names.
     *
     * Override this to restrict the environments that are allowed to run.
     *
     * @return string[]
     */
    private function getAllowedEnvs(): array
    {
        return [];
    }
    …
}

Then the MicroKernelTrait which is used inside my Kernel is importing the getKernelParameters as doGetKernelParameters from the KernelTrait. The doGetKernelParameters will call getAllowedEnvs. Here is the microkernel definition:

trait MicroKernelTrait
{
    use KernelTrait {
        registerContainerConfiguration as private doRegisterContainerConfiguration;
        initializeBundles as protected doInitializeBundles;
        initializeContainer as protected doInitializeContainer;
        getKernelParameters as private doGetKernelParameters;
        getBundlesDefinition as private doGetBundlesDefinition;
    }

    …
}

I've this PHPStan configuration:

parameters:
    level: max
    paths:
        - bin/
        - config/
        - public/
        - src/
        - tests/

When I run it, I got the following:

 ------ ------------------------------------------------ 
  Line   src/Kernel.php                                  
 ------ ------------------------------------------------ 
  15     Method App\Kernel::getAllowedEnvs() is unused.  
         🪪  method.unused                               
 ------ ------------------------------------------------

However the getAllowedEnvs method is called within the MicroKernelTrait::getKernelParameters via the KernelTrait::doGetKernelParameters() call.

This is a tricky one, I tried to explain the context here. Not sure it's a real bug but I got this on a fresh install so it might be interesting to check.

Let me know if you need more details !

Code snippet that reproduces the problem

https://phpstan.org/r/3e736a6f-67da-40ed-853e-c6f660b0f84e

Expected output

I expect here that PHPStan will detect the method call. It looks like it works in the playground, but not on a fresh Symfony install…

Did PHPStan help you today? Did it make you happy in any way?

Yes ! The tool is awesome, I use it everytime I start a new project and it helped me finding issues and avoiding crashes a lot ❤️.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions