-
Notifications
You must be signed in to change notification settings - Fork 574
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
Merged
Merged
Changes from 2 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,119 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14177; | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| /** | ||
| * @param list{0: string, 1: string, 2?: string, 3?: string} $b | ||
| */ | ||
| public function testUnset0(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 testUnset1(array $b): void | ||
| { | ||
| assertType('true', array_is_list($b)); | ||
| unset($b[1]); | ||
| assertType('bool', 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 testUnset2(array $b): void | ||
| { | ||
| assertType('true', array_is_list($b)); | ||
| unset($b[2]); | ||
| assertType('bool', 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 testUnset3(array $b): void | ||
| { | ||
| assertType('true', array_is_list($b)); | ||
| unset($b[3]); | ||
| assertType('true', array_is_list($b)); | ||
| $b[] = 'foo'; | ||
| assertType('false', array_is_list($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, | ||
| ); | ||
| } | ||
| } |
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.
Not always, right? The new type is a list only if we're removing an optional key and:
If you're going to implement this logic, add failing tests first 😊
Uh oh!
There was an error while loading. Please reload this page.
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.
Per phpstan/phpstan#12768 even unsetting the last item makes it no longer a list because of internal pointer and append with [] will create a hole
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.
Yeah but the point is that we're unsetting something that might not be on the array. So it might still be a list.