diff --git a/src/Knowledge/ParsedDeclarationSource.php b/src/Knowledge/ParsedDeclarationSource.php index 45534a22..502977e6 100644 --- a/src/Knowledge/ParsedDeclarationSource.php +++ b/src/Knowledge/ParsedDeclarationSource.php @@ -95,7 +95,7 @@ private function classInfoFromNode(Stmt\ClassLike $node, string $uri): ClassInfo kind: $this->determineKind($node), isAbstract: $node instanceof Stmt\Class_ && $node->isAbstract(), isFinal: $node instanceof Stmt\Class_ && $node->isFinal(), - isReadonly: $node instanceof Stmt\Class_ && $node->isReadonly(), + isReadonly: $this->isReadonlyClass($node), isAttribute: $this->isAttributeNode($node), parent: $this->resolveParent($node), interfaces: $this->extractInterfaces($node), @@ -298,6 +298,7 @@ private function extractParameters(array $params): array private function extractProperties(Stmt\ClassLike $node, ClasslikeName $className, string $filePath): array { $properties = []; + $classIsReadonly = $this->isReadonlyClass($node); if ($node instanceof Stmt\Enum_) { $properties = EnumImplicits::properties($className, $this->enumScalarType($node)); @@ -314,7 +315,7 @@ private function extractProperties(Stmt\ClassLike $node, ClasslikeName $classNam name: new PropertyName($className, $name), visibility: $this->visibilityFromFlags($stmt->flags), isStatic: $stmt->isStatic(), - isReadonly: $stmt->isReadonly(), + isReadonly: $classIsReadonly || $stmt->isReadonly(), isPromoted: false, type: TypeFactory::fromNode($stmt->type), docblock: $stmt->getDocComment()?->getText(), @@ -338,7 +339,7 @@ private function extractProperties(Stmt\ClassLike $node, ClasslikeName $classNam name: new PropertyName($className, $name), visibility: $this->visibilityFromFlags($param->flags), isStatic: false, - isReadonly: $param->isReadonly(), + isReadonly: $classIsReadonly || $param->isReadonly(), isPromoted: true, type: TypeFactory::fromNode($param->type), docblock: $param->getDocComment()?->getText(), @@ -443,6 +444,11 @@ private function isAttributeNode(Stmt\ClassLike $node): bool return false; } + private function isReadonlyClass(Stmt\ClassLike $node): bool + { + return $node instanceof Stmt\Class_ && $node->isReadonly(); + } + private function parameterFromNode(Param $param, int $position): ?ParameterInfo { if (!$param->var instanceof Variable || !is_string($param->var->name)) { diff --git a/tests/Fixtures/src/Utility/ClassModifiers.php b/tests/Fixtures/src/Utility/ClassModifiers.php index 06000afe..5c4cb19d 100644 --- a/tests/Fixtures/src/Utility/ClassModifiers.php +++ b/tests/Fixtures/src/Utility/ClassModifiers.php @@ -18,6 +18,8 @@ public function doSomething(): void readonly class ImmutableClass { + public int $count; + public function __construct( public string $value, ) { diff --git a/tests/Knowledge/ParsedDeclarationSourceTest.php b/tests/Knowledge/ParsedDeclarationSourceTest.php index 4f5b6853..4ffcc2fd 100644 --- a/tests/Knowledge/ParsedDeclarationSourceTest.php +++ b/tests/Knowledge/ParsedDeclarationSourceTest.php @@ -37,6 +37,8 @@ final class ParsedDeclarationSourceTest extends TestCase /** Declares all three kinds, so a lookup reading the wrong list is visible. */ private const string FIXTURE = 'AutoloadFiles/helpers.php'; + private const array IMMUTABLE = ['src/Utility/ClassModifiers.php', 'Fixtures\Utility\ImmutableClass']; + private const array PARENT_CLASS = ['src/Inheritance/ParentClass.php', 'Fixtures\Inheritance\ParentClass']; private const array USER = ['src/Domain/User.php', 'Fixtures\Domain\User']; @@ -721,6 +723,8 @@ public static function properties(): iterable ]; yield 'public static' => [...self::PARENT_CLASS, 'staticProperty', Visibility::Public, true, false, $string]; yield 'protected' => [...self::PARENT_CLASS, 'protectedProperty', Visibility::Protected, false, false, $int]; + yield 'declared in readonly class' => [...self::IMMUTABLE, 'count', Visibility::Public, false, true, $int]; + yield 'promoted in readonly class' => [...self::IMMUTABLE, 'value', Visibility::Public, false, true, $string]; } #[DataProvider('properties')] @@ -737,7 +741,7 @@ public function testPropertyCarriesItsModifiersAndType( self::assertSame($visibility, $info->visibility, 'the visibility keyword decides visibility'); self::assertSame($isStatic, $info->isStatic, 'only a static property is static'); - self::assertSame($isReadonly, $info->isReadonly, 'only a readonly property is readonly'); + self::assertSame($isReadonly, $info->isReadonly, 'a property is readonly by its own modifier or its class\'s'); self::assertEquals($type, $info->type, 'the declared type is carried'); }