Repository navigation
Pass only the right-hand side of ??= as the written expression to PropertyAssignNode and VariableAssignNode
#6943
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
phpstan-bot
wants to merge
3
commits into
phpstan:2.3.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-9m9rb7q
base: 2.3.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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
84ee93e
Pass only the right-hand side of `??=` as the written expression to `…
VincentLanglet cded8d5
Test that other compound assignment operators check the same type as …
phpstan-bot d81c4c1
Check only the right-hand side of `??=` in OffsetAccessValueAssignmen…
phpstan-bot 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
55 changes: 55 additions & 0 deletions
55
tests/PHPStan/Rules/Arrays/data/offset-access-value-assignment-coalesce.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,55 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace OffsetAccessValueAssignmentCoalesce; | ||
|
|
||
| /** | ||
| * @implements \ArrayAccess<string, int> | ||
| */ | ||
| class Foo implements \ArrayAccess | ||
| { | ||
| public function offsetExists($offset): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| public function offsetGet($offset): ?string | ||
| { | ||
| return 'x'; | ||
| } | ||
|
|
||
| /** | ||
| * @param string|null $offset | ||
| * @param int $value | ||
| */ | ||
| public function offsetSet($offset, $value): void | ||
| { | ||
| } | ||
|
|
||
| public function offsetUnset($offset): void | ||
| { | ||
| } | ||
| } | ||
|
|
||
| function coalesceValid(Foo $foo): void | ||
| { | ||
| $foo['x'] ??= 5; | ||
| } | ||
|
|
||
| function coalesceInvalid(Foo $foo): void | ||
| { | ||
| $foo['x'] ??= 'boom'; | ||
| } | ||
|
|
||
| function ifValid(Foo $foo): void | ||
| { | ||
| if (null === $foo['x']) { | ||
| $foo['x'] = 5; | ||
| } | ||
| } | ||
|
|
||
| function ifInvalid(Foo $foo): void | ||
| { | ||
| if (null === $foo['x']) { | ||
| $foo['x'] = 'boom'; | ||
| } | ||
| } |
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,115 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug15228; | ||
|
|
||
| class CoalesceAssignDemo | ||
| { | ||
| public ?int $a = null; | ||
|
|
||
| /** @var list<int>|null */ | ||
| public $b; | ||
|
|
||
| /** @return list<string> */ | ||
| private function strings(): array | ||
| { | ||
| return ['x']; | ||
| } | ||
|
|
||
| public function coalesceInvalid(): void | ||
| { | ||
| $this->a ??= 'boom'; | ||
| } | ||
|
|
||
| public function ifInvalid(): void | ||
| { | ||
| if (null === $this->a) { | ||
| $this->a = 'boom'; | ||
| } | ||
| } | ||
|
|
||
| public function coalesceValid(): void | ||
| { | ||
| $this->b = $this->strings(); | ||
| $this->b ??= [1, 2]; | ||
| } | ||
|
|
||
| public function ifValid(): void | ||
| { | ||
| $this->b = $this->strings(); | ||
| if (null === $this->b) { | ||
| $this->b = [1, 2]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class StaticCoalesceAssignDemo | ||
| { | ||
| public static ?int $a = null; | ||
|
|
||
| /** @var list<int>|null */ | ||
| public static $b; | ||
|
|
||
| /** @return list<string> */ | ||
| private static function strings(): array | ||
| { | ||
| return ['x']; | ||
| } | ||
|
|
||
| public static function coalesceInvalid(): void | ||
| { | ||
| self::$a ??= 'boom'; | ||
| } | ||
|
|
||
| public static function coalesceValid(): void | ||
| { | ||
| self::$b = self::strings(); | ||
| self::$b ??= [1, 2]; | ||
| } | ||
| } | ||
|
|
||
| class OffsetCoalesceAssignDemo | ||
| { | ||
| /** @var array<string, int> */ | ||
| public array $a = []; | ||
|
|
||
| /** @var array<string, list<int>|null> */ | ||
| public array $b = []; | ||
|
|
||
| public function coalesceInvalid(): void | ||
| { | ||
| $this->a['x'] ??= 'boom'; | ||
| } | ||
|
|
||
| public function coalesceValid(): void | ||
| { | ||
| $this->b['x'] ??= [1, 2]; | ||
| } | ||
| } | ||
|
|
||
| class OtherAssignOpsDemo | ||
| { | ||
| public ?int $a = null; | ||
|
|
||
| /** @var int<0, 10> */ | ||
| public int $r = 0; | ||
|
|
||
| public function concat(): void | ||
| { | ||
| $this->a .= 'x'; | ||
| } | ||
|
|
||
| public function concatExpanded(): void | ||
| { | ||
| $this->a = $this->a . 'x'; | ||
| } | ||
|
|
||
| public function plus(): void | ||
| { | ||
| $this->r += 5; | ||
| } | ||
|
|
||
| public function plusExpanded(): void | ||
| { | ||
| $this->r = $this->r + 5; | ||
| } | ||
| } |
Oops, something went wrong.
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.
do we have the same problem with other assignment operators?
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.
No, the other assignment operators don't have this problem, so the code stays as it is. I added a test to show it and pushed it as
cded8d547.Why only
??=is affected: for$x OP= $vwith any other operator, the value of the whole expression is exactly what gets written to$x. Passing that expression toPropertyAssignNode/VariableAssignNodeis already correct.??=is the only one where the two differ: the expression's type,nonNull($x) | $v, includes the old value, which is kept and never written.Test: a new
OtherAssignOpsDemoclass intests/PHPStan/Rules/Properties/data/bug-15228.phpputs each compound assignment next to its expanded form:$this->a .= 'x'and$this->a = $this->a . 'x'both report "(int|null)does not accept string".$this->r += 5and$this->r = $this->r + 5both report "(int<0, 10>)does not acceptint<5, 15>".The errors match with
checkUnionTypeson and off. With it off, the+=pair isn't reported in either form, so they still agree.make testsandmake phpstanboth pass.One unrelated thing came up while checking:
$this->i /= $xon anintproperty, which givesint|float, isn't reported. The expanded$this->i = $this->i / $xisn't reported either, so it isn't specific to compound assignment and is outside this PR.