Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PHPStan reports "undefined property" errors when accessing properties that exist in some but not all types of a union. #13145

Discussion options

Problem: PHPStan reports "undefined property" errors when accessing properties that exist in some but not all types of a union, even when the property exists in the specific subclass being used.

Setup:

  • Locator::$app is typed as \App\Console\Application|\App\Web\Application
  • Both extend \App\Base\Application
  • \App\Web\Application has a session property
  • \App\Console\Application does NOT have a session property
  • \App\Base\Application does NOT have a session property

Code:

/** @var \App\Console\Application|\App\Web\Application */
public static $app;
$session = Locator::$app->session; 
// Error: Access to an undefined property App\Console\Application|App\Web\Application::$session.

Access to an undefined property App\Console\Application|App\Web\Application::$session.

What would be the best way to approach it?

You must be logged in to vote

So how the developer or even PHPStan is supposed to know that Locator::$app is App\Web\Application and when? I feel like this could be made much more obvious to both developers and static analysis just by narrowing (make more specific) some types somewhere.

Replies: 1 comment 9 replies

Comment options

You can narrow the type with instanceof, or use property_exists().

I would go with instanceof as you are narrowing a union. property_exists is more useful on mixed IMO

You must be logged in to vote
9 replies
Comment options

I'm not using PropertiesClassReflectionExtension for that what i'm asking is whether there's a similar way to handle static properties. I ended up solving the issue using stubs, but i'm wondering if there's a more appropriate or built in solution.

Comment options

What issue you had exactly and what stub helped you solve it?

Comment options

Basically, the issue is due to the union type so of course PHPStan can't know whether the application is running in a console or web context. As a result, it throws an error saying the property doesn't exist in the console application, even though it does in the web one. i solved it by using stubs to modify the behavior of the PHPDoc. This way, if you're in a console environment, you'll get all the properties and methods of console\Application, and if you're in a web environment, you'll get everything from web\Application.

https://phpstan.org/r/c27571a4-7bad-4c8b-9f29-62c5a2480519

Comment options

So how the developer or even PHPStan is supposed to know that Locator::$app is App\Web\Application and when? I feel like this could be made much more obvious to both developers and static analysis just by narrowing (make more specific) some types somewhere.

Answer selected by terabytesoftw
Comment options

yes, i precisely solved it, i can also configure PHPStan, with two configurations for the console, and on the other web, and it really works.

But in any case i would like to know if there is any way to work with static properties in a PHPStan extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Support
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /