-
Notifications
You must be signed in to change notification settings - Fork 552
Unsetting optional offset might still give a list. #5029
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
Open
VincentLanglet
wants to merge
10
commits into
phpstan:2.1.x
Choose a base branch
from
VincentLanglet:fix14177
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+306
−5
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b5379a5
Fix array_key_exists false positive on list types with optional trail…
ondrejmirtes 86e7f3f
WIP
VincentLanglet d7d28b9
Rework
VincentLanglet 89f5a54
Simplify
VincentLanglet 405eecf
Fix
VincentLanglet f1d04af
Handle non constant unset
VincentLanglet 77b49bf
More
VincentLanglet 1d76e96
Improve
VincentLanglet faf726d
Fix
VincentLanglet 2a30bf1
Feedback
VincentLanglet 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14177Nsrt; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @param list{0: string, 1: string, 2?: string, 3?: string} $b | ||
| */ | ||
| public function testList(array $b): void | ||
| { | ||
| if (array_key_exists(3, $b)) { | ||
| assertType('list{0: string, 1: string, 2?: string, 3: string}', $b); | ||
| } else { | ||
| assertType('list{0: string, 1: string, 2?: string}', $b); | ||
| } | ||
| assertType('list{0: string, 1: string, 2?: string, 3?: string}', $b); | ||
| } | ||
|
|
||
| public function placeholderToEditor(string $html): void | ||
| { | ||
| $result = preg_replace_callback( | ||
| '~\[image\\sid="(\\d+)"(?:\\shref="([^"]*)")?(?:\\sclass="([^"]*)")?\]~', | ||
| function (array $matches): string { | ||
| $id = (int) $matches[1]; | ||
|
|
||
| assertType('list{0: non-falsy-string, 1: numeric-string, 2?: string, 3?: string}', $matches); | ||
|
|
||
| $replacement = sprintf( | ||
| '<img src="%s"%s/>', | ||
| $id, | ||
| array_key_exists(3, $matches) ? sprintf(' class="%s"', $matches[3]) : '', | ||
| ); | ||
|
|
||
| assertType('list{0: non-falsy-string, 1: numeric-string, 2?: string, 3?: string}', $matches); | ||
|
|
||
| return array_key_exists(2, $matches) && $matches[2] !== '' | ||
| ? sprintf('<a href="%s">%s</a>', $matches[2], $replacement) | ||
| : $replacement; | ||
| }, | ||
| $html, | ||
| ); | ||
| } | ||
|
|
||
| public function placeholderToEditor2(string $html): void | ||
| { | ||
| $result = preg_replace_callback( | ||
| '~\[image\\sid="(\\d+)?"(?:\\shref="([^"]*)")?(?:\\sclass="([^"]*)")?\]~', | ||
| function (array $matches): string { | ||
| $id = (int) $matches[0]; | ||
|
|
||
| assertType('list{0: non-falsy-string, 1?: \'\'|numeric-string, 2?: string, 3?: string}', $matches); | ||
|
|
||
| $replacement = sprintf( | ||
| '<img src="%s"%s/>', | ||
| $id, | ||
| array_key_exists(2, $matches) ? sprintf(' class="%s"', $matches[2]) : '', | ||
| ); | ||
|
|
||
| assertType('list{0: non-falsy-string, 1?: \'\'|numeric-string, 2?: string, 3?: string}', $matches); | ||
|
|
||
| return array_key_exists(1, $matches) && $matches[1] !== '' | ||
| ? sprintf('<a href="%s">%s</a>', $matches[1], $replacement) | ||
| : $replacement; | ||
| }, | ||
| $html, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class HelloWorld2 | ||
| { | ||
| /** | ||
| * @param list{0: string, 1: string, 2?: string, 3?: string} $b | ||
| */ | ||
| public function testUnset0OnList(array $b): void | ||
| { | ||
| assertType('true', array_is_list($b)); | ||
| unset($b[0]); | ||
| assertType('false', array_is_list($b)); | ||
| $b[] = 'foo'; | ||
| assertType('false', array_is_list($b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param list{0: string, 1: string, 2?: string, 3?: string} $b | ||
| */ | ||
| public function testUnset1OnList(array $b): void | ||
| { | ||
| assertType('true', array_is_list($b)); | ||
| unset($b[1]); | ||
| assertType('false', array_is_list($b)); | ||
| $b[] = 'foo'; | ||
| assertType('false', array_is_list($b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param list{0: string, 1: string, 2?: string, 3?: string} $b | ||
| */ | ||
| public function testUnset2OnList(array $b): void | ||
| { | ||
| assertType('true', array_is_list($b)); | ||
| unset($b[2]); | ||
| assertType('bool', array_is_list($b)); | ||
| $b[] = 'foo'; | ||
| assertType('bool', array_is_list($b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param list{0: string, 1: string, 2?: string, 3?: string} $b | ||
| */ | ||
| public function testUnset3OnList(array $b): void | ||
| { | ||
| assertType('true', array_is_list($b)); | ||
| unset($b[3]); | ||
| assertType('bool', array_is_list($b)); | ||
| $b[] = 'foo'; | ||
| assertType('bool', array_is_list($b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{0: string, 1?: string, 2: string, 3?: string} $b | ||
| */ | ||
| public function testUnset0OnArray(array $b): void | ||
| { | ||
| assertType('bool', array_is_list($b)); | ||
| unset($b[0]); | ||
| assertType('false', array_is_list($b)); | ||
| $b[] = 'foo'; | ||
| assertType('false', array_is_list($b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{0: string, 1?: string, 2: string, 3?: string} $b | ||
| */ | ||
| public function testUnset1OnArray(array $b): void | ||
| { | ||
| assertType('bool', array_is_list($b)); | ||
| unset($b[1]); | ||
| assertType('false', array_is_list($b)); | ||
| $b[] = 'foo'; | ||
| assertType('false', array_is_list($b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{0: string, 1?: string, 2: string, 3?: string} $b | ||
| */ | ||
| public function testUnset2OnArray(array $b): void | ||
| { | ||
| assertType('bool', array_is_list($b)); | ||
| unset($b[2]); | ||
| assertType('false', array_is_list($b)); | ||
| $b[] = 'foo'; | ||
| assertType('false', array_is_list($b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{0: string, 1?: string, 2: string, 3?: string} $b | ||
| */ | ||
| public function testUnset3OnArray(array $b): void | ||
| { | ||
| assertType('bool', array_is_list($b)); | ||
| unset($b[3]); | ||
| assertType('bool', array_is_list($b)); | ||
| $b[] = 'foo'; | ||
| assertType('bool', array_is_list($b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param list{0: string, 1: string, 2?: string, 3?: string} $a | ||
| * @param list{0: string, 1?: string, 2?: string, 3?: string} $b | ||
| * @param list{0: string, 1?: string, 2?: string, 3: string} $c | ||
| * @param 1|2 $int | ||
| */ | ||
| public function testUnsetNonConstant(array $a, array $b, array $c, int $int): void | ||
| { | ||
| assertType('true', array_is_list($a)); | ||
| assertType('true', array_is_list($b)); | ||
| assertType('true', array_is_list($c)); | ||
| unset($a[$int]); | ||
| unset($b[$int]); | ||
| unset($c[$int]); | ||
| assertType('false', array_is_list($a)); | ||
| assertType('bool', array_is_list($b)); | ||
| assertType('bool', array_is_list($c)); | ||
| } | ||
|
|
||
| /** | ||
| * @param list{0?: string, 1?: string, 2?: string, 3?: string} $a | ||
| * @param list{0: string, 1?: string, 2?: string, 3?: string} $b | ||
| */ | ||
| public function testUnsetInt(array $a, array $b, array $c, int $int): void | ||
| { | ||
| assertType('true', array_is_list($a)); | ||
| assertType('true', array_is_list($b)); | ||
| unset($a[$int]); | ||
| unset($b[$int]); | ||
| assertType('bool', array_is_list($a)); | ||
| assertType('false', array_is_list($b)); | ||
| } | ||
| } |
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,27 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14177; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public function placeholderToEditor(string $html): void | ||
| { | ||
| $result = preg_replace_callback( | ||
| '~\[image\\sid="(\\d+)"(?:\\shref="([^"]*)")?(?:\\sclass="([^"]*)")?]~', | ||
| function (array $matches): string { | ||
| $id = (int) $matches[1]; | ||
|
|
||
| $replacement = sprintf( | ||
| '<img src="%s"%s/>', | ||
| $id, | ||
| array_key_exists(3, $matches) ? sprintf(' class="%s"', $matches[3]) : '', | ||
| ); | ||
|
|
||
| return array_key_exists(2, $matches) && $matches[2] !== '' | ||
| ? sprintf('<a href="%s">%s</a>', $matches[2], $replacement) | ||
| : $replacement; | ||
| }, | ||
| $html, | ||
| ); | ||
| } | ||
| } |
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.
you might be able to kill the mutation with somethig like
https://phpstan.org/r/53bdb42a-d790-4576-aa47-62e33eb40865