-
Notifications
You must be signed in to change notification settings - Fork 508
Keep list on unset() with nested dim-fetch #3964
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
staabm
wants to merge
17
commits into
phpstan:2.1.x
Choose a base branch
from
staabm:keep-list
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.
+264
−22
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ef319f3
Keep list on unset() with nested dim-fetch
staabm d1ebed8
added type assertions
staabm a0cd5cd
test overwriting elements
staabm f677acc
Added regression test
staabm 1b7edb3
Added regression test
staabm 89baf23
Added regression test
staabm d52a781
Added regression test
staabm 656fcf7
Update bug-12330.php
staabm f286df7
fix min php version
staabm 43cba0f
cs
staabm a1b6514
fix remaining part of bug8282
staabm 032d2e7
simplify
staabm a72010a
Use Type->setExistingOffsetValueType() more
staabm 678b2cc
adjust bug8113 expectations
staabm 2993e19
less precise types
staabm 6a67939
fix cs
staabm c9cc738
simplify
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
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,63 @@ | ||
<?php | ||
|
||
namespace Bug12927; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class HelloWorld | ||
{ | ||
/** | ||
* @param list<array{abc: string}> $list | ||
* @return list<array<string>> | ||
*/ | ||
public function sayHello(array $list): array | ||
{ | ||
foreach($list as $k => $v) { | ||
unset($list[$k]['abc']); | ||
assertType('non-empty-list<array{}|array{abc: string}>', $list); | ||
assertType('array{}|array{abc: string}', $list[$k]); | ||
} | ||
return $list; | ||
} | ||
|
||
/** | ||
* @param list<array<string, string>> $list | ||
*/ | ||
public function sayFoo(array $list): void | ||
{ | ||
foreach($list as $k => $v) { | ||
unset($list[$k]['abc']); | ||
assertType('non-empty-list<array<string, string>>', $list); | ||
assertType('array<string, string>', $list[$k]); | ||
} | ||
assertType('list<array<string, string>>', $list); | ||
} | ||
|
||
/** | ||
* @param list<array<string, string>> $list | ||
*/ | ||
public function sayFoo2(array $list): void | ||
{ | ||
foreach($list as $k => $v) { | ||
$list[$k]['abc'] = 'world'; | ||
assertType("non-empty-list<non-empty-array<string, string>&hasOffsetValue('abc', 'world')>", $list); | ||
assertType("non-empty-array<string, string>&hasOffsetValue('abc', 'world')", $list[$k]); | ||
} | ||
assertType("list<non-empty-array<string, string>&hasOffsetValue('abc', 'world')>", $list); | ||
} | ||
|
||
/** | ||
* @param list<array<string, string>> $list | ||
*/ | ||
public function sayFooBar(array $list): void | ||
{ | ||
foreach($list as $k => $v) { | ||
if (rand(0,1)) { | ||
unset($list[$k]); | ||
} | ||
assertType('array<int<0, max>, array<string, string>>', $list); | ||
assertType('array<string, string>', $list[$k]); | ||
} | ||
assertType('array<string, string>', $list[$k]); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace Bug11171; | ||
|
||
class TypeExpression | ||
{ | ||
public string $value; | ||
|
||
/** | ||
* @var list<array{start_index: int, expression: self}> | ||
*/ | ||
public array $innerTypeExpressions = []; | ||
|
||
/** | ||
* @param \Closure(self): void $callback | ||
*/ | ||
public function walkTypes(\Closure $callback): void | ||
{ | ||
$startIndexOffset = 0; | ||
|
||
foreach ($this->innerTypeExpressions as $k => ['start_index' => $startIndexOrig, | ||
'expression' => $inner,]) { | ||
$this->innerTypeExpressions[$k]['start_index'] += $startIndexOffset; | ||
|
||
$innerLengthOrig = \strlen($inner->value); | ||
|
||
$inner->walkTypes($callback); | ||
|
||
$this->value = substr_replace( | ||
$this->value, | ||
$inner->value, | ||
$startIndexOrig + $startIndexOffset, | ||
$innerLengthOrig | ||
); | ||
|
||
$startIndexOffset += \strlen($inner->value) - $innerLengthOrig; | ||
} | ||
|
||
$callback($this); | ||
} | ||
} |
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,31 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace Bug8282; | ||
|
||
/** | ||
* @phpstan-type record array{id: positive-int, name: string} | ||
*/ | ||
class Collection | ||
{ | ||
/** @param list<record> $list */ | ||
public function __construct( | ||
public array $list | ||
) | ||
{ | ||
} | ||
|
||
public function updateName(int $index, string $name): void | ||
{ | ||
assert(isset($this->list[$index])); | ||
$this->list[$index]['name'] = $name; | ||
} | ||
|
||
public function updateNameById(int $id, string $name): void | ||
{ | ||
foreach ($this->list as $index => $entry) { | ||
if ($entry['id'] === $id) { | ||
$this->list[$index]['name'] = $name; | ||
} | ||
} | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Bug12330; | ||
|
||
/** | ||
* @param array{items: list<array<string, mixed>>} $options | ||
* @param-out array{items: list<array<string, mixed>>} $options | ||
*/ | ||
function alterItems(array &$options): void | ||
{ | ||
foreach ($options['items'] as $i => $item) { | ||
$options['items'][$i]['options']['title'] = $item['name']; | ||
} | ||
} | ||
|
||
/** | ||
* @param array{items: array<int, array<string, mixed>>} $options | ||
* @param-out array{items: array<int, array<string, mixed>>} $options | ||
*/ | ||
function alterItems2(array &$options): void | ||
{ | ||
foreach ($options['items'] as $i => $item) { | ||
$options['items'][$i]['options']['title'] = $item['name']; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Bug12754; | ||
|
||
class HelloWorld | ||
{ | ||
/** | ||
* @param list<array{string, string}> $list | ||
* @return void | ||
*/ | ||
public function modify(array &$list): void | ||
{ | ||
foreach ($list as $int => $array) { | ||
$list[$int][1] = $this->apply($array[1]); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $value | ||
* @return string | ||
*/ | ||
public function apply(string $value): mixed | ||
{ | ||
return $value; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -34,15 +34,15 @@ function () { | |
]; | ||
assertType("non-empty-array<array<mixed>>&hasOffsetValue('Review', array{id: null, text: null, answer: null})&hasOffsetValue('SurveyInvitation', non-empty-array&hasOffsetValue('review', null))", $review); | ||
unset($review['SurveyInvitation']['review']); | ||
assertType("non-empty-array<array<mixed>>&hasOffsetValue('Review', array<mixed~'review', mixed>)&hasOffsetValue('SurveyInvitation', array<mixed~'review', mixed>)", $review); | ||
assertType("non-empty-array<array<mixed>>&hasOffsetValue('Review', array{id: null, text: null, answer: null})&hasOffsetValue('SurveyInvitation', array<mixed~'review', mixed>)", $review); | ||
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. I think this expectation change is correct.. previously |
||
} | ||
assertType('array<array<mixed>>', $review); | ||
if (array_key_exists('User', $review['Review'])) { | ||
assertType("non-empty-array<array<mixed>>&hasOffsetValue('Review', non-empty-array&hasOffset('User'))", $review); | ||
$review['User'] = $review['Review']['User']; | ||
assertType("non-empty-array&hasOffsetValue('Review', non-empty-array&hasOffset('User'))&hasOffsetValue('User', mixed)", $review); | ||
unset($review['Review']['User']); | ||
assertType("non-empty-array&hasOffsetValue('Review', array<mixed~'User', mixed>)&hasOffsetValue('User', array<mixed~'User', mixed>)", $review); | ||
assertType("non-empty-array&hasOffsetValue('Review', array<mixed~'User', mixed>)&hasOffsetValue('User', mixed)", $review); | ||
} | ||
assertType("non-empty-array&hasOffsetValue('Review', array)", $review); | ||
}; |
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.
The idea is that we're overwriting an existing offset so
$this->keyType
should stay intact as is in the original version of this method.Of course setExistingOffsetValueType was used when unsetting an offset so some details somewhere might be wrong, but what you're doing here now is basically the same thing
setOffsetValueType
is doing so it seems wrong.