-
-
Notifications
You must be signed in to change notification settings - Fork 932
PHPStan reports "undefined property" errors when accessing properties that exist in some but not all types of a union. #13145
-
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::$appis typed as\App\Console\Application|\App\Web\Application- Both extend
\App\Base\Application \App\Web\Applicationhas asessionproperty\App\Console\Applicationdoes NOT have asessionproperty\App\Base\Applicationdoes NOT have asessionproperty
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?
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
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
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
What issue you had exactly and what stub helped you solve it?
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.