Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of instanceof does not infer correct intersection type #7200

Closed
lupinitylabs opened this issue May 8, 2022 · 5 comments · Fixed by phpstan/phpstan-src#1740
Closed

Use of instanceof does not infer correct intersection type #7200

lupinitylabs opened this issue May 8, 2022 · 5 comments · Fixed by phpstan/phpstan-src#1740
Labels
Milestone

Comments

@lupinitylabs
Copy link

lupinitylabs commented May 8, 2022

Bug report

If I have a property of type class-string<Model&One&Two&Three> and try to assign a class string of a Model that has previously been checked if it as instance of One, Two and Three, phpstan reports the type for the Model to be class-string<Model>&class-string<One>&class-string<Three>&class-string<Two>.

Code snippet that reproduces the problem

https://phpstan.org/r/d90507d3-8e31-456b-85fe-56e9571aabc6

Expected output

I would expect the outcome to be class-string<Model&One&Two&Three> instead of class-string<Model>&class-string<One>&class-string<Three>&class-string<Two>.

I also had a variation of this reporting where the code is practically the same, but phpstan inferred the type class-string<Model>|class-string<One>|class-string<Three>|class-string<Two> instead. I cannot tell why the playground gives a different result - but both results appear wrong to me.

@oliverklee
Copy link
Sponsor

oliverklee commented May 8, 2022

Possibly related: phpstan/phpstan-phpunit#131

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone May 9, 2022
@phpstan-bot
Copy link

phpstan-bot commented Sep 21, 2022

@lupinitylabs After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
 PHP 8.0 – 8.1 (1 error)
 ==========
 
-11: Property HelloWorld::$class (class-string<Model&One&Three&Two>|null) does not accept class-string<Model>&class-string<One>&class-string<Three>&class-string<Two>.
+11: Property HelloWorld::$class (class-string<Model&One&Three&Two>|null) does not accept class-string<Model&One&Three&Two>|(class-string<Model>&class-string<One>&class-string<Three>&class-string<Two>).
 
 PHP 7.1 – 7.4 (3 errors)
 ==========
@@ @@
 
  8: Promoted properties are supported only on PHP 8.0 and later.
 11: Accessing ::class constant on an expression is supported only on PHP 8.0 and later.
-11: Property HelloWorld::$class (class-string<Model&One&Three&Two>|null) does not accept class-string<Model>&class-string<One>&class-string<Three>&class-string<Two>.
+11: Property HelloWorld::$class (class-string<Model&One&Three&Two>|null) does not accept class-string<Model&One&Three&Two>|(class-string<Model>&class-string<One>&class-string<Three>&class-string<Two>).
Full report

PHP 8.0 – 8.1 (1 error)

Line Error
11 `Property HelloWorld::$class (class-string<Model&One&Three&Two>

PHP 7.1 – 7.4 (3 errors)

Line Error
8 Promoted properties are supported only on PHP 8.0 and later.
11 Accessing ::class constant on an expression is supported only on PHP 8.0 and later.
11 `Property HelloWorld::$class (class-string<Model&One&Three&Two>

@phpstan-bot
Copy link

phpstan-bot commented Sep 22, 2022

@lupinitylabs After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.0 – 8.1 (1 error)
+PHP 8.0 – 8.1
 ==========
 
-11: Property HelloWorld::$class (class-string<Model&One&Three&Two>|null) does not accept class-string<Model>&class-string<One>&class-string<Three>&class-string<Two>.
+No errors
 
-PHP 7.1 – 7.4 (3 errors)
+PHP 7.1 – 7.4 (2 errors)
 ==========
 
  8: Promoted properties are supported only on PHP 8.0 and later.
-11: Accessing ::class constant on an expression is supported only on PHP 8.0 and later.
-11: Property HelloWorld::$class (class-string<Model&One&Three&Two>|null) does not accept class-string<Model>&class-string<One>&class-string<Three>&class-string<Two>.
+11: Accessing ::class constant on an expression is supported only on PHP 8.0 and later.
Full report

PHP 8.0 – 8.1

No errors

PHP 7.1 – 7.4 (2 errors)

Line Error
8 Promoted properties are supported only on PHP 8.0 and later.
11 Accessing ::class constant on an expression is supported only on PHP 8.0 and later.

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Sep 22, 2022

/cc @rvanvelzen Regression test please :)

@github-actions
Copy link

github-actions bot commented Oct 24, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants