Skip to content

Commit

Permalink
Check also AssignRef in all assignment rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 13, 2020
1 parent 9afec60 commit 1639213
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Rules/Arrays/AppendedArrayItemTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignRef;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -41,6 +42,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array
if (
!$node instanceof Assign
&& !$node instanceof AssignOp
&& !$node instanceof AssignRef
) {
return [];
}
Expand All @@ -66,7 +68,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array
return [];
}

if ($node instanceof Assign) {
if ($node instanceof Assign || $node instanceof AssignRef) {
$assignedValueType = $scope->getType($node->expr);
} else {
$assignedValueType = $scope->getType($node);
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Arrays/OffsetAccessValueAssignmentRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array
if (
!$node instanceof Assign
&& !$node instanceof AssignOp
&& !$node instanceof Expr\AssignRef
) {
return [];
}
Expand All @@ -46,7 +47,7 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array

$arrayDimFetch = $node->var;

if ($node instanceof Assign) {
if ($node instanceof Assign || $node instanceof Expr\AssignRef) {
$assignedValueType = $scope->getType($node->expr);
} else {
$assignedValueType = $scope->getType($node);
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Properties/TypesAssignedToPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function processNode(Node $node, Scope $scope): array
if (
!$node instanceof Node\Expr\Assign
&& !$node instanceof Node\Expr\AssignOp
&& !$node instanceof Node\Expr\AssignRef
) {
return [];
}
Expand All @@ -61,7 +62,7 @@ public function processNode(Node $node, Scope $scope): array

$propertyType = $propertyReflection->getWritableType();

if ($node instanceof Node\Expr\Assign) {
if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignRef) {
$assignedValueType = $scope->getType($node->expr);
} else {
$assignedValueType = $scope->getType($node);
Expand Down
1 change: 1 addition & 0 deletions src/Rules/Properties/WritingToReadOnlyPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function processNode(Node $node, Scope $scope): array
if (
!$node instanceof Node\Expr\Assign
&& !$node instanceof Node\Expr\AssignOp
&& !$node instanceof Node\Expr\AssignRef
) {
return [];
}
Expand Down
10 changes: 7 additions & 3 deletions tests/PHPStan/Rules/Arrays/AppendedArrayItemTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ public function testAppendedArrayItemType(): void
],
[
'Array (array<int>) does not accept string.',
30,
27,
],
[
'Array (array<int>) does not accept string.',
32,
],
[
'Array (array<callable(): string>) does not accept Closure(): int.',
43,
45,
],
[
'Array (array<AppendedArrayItem\Lorem>) does not accept AppendedArrayItem\Baz.',
77,
79,
],
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ public function testRule(): void
'ArrayAccess<int, int> does not accept array<int, string>.',
21,
],
[
'ArrayAccess<int, int> does not accept string.',
24,
],
[
'ArrayAccess<int, int> does not accept float.',
35,
38,
],
]);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/appended-array-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function doFoo()
$this->callables[] = [__CLASS__, 'classMethod'];
$world = 'world';
$this->callables[] = ['Foo', "Hello $world"];

$this->integers[] = &$world;
}

public function assignOp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function doFoo(\ArrayAccess $arrayAccess): void

$arrayAccess[] = 'baz';
$arrayAccess[] = ['foo'];

$s = 'foo';
$arrayAccess[] = &$s;
}

public function doBar(int $test): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public function testTypesAssignedToProperties(): void
'Static property PropertiesAssignedTypes\Ipsum::$fooStatic (PropertiesAssignedTypes\Ipsum) does not accept PropertiesAssignedTypes\Bar.',
144,
],
[
'Property PropertiesAssignedTypes\AssignRefFoo::$stringProperty (string) does not accept int.',
312,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function testCheckAllProperties(): void
'Property WritingToReadOnlyProperties\Foo::$readOnlyProperty is not writable.',
26,
],
[
'Property WritingToReadOnlyProperties\Foo::$readOnlyProperty is not writable.',
35,
],
]);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Properties/data/properties-assigned-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,17 @@ function test(): void
$this->var = [[]];
}
}

class AssignRefFoo
{

/** @var string */
private $stringProperty;

public function doFoo()
{
$i = 1;
$this->stringProperty = &$i;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function doFoo()

$self->writeOnlyProperty = 1;
$self->writeOnlyProperty .= 1;

$s = 'foo';
$self->readOnlyProperty = &$s;
}

}

0 comments on commit 1639213

Please sign in to comment.