Skip to content

Fix readonly property assign with ArrayAccess offset #3817

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Rules/Properties/ReadOnlyByPhpDocPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace PHPStan\Rules\Properties;

use ArrayAccess;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
use PHPStan\Node\Expr\UnsetOffsetExpr;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeUtils;
use function in_array;
use function sprintf;
Expand Down Expand Up @@ -106,6 +110,14 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$assignedExpr = $node->getAssignedExpr();
if (
($assignedExpr instanceof SetOffsetValueTypeExpr || $assignedExpr instanceof UnsetOffsetExpr)
&& (new ObjectType(ArrayAccess::class))->isSuperTypeOf($scope->getType($assignedExpr->getVar()))->yes()
) {
continue;
}

$errors[] = RuleErrorBuilder::message(sprintf('@readonly property %s::$%s is assigned outside of the constructor.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->identifier('property.readOnlyByPhpDocAssignNotInConstructor')
->build();
Expand Down
12 changes: 12 additions & 0 deletions src/Rules/Properties/ReadOnlyPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace PHPStan\Rules\Properties;

use ArrayAccess;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
use PHPStan\Node\Expr\UnsetOffsetExpr;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeUtils;
use function in_array;
use function sprintf;
Expand Down Expand Up @@ -89,6 +93,14 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$assignedExpr = $node->getAssignedExpr();
if (
($assignedExpr instanceof SetOffsetValueTypeExpr || $assignedExpr instanceof UnsetOffsetExpr)
&& (new ObjectType(ArrayAccess::class))->isSuperTypeOf($scope->getType($assignedExpr->getVar()))->yes()
) {
continue;
}

$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is assigned outside of the constructor.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->identifier('property.readOnlyAssignNotInConstructor')
->build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public function testRule(): void
'@readonly property ReadonlyPropertyAssignPhpDoc\C::$c is assigned outside of the constructor.',
293,
],
[
'@readonly property ReadonlyPropertyAssignPhpDoc\ArrayAccessPropertyFetch::$storage is assigned outside of the constructor.',
311,
],
]);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public function testRule(): void
];
}

$errors[] = [
'Readonly property ReadonlyPropertyAssign\ArrayAccessPropertyFetch::$storage is assigned outside of the constructor.',
212,
];

$this->analyse([__DIR__ . '/data/readonly-assign.php'], $errors);
}

Expand Down Expand Up @@ -168,4 +173,22 @@ public function testBug6773(): void
]);
}

public function testBug8929(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/bug-8929.php'], []);
}

public function testBug12537(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/bug-12537.php'], []);
}

}
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-12537.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace Bug12537;

use WeakMap;

class Metadata {
/**
* @var WeakMap<stdClass, int>
*/
private readonly WeakMap $storage;

public function __construct() {
$this->storage = new WeakMap();
}

public function set(stdClass $class, int $value): void {
$this->storage[$class] = $value;
}

public function get(stdClass $class): mixed {
return $this->storage[$class] ?? null;
}
}

$class = new stdClass();
$meta = new Metadata();

$meta->set($class, 123);

var_dump($meta->get($class));
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-8929.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace Bug8929;

class Test
{
/** @var \WeakMap<object, mixed> */
protected readonly \WeakMap $cache;

public function __construct()
{
$this->cache = new \WeakMap();
}

public function add(object $key, mixed $value): void
{
$this->cache[$key] = $value;
}
}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Properties/data/readonly-assign-phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,21 @@ public function mod()
}

}

class ArrayAccessPropertyFetch
{

/** @readonly */
private \ArrayObject $storage;

public function __construct() {
$this->storage = new \ArrayObject();
}

public function set(\stdClass $class, int $value): void {
$this->storage[$class] = $value;
unset($this->storage[$class]);
$this->storage = new \WeakMap(); // invalid
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Properties/data/readonly-assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,20 @@ protected function setUp(): void
}

}

class ArrayAccessPropertyFetch
{

private readonly \ArrayObject $storage;

public function __construct() {
$this->storage = new \ArrayObject();
}

public function set(\stdClass $class, int $value): void {
$this->storage[$class] = $value;
unset($this->storage[$class]);
$this->storage = new \WeakMap(); // invalid
}

}
Loading