Skip to content
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
5 changes: 5 additions & 0 deletions src/Php/PhpVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ public function supportsReadonlyPropertyReinitializationOnClone(): TrinaryLogic
return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
}

public function supportsReadonlyPropertyDefaultValues(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
}

public function supportsFirstClassCallables(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
Expand Down
15 changes: 14 additions & 1 deletion src/Rules/Properties/ReadOnlyPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ public function processNode(Node $node, Scope $scope): array

$methodName = $scopeMethod->getName();
$inClone = $scope->getPhpVersion()->supportsReadonlyPropertyReinitializationOnClone()->yes() && strtolower($methodName) === '__clone';
$inUnserialize = strtolower($methodName) === '__unserialize';
if (
in_array($methodName, $this->constructorsHelper->getConstructors($scopeClassReflection), true)
|| strtolower($methodName) === '__unserialize'
|| $inUnserialize
|| $inClone
) {
if (TypeUtils::findThisType($scope->getType($propertyFetch->var)) === null) {
Expand All @@ -111,6 +112,18 @@ public function processNode(Node $node, Scope $scope): array
->line($propertyFetch->name->getStartLine())
->identifier('assign.readOnlyProperty')
->build();
} elseif (
// the default value already initialized the property, only __clone() and __unserialize() can reinitialize it
!$inClone
&& !$inUnserialize
&& !$nativeReflection->isPromoted()
&& $nativeReflection->getNativeReflection()->hasDefaultValue()
&& !$node->isArrayAccessOffsetWrite($scope)
) {
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is already assigned.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->line($propertyFetch->name->getStartLine())
->identifier('assign.readOnlyProperty')
->build();
}

continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Properties/ReadOnlyPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
->build();
}

if ($node->getDefault() !== null) {
if ($node->getDefault() !== null && !$scope->getPhpVersion()->supportsReadonlyPropertyDefaultValues()->yes()) {
$errors[] = RuleErrorBuilder::message('Readonly property cannot have a default value.')->nonIgnorable()
->identifier('property.readOnlyDefaultValue')
->build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ public function testBug14983(): void
]);
}

#[RequiresPhp('>= 8.6.0-dev')]
public function testDefaultValue(): void
{
// properties with a default value are initialized, assignments are reported by ReadOnlyPropertyAssignRule
$this->analyse([__DIR__ . '/data/readonly-property-default-value.php'], [
[
'Readonly property ReadonlyPropertyDefaultValue\Foo::$promoted is already assigned.',
22,
],
]);
}

#[RequiresPhp('>= 8.1.0')]
public function testBug7119(): void
{
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,41 @@ public function testCloneWith(): void
$this->analyse([__DIR__ . '/data/readonly-property-assign-clone-with.php'], []);
}

#[RequiresPhp('>= 8.6.0-dev')]
public function testDefaultValue(): void
{
$this->analyse([__DIR__ . '/data/readonly-property-default-value.php'], [
[
'Readonly property ReadonlyPropertyDefaultValue\Foo::$prop is already assigned.',
19,
],
[
'Readonly property ReadonlyPropertyDefaultValue\Foo::$nullable is already assigned.',
20,
],
[
'Readonly property ReadonlyPropertyDefaultValue\Foo::$array is already assigned.',
23,
],
[
'Readonly property ReadonlyPropertyDefaultValue\Foo::$prop is assigned outside of the constructor.',
28,
],
[
'Readonly property ReadonlyPropertyDefaultValue\Foo::$prop is already assigned.',
39,
],
[
'Readonly property ReadonlyPropertyDefaultValue\ReadonlyClass::$prop is already assigned.',
51,
],
[
'Readonly property ReadonlyPropertyDefaultValue\CloneReinitializationTwice::$prop is already assigned.',
76,
],
]);
}

#[RequiresPhp('>= 8.1.0')]
public function testBug11495(): void
{
Expand Down
28 changes: 15 additions & 13 deletions tests/PHPStan/Rules/Properties/ReadOnlyPropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,24 @@ public static function dataRule(): iterable
return;
}

yield [
$errors = [
[
[
'Readonly property must have a native type.',
9,
],
[
'Readonly property cannot have a default value.',
10,
],
[
'Readonly property cannot be static.',
23,
],
'Readonly property must have a native type.',
9,
],
];
if (PHP_VERSION_ID < 80600) {
$errors[] = [
'Readonly property cannot have a default value.',
10,
];
}
$errors[] = [
'Readonly property cannot be static.',
23,
];

yield [$errors];
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php // lint >= 8.6

namespace ReadonlyPropertyDefaultValue;

class Foo
{

public readonly int $prop = 1;

public readonly ?string $nullable = null;

public readonly int $noDefault;

/** @var array<string, int> */
public readonly array $array = [];

public function __construct(int $prop, public readonly int $promoted = 1)
{
$this->prop = $prop;
$this->nullable = 'foo';
$this->noDefault = 1;
$this->promoted = 2;
$this->array['foo'] = 1;
}

public function setProp(int $prop): void
{
$this->prop = $prop;
}

}

class Child extends Foo
{

public function __construct()
{
parent::__construct(1);
$this->prop = 2;
}

}

readonly class ReadonlyClass
{

public int $prop = 1;

public function __construct()
{
$this->prop = 2;
}

}

class CloneReinitialization
{

public readonly int $prop = 1;

public function __clone()
{
$this->prop++;
}

}

class CloneReinitializationTwice
{

public readonly int $prop = 1;

public function __clone()
{
$this->prop = 2;
$this->prop = 3;
}

}

class CloneWith
{

public readonly int $prop = 1;

public function withProp(int $prop): static
{
return clone($this, ['prop' => $prop]);
}

}

class Unserialize
{

public readonly int $prop = 1;

/** @param array{prop: int} $data */
public function __unserialize(array $data): void
{
$this->prop = $data['prop'];
}

}

class ReadInConstructor
{

public readonly int $prop = 1;

public function __construct()
{
echo $this->prop;
$this->doFoo();
}

private function doFoo(): void
{
echo $this->prop;
}

}
Loading