Skip to content

Commit b4da526

Browse files
committed
Revert "Factory: properties in readonly classes are not readonly"
This reverts commit ea40f2f.
1 parent f055a0e commit b4da526

File tree

4 files changed

+6
-5
lines changed

4 files changed

+6
-5
lines changed

src/PhpGenerator/Extractor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ private function addPropertyToClass(ClassLike $class, Node\Stmt\Property $node):
329329
$prop->setValue($this->toValue($item->default));
330330
}
331331

332-
$prop->setReadOnly(method_exists($node, 'isReadonly') && $node->isReadonly());
332+
$prop->setReadOnly((method_exists($node, 'isReadonly') && $node->isReadonly()) || ($class instanceof ClassType && $class->isReadOnly()));
333333
$this->addCommentAndAttributes($prop, $node);
334334
}
335335
}

src/PhpGenerator/Factory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
257257
$prop->setType((string) $from->getType());
258258

259259
$prop->setInitialized($from->hasType() && array_key_exists($prop->getName(), $defaults));
260-
$prop->setReadOnly(PHP_VERSION_ID >= 80100 && $from->isReadOnly() && !(PHP_VERSION_ID >= 80200 && $from->getDeclaringClass()->isReadOnly()));
260+
$prop->setReadOnly(PHP_VERSION_ID >= 80100 && $from->isReadOnly());
261261
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
262262
$prop->setAttributes($this->getAttributes($from));
263263
return $prop;

src/PhpGenerator/Printer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public function printClass(
170170
}
171171
}
172172

173+
$readOnlyClass = $class instanceof ClassType && $class->isReadOnly();
173174
$consts = [];
174175
$methods = [];
175176
if (
@@ -203,7 +204,7 @@ public function printClass(
203204
$type = $property->getType();
204205
$def = (($property->getVisibility() ?: 'public')
205206
. ($property->isStatic() ? ' static' : '')
206-
. ($property->isReadOnly() && $type ? ' readonly' : '')
207+
. (!$readOnlyClass && $property->isReadOnly() && $type ? ' readonly' : '')
207208
. ' '
208209
. ltrim($this->printType($type, $property->isNullable()) . ' ')
209210
. '$' . $property->getName());

tests/PhpGenerator/ClassType.readonly.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ require __DIR__ . '/../bootstrap.php';
1414
require __DIR__ . '/fixtures/classes.82.php';
1515

1616
$class = ClassType::from(new Abc\Class13);
17-
Assert::false($class->getProperty('foo')->isReadOnly());
17+
Assert::true($class->getProperty('foo')->isReadOnly());
1818
Assert::false($class->getMethod('__construct')->getParameter('bar')->isReadOnly());
1919

2020
$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/classes.82.php')))->extractAll();
2121
$class = $file->getClasses()[Abc\Class13::class];
22-
Assert::false($class->getProperty('foo')->isReadOnly());
22+
Assert::true($class->getProperty('foo')->isReadOnly());
2323
Assert::false($class->getMethod('__construct')->getParameter('bar')->isReadOnly());

0 commit comments

Comments
 (0)