Yii2 advanced + PHPStan integration

Unfortunately there’s not a lot of documentation on getting Yii2 and PHPStan to work together.
I’ve integrated proget-hq/phpstan-yii2, I’ve followed the documentation to the best of my ability, I’ve tinkered and googled to no avail.
Running the analysis fills up the console with errors “Access to static property $app on an unknown class Yii.” and “Call to static method t() on an unknown class Yii.”, so symbol discovery is obviously not working.
In phpstan.neon I have the following:

includes:
    - vendor/proget-hq/phpstan-yii2/extension.neon
parameters:
    level: 1
    paths:
        - backend/controllers
        - backend/helpers
        - backend/models
        - backend/tests/unit
        - backend/widgets
        - common/models
        - common/helpers
        - common/interfaces
        - common/modules
        - common/rules
        - common/tests/unit
        - common/widgets
        - console/controllers
        - console/helpers
        - console/models
    yii2:
        config_path: %rootDir%/../../../common/config/boostrap.php

I’ve tried various combination for config_path.

Could somebody please fill me in on what the config file should look like?

You need to add Yii.php to autoloaded files so either in neon:

parameters:
    autoload_files:
        - vendor/yiisoft/yii2/Yii.php

Or in the run command:

phpstan analyze -a vendor/yiisoft/yii2/Yii.php your_dir_here
1 Like

Quick update to make this current.

autoload_files is no longer a valid approach to this. Instead, in your phpstan.neon file, do this:

parameters:
    bootstrapFiles:
        - vendor/yiisoft/yii2/Yii.php
4 Likes