Skip to content

[PropertyInfo] a readonly property must not be reported as being writable #47763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
a readonly property must not be reported as being writable
  • Loading branch information
xabbuh committed Oct 1, 2022
commit f06b842724ceba4b511d1446b8ab7156fade47d6
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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