-
Notifications
You must be signed in to change notification settings - Fork 547
Add support for internal classes that overload offset access #3725
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
Merged
ondrejmirtes
merged 7 commits into
phpstan:1.12.x
from
Girgias:easy-offset-access-fixes
Dec 17, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fcdfa63
Add support for internal classes that overload offset access
Girgias bff963c
Add tests
Girgias 21881f4
Split tests for 8.4 and below
Girgias 7c60acf
add version constraints
Girgias 44d39ff
use PHP_VERSION_ID instead
Girgias 0669763
Update tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRul…
ondrejmirtes f50ed86
Update tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRul…
ondrejmirtes 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
91 changes: 91 additions & 0 deletions
91
tests/PHPStan/Rules/Arrays/data/internal-classes-overload-offset-access-invalid-php84.php
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,91 @@ | ||||||
| <?php | ||||||
| /** | ||||||
| * All of these offset accesses are invalid | ||||||
| * ++ and -- are also disallowed in general are they operate "by ref" | ||||||
|
Contributor
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.
Suggested change
|
||||||
| */ | ||||||
| namespace InternalClassesOverloadOffsetAccessInvalid\Php84; | ||||||
|
|
||||||
| function test1(\DOMNamedNodeMap $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test2(\Dom\NamedNodeMap $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test3(\DOMNodeList $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test4(\Dom\NodeList $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test5(\Dom\HTMLCollection $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test6(\Dom\DtdNamedNodeMap $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test7(\PDORow $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test8(\ResourceBundle $v): void | ||||||
| { | ||||||
| if ($v[0]) { | ||||||
| var_dump($v[0]); | ||||||
| } | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| var_dump($v['name']); | ||||||
| var_dump($v[0]); | ||||||
| } | ||||||
52 changes: 52 additions & 0 deletions
52
tests/PHPStan/Rules/Arrays/data/internal-classes-overload-offset-access-invalid.php
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,52 @@ | ||||||
| <?php | ||||||
| /** | ||||||
| * All of these offset accesses are invalid | ||||||
| * ++ and -- are also disallowed in general are they operate "by ref" | ||||||
|
Contributor
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.
Suggested change
|
||||||
| */ | ||||||
| namespace InternalClassesOverloadOffsetAccessInvalid; | ||||||
|
|
||||||
| function test1(\DOMNamedNodeMap $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test3(\DOMNodeList $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test7(\PDORow $v): void | ||||||
| { | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| } | ||||||
|
|
||||||
| function test8(\ResourceBundle $v): void | ||||||
| { | ||||||
| if ($v[0]) { | ||||||
| var_dump($v[0]); | ||||||
| } | ||||||
| $v[] = 'append'; | ||||||
| $v[0] = 'update'; | ||||||
| $v[0] .= ' and again'; | ||||||
| $r1 = &$v[0]; | ||||||
| unset($v[0]); | ||||||
| var_dump($r1); | ||||||
| var_dump($v['name']); | ||||||
| var_dump($v[0]); | ||||||
| } | ||||||
|
|
||||||
74 changes: 74 additions & 0 deletions
74
tests/PHPStan/Rules/Arrays/data/internal-classes-overload-offset-access-php84.php
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,74 @@ | ||
| <?php | ||
|
|
||
| namespace InternalClassesOverloadOffsetAccess\Php84; | ||
|
|
||
| function test1(\DOMNamedNodeMap $v): void | ||
| { | ||
| if ($v['attribute_name']) { | ||
| var_dump($v['attribute_name']); | ||
| } | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test2(\Dom\NamedNodeMap $v): void | ||
| { | ||
| if ($v['attribute_name']) { | ||
| var_dump($v['attribute_name']); | ||
| } | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test3(\DOMNodeList $v): void | ||
| { | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test4(\Dom\NodeList $v): void | ||
| { | ||
| //var_dump($v['attribute_name']); | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test5(\Dom\HTMLCollection $v): void | ||
| { | ||
| if ($v['name']) { | ||
| var_dump($v['name']); | ||
| } | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test6(\Dom\DtdNamedNodeMap $v): void | ||
| { | ||
| if ($v['name']) { | ||
| var_dump($v['name']); | ||
| } | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test7(\PDORow $v): void | ||
| { | ||
| if ($v['name']) { | ||
| var_dump($v['name']); | ||
| } | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test8(\ResourceBundle $v): void | ||
| { | ||
| var_dump($v['name']); | ||
| var_dump($v[0]); | ||
| } |
37 changes: 37 additions & 0 deletions
37
tests/PHPStan/Rules/Arrays/data/internal-classes-overload-offset-access.php
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,37 @@ | ||
| <?php | ||
|
|
||
| namespace InternalClassesOverloadOffsetAccess; | ||
|
|
||
| function test1(\DOMNamedNodeMap $v): void | ||
| { | ||
| if ($v['attribute_name']) { | ||
| var_dump($v['attribute_name']); | ||
| } | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test3(\DOMNodeList $v): void | ||
| { | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test7(\PDORow $v): void | ||
| { | ||
| if ($v['name']) { | ||
| var_dump($v['name']); | ||
| } | ||
| if ($v[0]) { | ||
| var_dump($v[0]); | ||
| } | ||
| } | ||
|
|
||
| function test8(\ResourceBundle $v): void | ||
| { | ||
| var_dump($v['name']); | ||
| var_dump($v[0]); | ||
| } | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.