Skip to content

PHPStan assues classes do not exist when conditionally defined classes are accessed inside a class_exists() block #5979

@driehle

Description

@driehle

Bug report

There can be a scenario, where a class may either exist or not exist, e.g. because of an optional dependency or because of supporting different versions of an upstream library. In such a scenario it is typical to check with class_exists if the corresponding class exists or not.

If the class does not exist, it seems like PHPStan inflects that such class cannot be a class-string. This leads to a type error.

Code snippet that reproduces the problem

Consider the following code, a snippet from DoctrineORMModule:

    /**
     * @return list<array{string, class-string}>
     */
    public function dataProviderForTestValidCommands(): array
    {
        $data = [
            // left out some commands here for simplicity ...
            // [...]
            [
                'migrations:execute',
                ExecuteCommand::class,
            ],
        ];

        // this is only available with DBAL 2.x
        if (class_exists(ImportCommand::class)) {
            $data[] = [
                'dbal:import',
                ImportCommand::class,
            ];
        }

        return $data;
    }

There should be no issue, as the result always holds the type list<array{string, class-string}>. However, PHPStan produces the following error on level 3:

 ------ -------------------------------------------------------------------------------------------------------------
  Line   tests/CliConfiguratorTest.php
 ------ -------------------------------------------------------------------------------------------------------------
  237    Method DoctrineORMModuleTest\Listener\CliConfiguratorTest::dataProviderForTestValidCommands() should return
         array<int, array{string, class-string}> but returns array{0: array{'dbal:run-sql',
         'Doctrine\\DBAL\\Tools\\Console\\Command\\RunSqlCommand'}, 1: array{'dbal:reserved-words',
         'Doctrine\\DBAL\\Tools\\Console\\Command\\ReservedWordsCommand'}, 2: array{'orm:clear-cache…',
         'Doctrine\\ORM\\Tools\\Console\\Command\\ClearCache\\QueryCommand'}, 3: array{'orm:clear-cache…',
         'Doctrine\\ORM\\Tools\\Console\\Command\\ClearCache\\ResultCommand'}, 4: array{'orm:generate-proxies',
         'Doctrine\\ORM\\Tools\\Console\\Command\\GenerateProxiesCommand'}, 5: array{'orm:ensure…',
         'Doctrine\\ORM\\Tools\\Console\\Command\\EnsureProductionSettingsCommand'}, 6: array{'orm:info',
         'Doctrine\\ORM\\Tools\\Console\\Command\\InfoCommand'}, 7: array{'orm:schema-tool…',
         'Doctrine\\ORM\\Tools\\Console\\Command\\SchemaTool\\CreateCommand'}, ...}.
 ------ -------------------------------------------------------------------------------------------------------------

Expected output

I would expect PHPStan not to raise any issues here.

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

Thank you for your great work with PHPStan! :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions