Skip to content

Commit

Permalink
bug #47763 [PropertyInfo] a readonly property must not be reported as…
Browse files Browse the repository at this point in the history
… 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 #47756
| License       | MIT
| Doc PR        |

Commits
-------

f06b842 a readonly property must not be reported as being writable
  • Loading branch information
fabpot committed Oct 2, 2022
2 parents 4d70aa9 + f06b842 commit 25c0a20
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Expand Up @@ -180,7 +180,7 @@ public function isReadable($class, $property, array $context = []): ?bool
*/
public function isWritable($class, $property, array $context = []): ?bool
{
if ($this->isAllowedProperty($class, $property)) {
if ($this->isAllowedProperty($class, $property, true)) {
return true;
}

Expand Down Expand Up @@ -389,11 +389,15 @@ private function isNullableProperty(string $class, string $property): bool
return false;
}

private function isAllowedProperty(string $class, string $property): bool
private function isAllowedProperty(string $class, string $property, bool $writeAccessRequired = false): bool
{
try {
$reflectionProperty = new \ReflectionProperty($class, $property);

if (\PHP_VERSION_ID >= 80100 && $writeAccessRequired && $reflectionProperty->isReadOnly()) {
return false;
}

if ($this->accessFlags & self::ALLOW_PUBLIC && $reflectionProperty->isPublic()) {
return true;
}
Expand Down
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php74Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php7Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php7ParentDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php81Dummy;
use Symfony\Component\PropertyInfo\Type;

/**
Expand Down Expand Up @@ -234,6 +235,7 @@ public function php71TypesProvider()

/**
* @dataProvider php80TypesProvider
*
* @requires PHP 8
*/
public function testExtractPhp80Type($property, array $type = null)
Expand All @@ -257,6 +259,7 @@ public function php80TypesProvider()

/**
* @dataProvider php81TypesProvider
*
* @requires PHP 8.1
*/
public function testExtractPhp81Type($property, array $type = null)
Expand All @@ -272,8 +275,17 @@ public function php81TypesProvider()
];
}

/**
* @requires PHP 8.1
*/
public function testReadonlyPropertiesAreNotWriteable()
{
$this->assertFalse($this->extractor->isWritable(Php81Dummy::class, 'foo'));
}

/**
* @dataProvider php82TypesProvider
*
* @requires PHP 8.2
*/
public function testExtractPhp82Type($property, array $type = null)
Expand Down
Expand Up @@ -13,6 +13,10 @@

class Php81Dummy
{
public function __construct(public readonly string $foo)
{
}

public function getNothing(): never
{
throw new \Exception('Oops');
Expand Down

0 comments on commit 25c0a20

Please sign in to comment.