Skip to content

[PropertyAccess] Fix compatibility with PHP 8.4 asymmetric visibility #59221

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
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
11 changes: 9 additions & 2 deletions src/Symfony/Component/PropertyAccess/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,22 @@ private function getWriteInfo(string $class, string $property, mixed $value): Pr
*/
private function isPropertyWritable(object $object, string $property): bool
{
if ($object instanceof \stdClass && property_exists($object, $property)) {
return true;
}

$mutatorForArray = $this->getWriteInfo($object::class, $property, []);
if (PropertyWriteInfo::TYPE_PROPERTY === $mutatorForArray->getType()) {
return $mutatorForArray->getVisibility() === 'public';
}

if (PropertyWriteInfo::TYPE_NONE !== $mutatorForArray->getType() || ($object instanceof \stdClass && property_exists($object, $property))) {
if (PropertyWriteInfo::TYPE_NONE !== $mutatorForArray->getType()) {
return true;
}

$mutator = $this->getWriteInfo($object::class, $property, '');

return PropertyWriteInfo::TYPE_NONE !== $mutator->getType() || ($object instanceof \stdClass && property_exists($object, $property));
return PropertyWriteInfo::TYPE_NONE !== $mutator->getType();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\PropertyAccess\Tests\Fixtures;

class AsymmetricVisibility
{
public public(set) mixed $publicPublic = null;
public protected(set) mixed $publicProtected = null;
public private(set) mixed $publicPrivate = null;
private private(set) mixed $privatePrivate = null;
public bool $virtualNoSetHook { get => true; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyAccess\Tests\Fixtures\AsymmetricVisibility;
use Symfony\Component\PropertyAccess\Tests\Fixtures\ExtendedUninitializedProperty;
use Symfony\Component\PropertyAccess\Tests\Fixtures\ReturnTyped;
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestAdderRemoverInvalidArgumentLength;
Expand Down Expand Up @@ -1023,4 +1024,62 @@ private function createUninitializedObjectPropertyGhost(): UninitializedObjectPr
return $class::createLazyGhost(initializer: function ($instance) {
});
}

/**
* @requires PHP 8.4
*/
public function testIsWritableWithAsymmetricVisibility()
{
$object = new AsymmetricVisibility();

$this->assertTrue($this->propertyAccessor->isWritable($object, 'publicPublic'));
$this->assertFalse($this->propertyAccessor->isWritable($object, 'publicProtected'));
$this->assertFalse($this->propertyAccessor->isWritable($object, 'publicPrivate'));
$this->assertFalse($this->propertyAccessor->isWritable($object, 'privatePrivate'));
$this->assertFalse($this->propertyAccessor->isWritable($object, 'virtualNoSetHook'));
}

/**
* @requires PHP 8.4
*/
public function testIsReadableWithAsymmetricVisibility()
{
$object = new AsymmetricVisibility();

$this->assertTrue($this->propertyAccessor->isReadable($object, 'publicPublic'));
$this->assertTrue($this->propertyAccessor->isReadable($object, 'publicProtected'));
$this->assertTrue($this->propertyAccessor->isReadable($object, 'publicPrivate'));
$this->assertFalse($this->propertyAccessor->isReadable($object, 'privatePrivate'));
$this->assertTrue($this->propertyAccessor->isReadable($object, 'virtualNoSetHook'));
}

/**
* @requires PHP 8.4
*
* @dataProvider setValueWithAsymmetricVisibilityDataProvider
*/
public function testSetValueWithAsymmetricVisibility(string $propertyPath, ?string $expectedException)
{
$object = new AsymmetricVisibility();

if ($expectedException) {
$this->expectException($expectedException);
} else {
$this->expectNotToPerformAssertions();
}

$this->propertyAccessor->setValue($object, $propertyPath, true);
}

/**
* @return iterable<array{0: string, 1: null|class-string}>
*/
public static function setValueWithAsymmetricVisibilityDataProvider(): iterable
{
yield ['publicPublic', null];
yield ['publicProtected', \Error::class];
yield ['publicPrivate', \Error::class];
yield ['privatePrivate', NoSuchPropertyException::class];
yield ['virtualNoSetHook', \Error::class];
}
}
Loading