From b4da5264c4a45a11cc20dbb56c8e8c6a95c7e270 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 12 May 2024 15:41:01 +0200 Subject: [PATCH] Revert "Factory: properties in readonly classes are not readonly" This reverts commit ea40f2f8d743d5ca5a203f2ece4f80940aba115d. --- src/PhpGenerator/Extractor.php | 2 +- src/PhpGenerator/Factory.php | 2 +- src/PhpGenerator/Printer.php | 3 ++- tests/PhpGenerator/ClassType.readonly.phpt | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/PhpGenerator/Extractor.php b/src/PhpGenerator/Extractor.php index 6220cddd..e783ef71 100644 --- a/src/PhpGenerator/Extractor.php +++ b/src/PhpGenerator/Extractor.php @@ -329,7 +329,7 @@ private function addPropertyToClass(ClassLike $class, Node\Stmt\Property $node): $prop->setValue($this->toValue($item->default)); } - $prop->setReadOnly(method_exists($node, 'isReadonly') && $node->isReadonly()); + $prop->setReadOnly((method_exists($node, 'isReadonly') && $node->isReadonly()) || ($class instanceof ClassType && $class->isReadOnly())); $this->addCommentAndAttributes($prop, $node); } } diff --git a/src/PhpGenerator/Factory.php b/src/PhpGenerator/Factory.php index 544e3f35..431d28e8 100644 --- a/src/PhpGenerator/Factory.php +++ b/src/PhpGenerator/Factory.php @@ -257,7 +257,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property $prop->setType((string) $from->getType()); $prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults)); - $prop->setReadOnly(PHP_VERSION_ID >= 80100 && $from->isReadOnly() && !(PHP_VERSION_ID >= 80200 && $from->getDeclaringClass()->isReadOnly())); + $prop->setReadOnly(PHP_VERSION_ID >= 80100 && $from->isReadOnly()); $prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment())); $prop->setAttributes($this->getAttributes($from)); return $prop; diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 1d9528ae..ea2d5468 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -170,6 +170,7 @@ public function printClass( } } + $readOnlyClass = $class instanceof ClassType && $class->isReadOnly(); $consts = []; $methods = []; if ( @@ -203,7 +204,7 @@ public function printClass( $type = $property->getType(); $def = (($property->getVisibility() ?: 'public') . ($property->isStatic() ? ' static' : '') - . ($property->isReadOnly() && $type ? ' readonly' : '') + . (!$readOnlyClass && $property->isReadOnly() && $type ? ' readonly' : '') . ' ' . ltrim($this->printType($type, $property->isNullable()) . ' ') . '$' . $property->getName()); diff --git a/tests/PhpGenerator/ClassType.readonly.phpt b/tests/PhpGenerator/ClassType.readonly.phpt index 07817b4c..1f34721b 100644 --- a/tests/PhpGenerator/ClassType.readonly.phpt +++ b/tests/PhpGenerator/ClassType.readonly.phpt @@ -14,10 +14,10 @@ require __DIR__ . '/../bootstrap.php'; require __DIR__ . '/fixtures/classes.82.php'; $class = ClassType::from(new Abc\Class13); -Assert::false($class->getProperty('foo')->isReadOnly()); +Assert::true($class->getProperty('foo')->isReadOnly()); Assert::false($class->getMethod('__construct')->getParameter('bar')->isReadOnly()); $file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/classes.82.php')))->extractAll(); $class = $file->getClasses()[Abc\Class13::class]; -Assert::false($class->getProperty('foo')->isReadOnly()); +Assert::true($class->getProperty('foo')->isReadOnly()); Assert::false($class->getMethod('__construct')->getParameter('bar')->isReadOnly());