Skip to content

Commit a7188ef

Browse files
takaramondrejmirtes
authored andcommitted
$this cannot be reassigned when passed by reference
1 parent b4b0887 commit a7188ef

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4524,8 +4524,10 @@ private function processArgs(
45244524
if ($assignByReference) {
45254525
$argValue = $arg->value;
45264526
if ($argValue instanceof Variable && is_string($argValue->name)) {
4527-
$nodeCallback(new VariableAssignNode($argValue, new TypeExpr($byRefType), false), $scope);
4528-
$scope = $scope->assignVariable($argValue->name, $byRefType, new MixedType());
4527+
if ($argValue->name !== 'this') {
4528+
$nodeCallback(new VariableAssignNode($argValue, new TypeExpr($byRefType), false), $scope);
4529+
$scope = $scope->assignVariable($argValue->name, $byRefType, new MixedType());
4530+
}
45294531
} else {
45304532
$scope = $scope->invalidateExpression($argValue);
45314533
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,7 @@ public function dataFileAsserts(): iterable
14961496
yield from $this->gatherAssertTypes(__DIR__ . '/data/case-insensitive-parent.php');
14971497
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10893.php');
14981498
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4754.php');
1499+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10477.php');
14991500
}
15001501

15011502
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bug10477;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class A
8+
{
9+
protected $data;
10+
protected $foo = '';
11+
12+
public function a(): void
13+
{
14+
assertType('mixed', $this->foo);
15+
assertType('$this(Bug10477\A)', $this);
16+
(new B())->foo($this);
17+
assertType('mixed', $this->foo);
18+
assertType('$this(Bug10477\A)', $this);
19+
if (isset($this->data['test'])) {
20+
$this->foo = $this->data['test'];
21+
}
22+
}
23+
}
24+
25+
class B
26+
{
27+
public function foo(mixed &$var): void {}
28+
}

tests/PHPStan/Rules/Properties/AccessPropertiesInAssignRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,9 @@ public function testConflictingAnnotationProperty(): void
120120
$this->analyse([__DIR__ . '/data/conflicting-annotation-property.php'], []);
121121
}
122122

123+
public function testBug10477(): void
124+
{
125+
$this->analyse([__DIR__ . '/../../Analyser/data/bug-10477.php'], []);
126+
}
127+
123128
}

0 commit comments

Comments
 (0)