Skip to content
Open
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
17 changes: 12 additions & 5 deletions src/Rules/Properties/ReadOnlyPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
use PHPStan\Node\Expr\UnsetOffsetExpr;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
Expand All @@ -30,6 +31,7 @@ final class ReadOnlyPropertyAssignRule implements Rule
public function __construct(
private PropertyReflectionFinder $propertyReflectionFinder,
private ConstructorsHelper $constructorsHelper,
private PhpVersion $phpVersion,
)
{
}
Expand Down Expand Up @@ -77,11 +79,16 @@ public function processNode(Node $node, Scope $scope): array

$scopeClassReflection = $scope->getClassReflection();
if ($scopeClassReflection->getName() !== $declaringClass->getName()) {
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is assigned outside of its declaring class.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->line($propertyFetch->name->getStartLine())
->identifier('property.readOnlyAssignOutOfClass')
->build();
continue;
$allowedInSubclass = $this->phpVersion->supportsAsymmetricVisibility()
&& !$propertyReflection->isPrivateSet()
&& $scopeClassReflection->isSubclassOfClass($propertyReflection->getDeclaringClass());
if (!$allowedInSubclass) {
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is assigned outside of its declaring class.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->line($propertyFetch->name->getStartLine())
->identifier('property.readOnlyAssignOutOfClass')
->build();
continue;
}
}

$scopeMethod = $scope->getFunction();
Expand Down
42 changes: 33 additions & 9 deletions tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Properties;

use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
Expand All @@ -25,6 +26,7 @@ protected function getRule(): Rule
'ReadonlyPropertyAssign\\TestCase::setUp',
],
),
new PhpVersion(PHP_VERSION_ID),
);
}

Expand All @@ -36,26 +38,35 @@ public function testRule(): void
'Readonly property ReadonlyPropertyAssign\Foo::$foo is assigned outside of the constructor.',
21,
],
[
];

if (PHP_VERSION_ID < 80400) {
// Since PHP 8.4, readonly is implicitly protected(set),
// so child classes may initialize the property.
$errors[] = [
'Readonly property ReadonlyPropertyAssign\Foo::$bar is assigned outside of its declaring class.',
33,
],
[
];
$errors[] = [
'Readonly property ReadonlyPropertyAssign\Foo::$baz is assigned outside of its declaring class.',
34,
],
[
];
$errors[] = [
'Readonly property ReadonlyPropertyAssign\Foo::$bar is assigned outside of its declaring class.',
39,
],
];

if (PHP_VERSION_ID < 80400) {
];
// reported by AccessPropertiesInAssignRule on 8.4+
$errors[] = [
'Readonly property ReadonlyPropertyAssign\Foo::$baz is assigned outside of its declaring class.',
46,
];
} else {
// On PHP 8.4+ the assignment is allowed by visibility rules,
// but still has to happen in a constructor of the child class.
$errors[] = [
'Readonly property ReadonlyPropertyAssign\Foo::$bar is assigned outside of the constructor.',
39,
];
}

$errors = array_merge($errors, [
Expand Down Expand Up @@ -180,4 +191,17 @@ public function testCloneWith(): void
$this->analyse([__DIR__ . '/data/readonly-property-assign-clone-with.php'], []);
}

#[RequiresPhp('>= 8.4.0')]
public function testBug12871(): void
{
// The private(set) assignment in a subclass is reported by AccessPropertiesInAssignRule,
// so this rule only reports the write outside of a constructor.
$this->analyse([__DIR__ . '/data/bug-12871.php'], [
[
'Readonly property Bug12871\A::$foo is assigned outside of the constructor.',
54,
],
]);
}

}
57 changes: 57 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-12871.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php // lint >= 8.4

namespace Bug12871;

abstract readonly class A
{

protected string $foo;

public function __construct()
{
$this->foo = '';
}

}

readonly class B extends A
{

public function __construct()
{
$this->foo = 'foo';
}

}

readonly class PrivateSetParent
{

public private(set) string $bar;

public function __construct()
{
$this->bar = '';
}

}

readonly class PrivateSetChild extends PrivateSetParent
{

public function __construct()
{
$this->bar = 'nope'; // report - private(set)
}

}

readonly class NonConstructorChild extends A
{

public function init(): void
{
$this->foo = 'nope'; // report - outside constructor
}

}
Loading