-
Notifications
You must be signed in to change notification settings - Fork 462
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
improve is_subclass_of type checks #1321
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
68c4a6b
reproduce bug 2755
clxmstaab a1c0883
reproduce bug 6697
clxmstaab 376161f
declare is_subclass_of conditional types in stub
clxmstaab 3f80c96
fix
clxmstaab 219bc3d
fix
clxmstaab 48d487f
cs
clxmstaab 20ff2f3
more tests
clxmstaab 99e6eed
fix php7
clxmstaab 7b6d2b8
fix test
clxmstaab 8ba138d
fix test name
clxmstaab b510f98
fix
clxmstaab 9316007
fix
clxmstaab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,15 @@ | |
|
||
/** | ||
* @param ($allow_string is false ? object : object|string) $object_or_class | ||
* @param class-string $class | ||
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. changed both, |
||
* @param string $class | ||
* @param bool $allow_string | ||
* @return ($allow_string is false ? ($object_or_class is object ? bool : false) : bool) | ||
*/ | ||
function is_subclass_of($object_or_class, string $class, $allow_string = true): bool{} | ||
|
||
/** | ||
* @param ($allow_string is false ? object : object|string) $object_or_class | ||
* @param string $class | ||
* @param bool $allow_string | ||
* @return ($allow_string is false ? ($object_or_class is object ? bool : false) : bool) | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug2755; | ||
|
||
/** | ||
* @param array<class-string<object>> $interfaces | ||
* @param array<class-string<object>> $classes | ||
*/ | ||
function foo(array $interfaces, array $classes): void | ||
{ | ||
foreach ($interfaces as $interface) { | ||
foreach ($classes as $class) { | ||
if (is_subclass_of($class, $interface)) { | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/PHPStan/Rules/Functions/data/is-subclass-allow-string.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace IsSubclassAllowString; | ||
|
||
class A {} | ||
|
||
class B extends A {} | ||
|
||
$b = new B(); | ||
|
||
if (is_subclass_of(A::class, A::class, false)) { // should error | ||
} | ||
|
||
if (is_subclass_of(B::class, B::class, false)) { // should error | ||
} | ||
|
||
if (is_subclass_of(B::class, A::class, false)) { // should error | ||
} | ||
|
||
if (is_subclass_of($b, A::class)) { // fine | ||
} | ||
|
||
if (is_subclass_of($b, B::class)) { // fine | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
obviously this is not a great fix, but with this PR we fix the existing/reported false-positives.
proper support for generic-class-string in
is_subclass_of
could be added later.. since there are not existing tests for this case, this should be fine for now