-
Notifications
You must be signed in to change notification settings - Fork 599
Compare the element type of a variadic by-ref parameter against its out type #6227
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
staabm
merged 5 commits into
phpstan:2.3.x
from
SanderMuller:variadic-byref-out-element-type
Oct 1, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9cc8bbb
Compare the element type of a variadic by-ref parameter against its o…
SanderMuller 8fa6e1c
Drop the variadic branch from the level-filtering callback
SanderMuller c209234
Inline the variadic element type into the two parameter-out checks
SanderMuller a6cf832
Cover the non-array rebind of a variadic by-ref parameter with an ite…
SanderMuller 84adb60
Cover a maybe-array rebind of a variadic by-ref parameter
SanderMuller 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php declare(strict_types = 1); // lint >= 8.0 | ||
|
|
||
| namespace Bug15066; | ||
|
|
||
| function variadicByRef(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as &$ref) { | ||
| $ref = $ref === null ? null : trim($ref); | ||
| } | ||
| } | ||
|
|
||
| function singleByRef(string|null &$ref): void | ||
| { | ||
| $ref = $ref === null ? null : trim($ref); | ||
| } | ||
|
|
||
| function variadicByIndex(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as $key => $value) { | ||
| $refs[$key] = $value === null ? null : trim($value); | ||
| } | ||
| } | ||
|
|
||
| function variadicNeverNull(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as $key => $value) { | ||
| $refs[$key] = 'foo'; | ||
| } | ||
| } | ||
|
|
||
| function variadicNeverWritten(string|null &...$refs): void | ||
| { | ||
| } | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public function variadicByIndex(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as $key => $value) { | ||
| $refs[$key] = $value === null ? null : trim($value); | ||
| } | ||
| } | ||
|
|
||
| public function variadicNeverNull(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as $key => $value) { | ||
| $refs[$key] = 'foo'; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // Rebinding the packed variable discards the references it held, so PHP writes nothing back and | ||
| // there is no out value left to check. An array is still compared, because a write through an | ||
| // offset reaches the caller and leaves the variable as an array too. | ||
| function variadicRebindNonArray(string|null &...$refs): void | ||
| { | ||
| $refs = 42; | ||
| } | ||
|
|
||
| function variadicRebindOnlyString(string|null &...$refs): void | ||
| { | ||
| $refs = ['ok']; | ||
| } | ||
|
|
||
| // An iterable that is not an array still has an element type, but rebinding to it discards the | ||
| // references just the same, so it stays silent too. | ||
| function variadicRebindTraversable(string|null &...$refs): void | ||
| { | ||
| $refs = new \ArrayIterator(['ok']); | ||
| } | ||
|
|
||
| // Maybe an array is not compared either, even when the other types also have an element type. | ||
| function variadicRebindMaybeArray(string|null &...$refs): void | ||
| { | ||
| $refs = rand(0, 1) === 1 ? ['ok'] : new \ArrayIterator(['ok']); | ||
| } |
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,83 @@ | ||
| <?php declare(strict_types = 1); // lint >= 8.0 | ||
|
|
||
| namespace Bug15066Variables; | ||
|
|
||
| function variadicByRef(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as &$ref) { | ||
| $ref = $ref === null ? null : trim($ref); | ||
| } | ||
| } | ||
|
|
||
| function variadicByIndex(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as $key => $value) { | ||
| $refs[$key] = $value === null ? null : trim($value); | ||
| } | ||
| } | ||
|
|
||
| function variadicWrongType(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as $key => $value) { | ||
| $refs[$key] = 42; | ||
| } | ||
| } | ||
|
|
||
| /** @param-out string|null $refs */ | ||
| function variadicParamOut(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as $key => $value) { | ||
| $refs[$key] = $value === null ? null : trim($value); | ||
| } | ||
| } | ||
|
|
||
| /** @param-out string $refs */ | ||
| function variadicParamOutNeverWritten(string|null &...$refs): void | ||
| { | ||
| } | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public function variadicByIndex(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as $key => $value) { | ||
| $refs[$key] = $value === null ? null : trim($value); | ||
| } | ||
| } | ||
|
|
||
| public function variadicWrongType(string|null &...$refs): void | ||
| { | ||
| foreach ($refs as $key => $value) { | ||
| $refs[$key] = 42; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // Rebinding the packed variable discards the references it held, so PHP writes nothing back to any | ||
| // caller. Nothing is reported for a non-array, because no out value is left to check. An array is | ||
| // still reported: a write through an offset reaches the caller and leaves the variable as an array | ||
| // too, so the two cannot be told apart here, and the offset write is the case that matters. | ||
| function variadicRebindNonArray(string|null &...$refs): void | ||
| { | ||
| $refs = 42; | ||
| } | ||
|
|
||
| function variadicRebindWrongArray(string|null &...$refs): void | ||
| { | ||
| $refs = [42]; | ||
| } | ||
|
|
||
| function variadicRebindOkArray(string|null &...$refs): void | ||
| { | ||
| $refs = ['ok']; | ||
| } | ||
|
|
||
| // The packed variable may end up only maybe holding an array. There is then no element type to | ||
| // speak of, so the comparison is skipped rather than run against a nonexistent one. | ||
| function variadicRebindMaybeArray(string|null &...$refs): void | ||
| { | ||
| $refs = rand(0, 1) === 1 ? [42] : null; | ||
| } | ||
|
|
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.
mutating testing suggests this condition is not covered by a test
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.
Right, thanks. The existing case rebinds to
42, and an int has no element type, so the check reported nothing with or without the guard. I added a case that rebinds tonew \ArrayIterator(['ok']). Without the guard, it now fails with "never assigns null to &$refs". The same guard inParameterOutTypeCheckwas already covered: removing it failsParameterOutAssignedTypeRuleTest::testBug15066.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.
The mutation run on the new head still found one:
!isArray()->yes()toisArray()->no(), which only changes the maybe-array case. I addedrand(0, 1) === 1 ? ['ok'] : new \ArrayIterator(['ok']). With either mutant, the test now fails with "never assigns null to &$refs".