-
Notifications
You must be signed in to change notification settings - Fork 513
use conditional parameter for is_a() via stub #1306
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 all commits
6bbb2a6
b17aa8d
49610a6
49cb07b
6078953
93b4674
b3dd3f3
e569f1e
3460b04
d1155f3
34029c5
7c30478
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 |
---|---|---|
|
@@ -9,9 +9,12 @@ | |
use PHPStan\Analyser\TypeSpecifierAwareExtension; | ||
use PHPStan\Analyser\TypeSpecifierContext; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\ClassStringType; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
use PHPStan\Type\Generic\GenericClassStringType; | ||
use PHPStan\Type\MixedType; | ||
use function count; | ||
use function strtolower; | ||
|
||
|
@@ -42,6 +45,16 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n | |
$allowStringType = isset($node->getArgs()[2]) ? $scope->getType($node->getArgs()[2]->value) : new ConstantBooleanType(false); | ||
$allowString = !$allowStringType->equals(new ConstantBooleanType(false)); | ||
|
||
// early exit in some cases, so we don't create false positives via IsAFunctionTypeSpecifyingHelper | ||
if ($classType instanceof ClassStringType && !$classType instanceof GenericClassStringType) { | ||
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. especially this condition is pretty ugly .. would love feedback on how to improve it. at first I only had the |
||
if ($objectOrClassType->isString()->yes() && $allowString) { | ||
return new SpecifiedTypes([], []); | ||
} | ||
if (!$objectOrClassType->isString()->yes() && !$objectOrClassType instanceof MixedType) { | ||
return new SpecifiedTypes([], []); | ||
} | ||
} | ||
|
||
if (!$classType instanceof ConstantStringType && !$context->truthy()) { | ||
return new SpecifiedTypes([], []); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bug4371; | ||
|
||
use function PHPStan\Testing\assertType; | ||
use Exception; | ||
|
||
class HelloWorld | ||
{ | ||
/** | ||
* @param Exception $exception | ||
* @param class-string $classString | ||
*/ | ||
public function sayHello(Exception $exception, $classString): void | ||
{ | ||
assertType('bool', is_a($exception, $classString)); | ||
assertType('bool', is_a($exception, $classString, true)); | ||
assertType('bool', is_a($exception, $classString, false)); | ||
|
||
$exceptionClass = get_class($exception); | ||
assertType('false', is_a($exceptionClass, $classString)); | ||
assertType('bool', is_a($exceptionClass, $classString, true)); | ||
assertType('false', is_a($exceptionClass, $classString, 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. with 3e955af I dropped the conditional return type stub again, as I am no longer sure its worth to report about since we already have extensions for |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Bug4371; | ||
|
||
class Foo { | ||
} | ||
|
||
class Bar extends Foo { | ||
|
||
} | ||
|
||
class HalloWorld { | ||
public function doFoo() { | ||
if(is_a(Bar::class, Foo::class)) { // should be reported | ||
echo "This will never be true"; | ||
} else { | ||
echo "NO"; | ||
} | ||
} | ||
public function doBar() { | ||
if(is_a(Bar::class, Foo::class, false)) { // should be reported | ||
echo "This will never be true"; | ||
} else { | ||
echo "NO"; | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ public function doFoo( | |
|
||
} | ||
$className = 'Foo'; | ||
if (is_a($className, \Throwable::class, true)) { // should be fine | ||
if (is_a($className, \Throwable::class, true)) { // always 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. is no longer considered "fine" because 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. But also please test 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. hm I guess you mean I should test |
||
|
||
} | ||
if (is_array($callable)) { | ||
|
@@ -860,3 +860,21 @@ public function doFoo(int $i, array $is): void | |
} | ||
|
||
} | ||
|
||
class NonFinalSubclassMightImplementInterface { | ||
/** | ||
* @param class-string<Foo> $classString | ||
*/ | ||
public function doFoo($classString, Foo $obj): void { | ||
if (is_a($classString, \Throwable::class, true)) { // should be fine | ||
|
||
} | ||
|
||
if (is_a($obj, \Throwable::class, true)) { // should be fine | ||
|
||
} | ||
if (is_a($obj, \Throwable::class)) { // should be fine | ||
|
||
} | ||
} | ||
} |
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.
because I dropped this early-return, I had to compensate a few false positives in the
IsAFunctionTypeSpecifyingExtension
- added a few early returns.