Skip to content

Commit

Permalink
Revert "Factory: properties in readonly classes are not readonly"
Browse files Browse the repository at this point in the history
This reverts commit ea40f2f.
  • Loading branch information
dg committed May 12, 2024
1 parent f055a0e commit b4da526
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpGenerator/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public function printClass(
}
}

$readOnlyClass = $class instanceof ClassType && $class->isReadOnly();
$consts = [];
$methods = [];
if (
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions tests/PhpGenerator/ClassType.readonly.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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());

0 comments on commit b4da526

Please sign in to comment.