-
Notifications
You must be signed in to change notification settings - Fork 575
Suppress property.nonObject and method.nonObject errors when property_exists()/method_exists() guard is present
#5729
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
Closed
phpstan-bot
wants to merge
12
commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-jmikw0u
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f407edc
Suppress `property.nonObject` and `method.nonObject` errors when `pro…
phpstan-bot 92fedf0
Add `// lint >= 8.0` comment to bug-14667 test files using `mixed` type
phpstan-bot 83e8923
Do not suppress property.nonObject error when type cannot access prop…
phpstan-bot 43d6936
Merge assign test into main bug-14667 test file and add method_exists…
phpstan-bot 701ce15
remove now dead code
staabm e9be37e
Add explicit canAccessProperties/canCallMethods maybe tests for class…
phpstan-bot 0f7c35e
Narrow class-string to class-string&hasMethod() in method_exists() guard
phpstan-bot 4657cdf
Merge method_exists rule test into nsrt bug-14667 test file
phpstan-bot 81cabd4
improve variable names
staabm 2a943b9
Add static property access and static method call tests for bug-14667
phpstan-bot 0cbc5fb
Narrow class-string to class-string&hasProperty/hasMethod in property…
phpstan-bot 5f7bcd9
mixed requires php 8.0
staabm 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| namespace Bug14667Nsrt; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @param mixed $row */ | ||
| function testMixed($row): void | ||
|
staabm marked this conversation as resolved.
|
||
| { | ||
| if (property_exists($row, 'prop')) { | ||
| assertType('(class-string&hasProperty(prop))|(object&hasProperty(prop))', $row); | ||
| } | ||
| } | ||
|
|
||
| function testExplicitMixed(mixed $row): void | ||
| { | ||
| if (property_exists($row, 'prop')) { | ||
| assertType('(class-string&hasProperty(prop))|(object&hasProperty(prop))', $row); | ||
| } | ||
| } | ||
|
|
||
| /** @param mixed $row */ | ||
| function testMethodExistsMixed($row): void | ||
| { | ||
| if (method_exists($row, 'foo')) { | ||
| assertType('(class-string&hasMethod(foo))|(object&hasMethod(foo))', $row); | ||
| $row->foo(); | ||
| } | ||
| } | ||
|
|
||
| function testMethodExistsExplicitMixed(mixed $row): void | ||
| { | ||
| if (method_exists($row, 'foo')) { | ||
| assertType('(class-string&hasMethod(foo))|(object&hasMethod(foo))', $row); | ||
| $row->foo(); | ||
| } | ||
| } | ||
|
|
||
| /** @param object|string $row */ | ||
| function testMethodExistsObjectOrString($row): void | ||
| { | ||
| if (method_exists($row, 'foo')) { | ||
| $row->foo(); | ||
| } | ||
| } | ||
|
|
||
| function testMethodExistsObject(object $row): void | ||
| { | ||
| if (method_exists($row, 'bar')) { | ||
| $row->bar(); | ||
| } | ||
| } | ||
|
|
||
| /** @param mixed $x */ | ||
| function testMethodExistsMixedChained($x): void | ||
| { | ||
| if (method_exists($x, 'getName') && $x->getName() !== null) { | ||
| echo $x->getName(); | ||
| } | ||
| } | ||
|
|
||
| /** @param class-string|object $row */ | ||
| function testMethodExistsClassStringOrObject($row): void | ||
| { | ||
| if (method_exists($row, 'foo')) { | ||
| $row->foo(); | ||
| } | ||
| } | ||
|
|
||
| /** @param class-string $row */ | ||
| function testMethodExistsClassString(string $row): void | ||
| { | ||
| if (method_exists($row, 'foo')) { | ||
| $row->foo(); // error: Cannot call method foo() on class-string. | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
add a test for this condition, when $type canAccessProperties is maybe
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.
Done. Added
testClassStringOrObjecttest cases to both the properties and methods bug-14667 test files. These use@param class-string|objectwhich directly producescanAccessProperties()/canCallMethods() = maybe(class-string returnsno, object returnsyes, union returnsmaybe), explicitly exercising the!$type->canAccessProperties()->no()condition. All 12140 tests pass andmake phpstanreports no errors.