Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
driehle opened this issue Nov 11, 2021 · 5 comments

Comments

@driehle
Copy link

driehle commented Nov 11, 2021

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! :-)

@mergeable
Copy link

mergeable bot commented Nov 11, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@driehle
Copy link
Author

driehle commented Nov 11, 2021

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@da34d3f

@driehle
Copy link
Author

driehle commented Nov 11, 2021

Wow, that was ridiculously fast! 🚀 Thanks!

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants