diff --git a/src/Php/PhpVersions.php b/src/Php/PhpVersions.php index 893dbc8fa28..18a699c704f 100644 --- a/src/Php/PhpVersions.php +++ b/src/Php/PhpVersions.php @@ -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; diff --git a/src/Rules/Properties/ReadOnlyPropertyAssignRule.php b/src/Rules/Properties/ReadOnlyPropertyAssignRule.php index 2eafebcb38d..24a134e0749 100644 --- a/src/Rules/Properties/ReadOnlyPropertyAssignRule.php +++ b/src/Rules/Properties/ReadOnlyPropertyAssignRule.php @@ -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) { @@ -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; diff --git a/src/Rules/Properties/ReadOnlyPropertyRule.php b/src/Rules/Properties/ReadOnlyPropertyRule.php index 3df2c75db43..19a7707d5e0 100644 --- a/src/Rules/Properties/ReadOnlyPropertyRule.php +++ b/src/Rules/Properties/ReadOnlyPropertyRule.php @@ -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(); diff --git a/tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php b/tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php index 1e4af3d4f4a..330a39da68e 100644 --- a/tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php +++ b/tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php @@ -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 { diff --git a/tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php b/tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php index 98d73dc9e6a..b3ae9c753d7 100644 --- a/tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php +++ b/tests/PHPStan/Rules/Properties/ReadOnlyPropertyAssignRuleTest.php @@ -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 { diff --git a/tests/PHPStan/Rules/Properties/ReadOnlyPropertyRuleTest.php b/tests/PHPStan/Rules/Properties/ReadOnlyPropertyRuleTest.php index 61b16afebff..c6807c19b19 100644 --- a/tests/PHPStan/Rules/Properties/ReadOnlyPropertyRuleTest.php +++ b/tests/PHPStan/Rules/Properties/ReadOnlyPropertyRuleTest.php @@ -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]; } /** diff --git a/tests/PHPStan/Rules/Properties/data/readonly-property-default-value.php b/tests/PHPStan/Rules/Properties/data/readonly-property-default-value.php new file mode 100644 index 00000000000..ab0e7925752 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/readonly-property-default-value.php @@ -0,0 +1,122 @@ += 8.6 + +namespace ReadonlyPropertyDefaultValue; + +class Foo +{ + + public readonly int $prop = 1; + + public readonly ?string $nullable = null; + + public readonly int $noDefault; + + /** @var array */ + 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; + } + +}