-
Notifications
You must be signed in to change notification settings - Fork 542
Avoid false inference with instanceof #3657
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug12107; | ||
|
||
use LogicException; | ||
use Throwable; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class HelloWorld | ||
{ | ||
public function sayHello(Throwable $e1, LogicException $e2): void | ||
{ | ||
if ($e1 instanceof $e2) { | ||
return; | ||
} | ||
|
||
assertType('Throwable', $e1); | ||
assertType('bool', $e1 instanceof $e2); // could be false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm unsure to say PHPStan that
But I'm not sure it's a big loss. |
||
} | ||
|
||
/** @param class-string<LogicException> $e2 */ | ||
public function sayHello2(Throwable $e1, string $e2): void | ||
{ | ||
if ($e1 instanceof $e2) { | ||
return; | ||
} | ||
|
||
|
||
assertType('Throwable', $e1); | ||
assertType('bool', $e1 instanceof $e2); // could be false | ||
} | ||
|
||
public function sayHello3(Throwable $e1): void | ||
{ | ||
if ($e1 instanceof LogicException) { | ||
return; | ||
} | ||
|
||
assertType('Throwable~LogicException', $e1); | ||
assertType('false', $e1 instanceof LogicException); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ public function doBar(Foo $foo, Bar $bar): void | |
if ($foo instanceof $class) { | ||
assertType(self::class, $foo); | ||
} else { | ||
assertType('InstanceOfClassString\Foo~InstanceOfClassString\Bar', $foo); | ||
assertType('InstanceOfClassString\Foo', $foo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I call |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,9 +80,9 @@ public function testExprInstanceof($subject, string $classString, $union, $inter | |
assertType('true', $subject instanceof Foo); | ||
assertType('bool', $subject instanceof $classString); | ||
} else { | ||
assertType('mixed~InstanceOfNamespace\Foo', $subject); | ||
assertType('false', $subject instanceof Foo); | ||
assertType('false', $subject instanceof $classString); | ||
assertType('mixed', $subject); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if I pass |
||
assertType('bool', $subject instanceof Foo); | ||
assertType('bool', $subject instanceof $classString); // could be false | ||
} | ||
|
||
$constantString = 'InstanceOfNamespace\BarParent'; | ||
|
@@ -132,23 +132,23 @@ public function testExprInstanceof($subject, string $classString, $union, $inter | |
assertType('ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject); | ||
assertType('bool', $subject instanceof $objectT); | ||
} else { | ||
assertType('mixed~ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject); | ||
assertType('mixed', $subject); | ||
assertType('bool', $subject instanceof $objectT); // can be false | ||
} | ||
|
||
if ($subject instanceof $objectTString) { | ||
assertType('ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject); | ||
assertType('bool', $subject instanceof $objectTString); | ||
} else { | ||
assertType('mixed~ObjectT of InstanceOfNamespace\BarInterface (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject); | ||
assertType('mixed', $subject); | ||
assertType('bool', $subject instanceof $objectTString); // can be false | ||
} | ||
|
||
if ($subject instanceof $mixedTString) { | ||
assertType('MixedT (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)&object', $subject); | ||
assertType('bool', $subject instanceof $mixedTString); | ||
} else { | ||
assertType('mixed~MixedT (method InstanceOfNamespace\Foo::testExprInstanceof(), argument)', $subject); | ||
assertType('mixed', $subject); | ||
assertType('bool', $subject instanceof $mixedTString); // can be false | ||
} | ||
|
||
|
@@ -180,8 +180,8 @@ public function testExprInstanceof($subject, string $classString, $union, $inter | |
assertType('InstanceOfNamespace\Foo', $object); | ||
assertType('bool', $object instanceof $classString); | ||
} else { | ||
assertType('object~InstanceOfNamespace\Foo', $object); | ||
assertType('false', $object instanceof $classString); | ||
assertType('object', $object); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same idea. |
||
assertType('bool', $object instanceof $classString); // could be false | ||
} | ||
|
||
if ($instance instanceof $string) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,11 +167,6 @@ public function testInstanceof(): void | |
388, | ||
$tipText, | ||
], | ||
[ | ||
'Instanceof between T of Exception and Error will always evaluate to false.', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code is
if I call it with |
||
404, | ||
$tipText, | ||
], | ||
[ | ||
'Instanceof between class-string<DateTimeInterface> and DateTimeInterface will always evaluate to false.', | ||
418, | ||
|
@@ -270,11 +265,6 @@ public function testInstanceofWithoutAlwaysTrue(): void | |
'Instanceof between mixed and ImpossibleInstanceOf\InvalidTypeTest|int results in an error.', | ||
362, | ||
],*/ | ||
[ | ||
'Instanceof between T of Exception and Error will always evaluate to false.', | ||
404, | ||
$tipText, | ||
], | ||
[ | ||
'Instanceof between class-string<DateTimeInterface> and DateTimeInterface will always evaluate to false.', | ||
418, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to copy the logic from MutatingScope::inferType with an uncertainty boolean:
phpstan-src/src/Analyser/MutatingScope.php
Lines 960 to 989 in adcabb3