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 45419ef commit 1274d3e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/PhpGenerator/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ private function addClassLikeToFile(PhpFile $phpFile, Node\Stmt\ClassLike $node)

$this->addCommentAndAttributes($class, $node);
$this->addClassMembers($class, $node);

if ($class instanceof ClassType && $class->isReadOnly()) {
array_map(fn($property) => $property->setReadOnly(), $class->getProperties());
}
return $class;
}

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 1274d3e

Please sign in to comment.