8000 bug #48108 [PropertyAccess] Readonly properties must have no Property… · enumag/symfony@af5b340 · GitHub
[go: up one dir, main page]

Skip to content

Commit af5b340

Browse files
bug symfony#48108 [PropertyAccess] Readonly properties must have no PropertyWriteInfo (CasvanDongen)
This PR was merged into the 5.4 branch. Discussion ---------- [PropertyAccess] Readonly properties must have no PropertyWriteInfo | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - The reported WriteInfo of readonly promoted properties is incorrectly returned as a writeable property when constructor extraction is disabled. This PR fixes that by correctly returning `PropertyWriteInfo::TYPE_NONE` when `enable_constructor_extraction` is `false`. Obviously, this fix only applies to PHP8.1 or higher. Commits ------- 2aa6c7d [PropertyAccess] Readonly properties must have no PropertyWriteInfo
2 parents 887c7b0 + 2aa6c7d commit af5b340

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,12 @@ public function getWriteInfo(string $class, string $property, array $context = [
397397

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

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

404408
if ($allowMagicSet) {

src/Symfony/Component/PropertyInfo/Tests/Extractor/ReflectionExtractorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,18 @@ public function testGetWriteInfoDeprecatedEnableMagicCallExtractionInContext()
642642
]);
643643
}
644644

645+
/**
646+
* @requires PHP 8.1
647+
*/
648+
public function testGetWriteInfoReadonlyProperties()
649+
{
650+
$writeMutatorConstructor = $this->extractor->getWriteInfo(Php81Dummy::class, 'foo', ['enable_constructor_extraction' => true]);
651+
$writeMutatorWithoutConstructor = $this->extractor->getWriteInfo(Php81Dummy::class, 'foo', ['enable_constructor_extraction' => false]);
652+
653+
$this->assertSame(PropertyWriteInfo::TYPE_CONSTRUCTOR, $writeMutatorConstructor->getType());
654+
$this->assertSame(PropertyWriteInfo::TYPE_NONE, $writeMutatorWithoutConstructor->getType());
655+
}
656+
645657
/**
646658
* @dataProvider extractConstructorTypesProvider
647659
*/

0 commit comments

Comments
 (0)
0