10000 [PropertyInfo] Check if the virtual property has a setter in `isAllowedProperty` by pan93412 · Pull Request #58962 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] Check if the virtual property has a setter in isAllowedProperty #58962

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Check if the virtual property has a setter in isAllowedProperty
A virtual property without setter cannot be written.
  • Loading branch information
pan93412 committed Nov 25, 2024
commit b482f1b6fa7aa9621bb79838876f84966b9860a6
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,17 @@ private function isAllowedProperty(string $class, string $property, bool $writeA
return false;
}

if (
$writeAccessRequired
&& method_exists($reflectionProperty, 'isVirtual')
&& method_exists($reflectionProperty, 'hasHook')
&& enum_exists('PropertyHookType')
&& $reflectionProperty->isVirtual()
&& !$reflectionProperty->hasHook(\PropertyHookType::Set)
) {
return false;
}

return (bool) ($reflectionProperty->getModifiers() & $this->propertyReflectionFlags);
} catch (\ReflectionException $e) {
// Return false if the property doesn't exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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\Tests\Fixtures\VirtualProperties;
use Symfony\Component\PropertyInfo\Type;

/**
Expand Down Expand Up @@ -699,4 +700,17 @@ public function testAsymmetricVisibility()
$this->assertFalse($this->extractor->isWritable(AsymmetricVisibility::class, 'publicProtected'));
$this->assertFalse($this->extractor->isWritable(AsymmetricVisibility::class, 'protectedPrivate'));
}

/**
* @requires PHP 8.4
*/
public function testVirtualProperties()
{
$this->assertTrue($this->extractor->isReadable(VirtualProperties::class, 'virtualNoSetHook'));
$this->assertTrue($this->extractor->isReadable(VirtualProperties::class, 'virtualSetHookOnly'));
$this->assertTrue($this->extractor->isReadable(VirtualProperties::class, 'virtualHook'));
$this->assertFalse($this->extractor->isWritable(VirtualProperties::class, 'virtualNoSetHook'));
$this->assertTrue($this->extractor->isWritable(VirtualProperties::class, 'virtualSetHookOnly'));
$this->assertTrue($this->extractor->isWritable(VirtualProperties::class, 'virtualHook'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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\PropertyInfo\Tests\Fixtures;

class VirtualProperties
{
public bool $virtualNoSetHook { get => true; }
public bool $virtualSetHookOnly { set => $value; }
public bool $virtualHook { get => true; set => $value; }
}
0