Skip to content

Commit

Permalink
[PropertyAccess] Readonly properties must have no PropertyWriteInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
CasvanDongen authored and nicolas-grekas committed Apr 17, 2023
1 parent 8454a44 commit ff45ebb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Extractor/ReflectionExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,12 @@ public function getWriteInfo(string $class, string $property, array $context = [

if ($reflClass->hasProperty($property) && ($reflClass->getProperty($property)->getModifiers() & $this->propertyReflectionFlags)) {
$reflProperty = $reflClass->getProperty($property);
if (\PHP_VERSION_ID < 80100 || !$reflProperty->isReadOnly()) {
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY, $property, $this->getWriteVisiblityForProperty($reflProperty), $reflProperty->isStatic());
}

return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY, $property, $this->getWriteVisiblityForProperty($reflProperty), $reflProperty->isStatic());
$errors[] = [sprintf('The property "%s" in class "%s" is a promoted readonly property.', $property, $reflClass->getName())];
$allowMagicSet = $allowMagicCall = false;
}

if ($allowMagicSet) {
Expand Down
12 changes: 12 additions & 0 deletions Tests/Extractor/ReflectionExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,18 @@ public function testGetWriteInfoDeprecatedEnableMagicCallExtractionInContext()
]);
}

/**
* @requires PHP 8.1
*/
public function testGetWriteInfoReadonlyProperties()
{
$writeMutatorConstructor = $this->extractor->getWriteInfo(Php81Dummy::class, 'foo', ['enable_constructor_extraction' => true]);
$writeMutatorWithoutConstructor = $this->extractor->getWriteInfo(Php81Dummy::class, 'foo', ['enable_constructor_extraction' => false]);

$this->assertSame(PropertyWriteInfo::TYPE_CONSTRUCTOR, $writeMutatorConstructor->getType());
$this->assertSame(PropertyWriteInfo::TYPE_NONE, $writeMutatorWithoutConstructor->getType());
}

/**
* @dataProvider extractConstructorTypesProvider
*/
Expand Down

0 comments on commit ff45ebb

Please sign in to comment.