Phpstan Class_parent not found

1.1k Views Asked by At

I am trying to get static PHP code analysis to work on my OXID 6 eshop.

I am, however, hitting a brick wall when it comes to this:

When I run vendor/bin/phpstan analyse --configuration phpstan.neon source/modules/myvendor/mymodule I always get errors like Class mynamespace\Article_parent not found.

This is probably because in Oxid, the eshop classes are not directly overwritten, but instead declared like this

<?php
namespace mynamespace;

class Article extends Article_parent
{
...

and in a metadata.php the overwritten class is defined:

'extend' => [
        \OxidEsales\Eshop\Application\Model\Article::class => \mynamespace\Article::class,
    ],

Phpstan does not seem to be able to resolve this, I've tried many different configuration files, like this one

parameters:
    level: max
    inferPrivatePropertyTypeFromConstructor: true
    autoload_files:
        - vendor/oxid-esales/oxideshop-ce/source/oxfunctions.php
        - vendor/oxid-esales/oxideshop-ce/source/overridablefunctions.php

or this one

parameters:
    level: 7
    autoload_files:
    - vendor/oxid-esales/testing-library/base.php
    - vendor/oxid-esales/oxideshop-ce/source/oxfunctions.php
    - vendor/autoload.php

but to no avail. How can I get phpstan to work?

2

There are 2 best solutions below

1
On BEST ANSWER

The \mynamespace\Article_parent class does not exist and will be created at runtime as an alias. This is because it depends on your configuration / other modules installed where this alias points to.

My colleague Alfred Bez created the oxid-dump-autoload tool, what will dump out the module chain based on your current configuration and setup, so that PHPStan / Psalm can find the classes:

https://github.com/alfredbez/oxid-dump-autoload

Hope I could help

1
On

Please upgrade to the latest PHPStan version 0.12.33.

In version 0.12.26 a lot has been reworked that's described in this article. The main takeaway is that you should no longer get "class not found" for classes that clearly exist without any need for additional configuration.

Also, autoload_files has been deprecated. Please follow Discovering Symbols guide to see what to do instead (you can most likely just delete this section from your configuration).