Skip to content

Commit 1639213

Browse files
committed
Check also AssignRef in all assignment rules
1 parent 9afec60 commit 1639213

12 files changed

Lines changed: 50 additions & 7 deletions

src/Rules/Arrays/AppendedArrayItemTypeRule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node\Expr\ArrayDimFetch;
66
use PhpParser\Node\Expr\Assign;
77
use PhpParser\Node\Expr\AssignOp;
8+
use PhpParser\Node\Expr\AssignRef;
89
use PHPStan\Analyser\Scope;
910
use PHPStan\Rules\Properties\PropertyReflectionFinder;
1011
use PHPStan\Rules\RuleErrorBuilder;
@@ -41,6 +42,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array
4142
if (
4243
!$node instanceof Assign
4344
&& !$node instanceof AssignOp
45+
&& !$node instanceof AssignRef
4446
) {
4547
return [];
4648
}
@@ -66,7 +68,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array
6668
return [];
6769
}
6870

69-
if ($node instanceof Assign) {
71+
if ($node instanceof Assign || $node instanceof AssignRef) {
7072
$assignedValueType = $scope->getType($node->expr);
7173
} else {
7274
$assignedValueType = $scope->getType($node);

src/Rules/Arrays/OffsetAccessValueAssignmentRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array
3636
if (
3737
!$node instanceof Assign
3838
&& !$node instanceof AssignOp
39+
&& !$node instanceof Expr\AssignRef
3940
) {
4041
return [];
4142
}
@@ -46,7 +47,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array
4647

4748
$arrayDimFetch = $node->var;
4849

49-
if ($node instanceof Assign) {
50+
if ($node instanceof Assign || $node instanceof Expr\AssignRef) {
5051
$assignedValueType = $scope->getType($node->expr);
5152
} else {
5253
$assignedValueType = $scope->getType($node);

src/Rules/Properties/TypesAssignedToPropertiesRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function processNode(Node $node, Scope $scope): array
4141
if (
4242
!$node instanceof Node\Expr\Assign
4343
&& !$node instanceof Node\Expr\AssignOp
44+
&& !$node instanceof Node\Expr\AssignRef
4445
) {
4546
return [];
4647
}
@@ -61,7 +62,7 @@ public function processNode(Node $node, Scope $scope): array
6162

6263
$propertyType = $propertyReflection->getWritableType();
6364

64-
if ($node instanceof Node\Expr\Assign) {
65+
if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef) {
6566
$assignedValueType = $scope->getType($node->expr);
6667
} else {
6768
$assignedValueType = $scope->getType($node);

src/Rules/Properties/WritingToReadOnlyPropertiesRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function processNode(Node $node, Scope $scope): array
4444
if (
4545
!$node instanceof Node\Expr\Assign
4646
&& !$node instanceof Node\Expr\AssignOp
47+
&& !$node instanceof Node\Expr\AssignRef
4748
) {
4849
return [];
4950
}

tests/PHPStan/Rules/Arrays/AppendedArrayItemTypeRuleTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ public function testAppendedArrayItemType(): void
4242
],
4343
[
4444
'Array (array<int>) does not accept string.',
45-
30,
45+
27,
46+
],
47+
[
48+
'Array (array<int>) does not accept string.',
49+
32,
4650
],
4751
[
4852
'Array (array<callable(): string>) does not accept Closure(): int.',
49-
43,
53+
45,
5054
],
5155
[
5256
'Array (array<AppendedArrayItem\Lorem>) does not accept AppendedArrayItem\Baz.',
53-
77,
57+
79,
5458
],
5559
]
5660
);

tests/PHPStan/Rules/Arrays/OffsetAccessValueAssignmentRuleTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ public function testRule(): void
3636
'ArrayAccess<int, int> does not accept array<int, string>.',
3737
21,
3838
],
39+
[
40+
'ArrayAccess<int, int> does not accept string.',
41+
24,
42+
],
3943
[
4044
'ArrayAccess<int, int> does not accept float.',
41-
35,
45+
38,
4246
],
4347
]);
4448
}

tests/PHPStan/Rules/Arrays/data/appended-array-item.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public function doFoo()
2323
$this->callables[] = [__CLASS__, 'classMethod'];
2424
$world = 'world';
2525
$this->callables[] = ['Foo', "Hello $world"];
26+
27+
$this->integers[] = &$world;
2628
}
2729

2830
public function assignOp()

tests/PHPStan/Rules/Arrays/data/offset-access-value-assignment.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public function doFoo(\ArrayAccess $arrayAccess): void
1919

2020
$arrayAccess[] = 'baz';
2121
$arrayAccess[] = ['foo'];
22+
23+
$s = 'foo';
24+
$arrayAccess[] = &$s;
2225
}
2326

2427
public function doBar(int $test): void

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public function testTypesAssignedToProperties(): void
7474
'Static property PropertiesAssignedTypes\Ipsum::$fooStatic (PropertiesAssignedTypes\Ipsum) does not accept PropertiesAssignedTypes\Bar.',
7575
144,
7676
],
77+
[
78+
'Property PropertiesAssignedTypes\AssignRefFoo::$stringProperty (string) does not accept int.',
79+
312,
80+
],
7781
]);
7882
}
7983

tests/PHPStan/Rules/Properties/WritingToReadOnlyPropertiesRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public function testCheckAllProperties(): void
5353
'Property WritingToReadOnlyProperties\Foo::$readOnlyProperty is not writable.',
5454
26,
5555
],
56+
[
57+
'Property WritingToReadOnlyProperties\Foo::$readOnlyProperty is not writable.',
58+
35,
59+
],
5660
]);
5761
}
5862

0 commit comments

Comments
 (0)