1

I have a project (PHP 8.2.6, Yii2, PHPStan). After running phpstan command, I got a list of errors that should be fixed for successful checking. Example of errors:

  1. Access to an undefined property yii\base\Application::$request.
  2. Access to an undefined property yii\base\Application::$response.

This errors caused because of use such code: Yii::$app->request->post() or Yii::$app->response, etc.

The first solution is to add /** @phpstan-ignore-next-line */ everywhere in the code where, for example, Yii::$app->response-> will be used.

Question, is there any other proper way to get rid of such errors?

My PHPStan configuration:

parameters:
    level: 2
    bootstrapFiles:
        - vendor/yiisoft/yii2/Yii.php
    excludePaths:
        - 'runtime/*'
        - 'vendor/*'
        - 'tests/*'
    stubFiles:
        - stubs/ActiveRecord.stub
        - stubs/Yii.stub
        - stubs/YiiRbac.stub
        - stubs/YiiBase.stub
        - stubs/YiiWeb.stub
        - stubs/YiiConsole.stub
    universalObjectCratesClasses:
        - stdClass

1 Answer 1

2

When debugging problem like these, you should verify what type Yii::$app is. You can troubleshoot types:

\PHPStan\dumpType(Yii::$app);

Is it what you expect, the actual type yii\base\Application? If yes, what makes you believe that yii\base\Application::$request property exists? It's probably not declared in a native way on the class. Is it a magic property defined via __get? Is there a missing @property PHPDoc above the class?

See the helpful article on the topic: https://phpstan.org/blog/solving-phpstan-access-to-undefined-property

If you need to fix wrong 3rd party PHPDocs, you can write a stub file: https://phpstan.org/user-guide/stub-files

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.