8000 bug #47763 [PropertyInfo] a readonly property must not be reported as… · enumag/symfony@25c0a20 · GitHub
[go: up one dir, main page]

Skip to content

Commit 25c0a20

Browse files
committed
bug symfony#47763 [PropertyInfo] a readonly property must not be reported as being writable (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [PropertyInfo] a readonly property must not be reported as being writable | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#47756 | License | MIT | Doc PR | Commits ------- f06b842 a readonly property must not be reported as being writable
2 parents 4d70aa9 + f06b842 commit 25c0a20

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function isReadable($class, $property, array $context = []): ?bool
180180
*/
181181
public function isWritable($class, $property, array $context = []): ?bool
182182
{
183-
if ($this->isAllowedProperty($class, $property)) {
183+
if ($this->isAllowedProperty($class, $property, true)) {
184184
return true;
185185
}
186186

@@ -389,11 +389,15 @@ private function isNullableProperty(string $class, string $property): bool
389389
return false;
390390
}
391391

392-
private function isAllowedProperty(string $class, string $property): bool
392+
private function isAllowedProperty(string $class, string $property, bool $writeAccessRequired = false): bool
393393
{
394394
try {
395395
$reflectionProperty = new \ReflectionProperty($class, $property);
396396

397+
if (\PHP_VERSION_ID >= 80100 && $writeAccessRequired && $reflectionProperty->isReadOnly()) {
398+
return false;
399+
}
400+
397401
if ($this->accessFlags & self::ALLOW_PUBLIC && $reflectionProperty->isPublic()) {
398402
return true;
399403
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php74Dummy;
2323
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php7Dummy;
2424
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php7ParentDummy;
25+
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php81Dummy;
2526
use Symfony\Component\PropertyInfo\Type;
2627

2728
/**
@@ -234,6 +235,7 @@ public function php71TypesProvider()
234235

235236
/**
236237
* @dataProvider php80TypesProvider
238+
*
237239
* @requires PHP 8
238240
*/
239241
public function testExtractPhp80Type($property, array $type = null)
@@ -257,6 +259,7 @@ public function php80TypesProvider()
257259

258260
/**
259261
* @dataProvider php81TypesProvider
262+
*
260263
* @requires PHP 8.1
261264
*/
262265
public function testExtractPhp81Type($property, array $type = null)
@@ -272,8 +275,17 @@ public function php81TypesProvider()
272275
];
273276
}
274277

278+
/**
279+
* @requires PHP 8.1
280+
*/
281+
public function testReadonlyPropertiesAreNotWriteable()
282+
{
283+
$this->assertFalse($this->extractor->isWritable(Php81Dummy::class, 'foo'));
284+
}
285+
275286
/**
276287
* @dataProvider php82TypesProvider
288+
*
277289
* @requires PHP 8.2
278290
*/
279291
public function testExtractPhp82Type($property, array $type = null)

src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php81Dummy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
class Php81Dummy
1515
{
16+
public function __construct(public readonly string $foo)
17+
{
18+
}
19+
1620
public function getNothing(): never
1721
{
1822
throw new \Exception('Oops');

0 commit comments

Comments
 (0)
0