Skip to content

Commit eebd086

Browse files
authored
[Php81] Skip readonly on property returned by reference in ReadOnlyPropertyRector (#8108)
1 parent 0180e9b commit eebd086

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;
4+
5+
final class SkipReturnedByRef
6+
{
7+
public function __construct(
8+
private array $byLine = [],
9+
) {
10+
}
11+
12+
public function &getByLine(): array
13+
{
14+
return $this->byLine;
15+
}
16+
}

‎rules/Php81/Rector/Property/ReadOnlyPropertyRector.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpParser\Node\Stmt\Class_;
1515
use PhpParser\Node\Stmt\ClassMethod;
1616
use PhpParser\Node\Stmt\Property;
17+
use PhpParser\Node\Stmt\Return_;
1718
use PhpParser\NodeVisitor;
1819
use PHPStan\Analyser\Scope;
1920
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
@@ -174,6 +175,11 @@ private function refactorProperty(Class_ $class, Property $property, Scope $scop
174175
return null;
175176
}
176177

178+
// returned by reference can be mutated outside the class, so it cannot be readonly
179+
if ($this->isPropertyReturnedByRef($class, (string) $this->getName($property))) {
180+
return null;
181+
}
182+
177183
$this->visibilityManipulator->makeReadonly($property);
178184
$this->removeReadOnlyDoc($property);
179185

@@ -236,13 +242,47 @@ private function refactorParam(Class_ $class, ClassMethod $classMethod, Param $p
236242
return null;
237243
}
238244

245+
// returned by reference can be mutated outside the class, so it cannot be readonly
246+
if ($this->isPropertyReturnedByRef($class, (string) $this->getName($param))) {
247+
return null;
248+
}
249+
239250
$this->visibilityManipulator->makeReadonly($param);
240251

241252
$this->removeReadOnlyDoc($param);
242253

243254
return $param;
244255
}
245256

257+
private function isPropertyReturnedByRef(Class_ $class, string $propertyName): bool
258+
{
259+
foreach ($class->getMethods() as $classMethod) {
260+
if (! $classMethod->byRef) {
261+
continue;
262+
}
263+
264+
$returns = $this->betterNodeFinder->findInstanceOf($classMethod, Return_::class);
265+
foreach ($returns as $return) {
266+
if (! $return->expr instanceof Expr) {
267+
continue;
268+
}
269+
270+
$propertyFetch = $this->betterNodeFinder->findFirst(
271+
$return->expr,
272+
fn (Node $subNode): bool => $subNode instanceof PropertyFetch
273+
&& $this->isName($subNode->var, 'this')
274+
&& $this->isName($subNode, $propertyName)
275+
);
276+
277+
if ($propertyFetch instanceof PropertyFetch) {
278+
return true;
279+
}
280+
}
281+
}
282+
283+
return false;
284+
}
285+
246286
private function isPromotedPropertyAssigned(Class_ $class, Param $param): bool
247287
{
248288
$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);

0 commit comments

Comments
 (0)