-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyAccess][PropertyInfo] customize behavior for property hooks on read and write #59246
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
base: 7.3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.3 | ||
--- | ||
|
||
* Allow to customize behavior for property hooks on read and write | ||
|
||
7.0 | ||
--- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\PropertyAccess\Tests\Fixtures; | ||
|
||
class TestClassHooks | ||
{ | ||
public string $hookGetOnly = 'default' { | ||
get => $this->hookGetOnly . ' (hooked on get)'; | ||
} | ||
|
||
public string $hookSetOnly = 'default' { | ||
set(string $value) { | ||
$this->hookSetOnly = $value . ' (hooked on set)'; | ||
} | ||
} | ||
|
||
public string $hookBoth = 'default' { | ||
get => $this->hookBoth . ' (hooked on get)'; | ||
set(string $value) { | ||
$this->hookBoth = $value . ' (hooked on set)'; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,12 +392,12 @@ | |
return new PropertyReadInfo(PropertyReadInfo::TYPE_METHOD, $getsetter, $this->getReadVisibilityForMethod($method), $method->isStatic(), false); | ||
} | ||
|
||
if ($allowMagicGet && $reflClass->hasMethod('__get') && (($r = $reflClass->getMethod('__get'))->getModifiers() & $this->methodReflectionFlags)) { | ||
return new PropertyReadInfo(PropertyReadInfo::TYPE_PROPERTY, $property, PropertyReadInfo::VISIBILITY_PUBLIC, false, $r->returnsReference()); | ||
if ($hasProperty && (($r = $reflClass->getProperty($property))->getModifiers() & $this->propertyReflectionFlags)) { | ||
return new PropertyReadInfo(PropertyReadInfo::TYPE_PROPERTY, $property, $this->getReadVisibilityForProperty($r), $r->isStatic(), true, $this->propertyHasHook($r, 'get'), $this->propertyIsVirtual($r)); | ||
} | ||
|
||
if ($hasProperty && (($r = $reflClass->getProperty($property))->getModifiers() & $this->propertyReflectionFlags)) { | ||
return new PropertyReadInfo(PropertyReadInfo::TYPE_PROPERTY, $property, $this->getReadVisibilityForProperty($r), $r->isStatic(), true); | ||
if ($allowMagicGet && $reflClass->hasMethod('__get') && (($r = $reflClass->getMethod('__get'))->getModifiers() & $this->methodReflectionFlags)) { | ||
return new PropertyReadInfo(PropertyReadInfo::TYPE_PROPERTY, $property, PropertyReadInfo::VISIBILITY_PUBLIC, false, $r->returnsReference(), false, false); | ||
} | ||
Comment on lines
+395
to
401
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See that comment #59246 (comment) |
||
|
||
if ($allowMagicCall && $reflClass->hasMethod('__call') && ($reflClass->getMethod('__call')->getModifiers() & $this->methodReflectionFlags)) { | ||
|
@@ -481,7 +481,7 @@ | |
if ($reflClass->hasProperty($property) && ($reflClass->getProperty($property)->getModifiers() & $this->propertyReflectionFlags)) { | ||
$reflProperty = $reflClass->getProperty($property); | ||
if (!$reflProperty->isReadOnly()) { | ||
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY, $property, $this->getWriteVisibilityForProperty($reflProperty), $reflProperty->isStatic()); | ||
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY, $property, $this->getWriteVisibilityForProperty($reflProperty), $reflProperty->isStatic(), $this->propertyHasHook($reflProperty, 'set')); | ||
} | ||
|
||
$errors[] = [\sprintf('The property "%s" in class "%s" is a promoted readonly property.', $property, $reflClass->getName())]; | ||
|
@@ -491,7 +491,7 @@ | |
if ($allowMagicSet) { | ||
[$accessible, $methodAccessibleErrors] = $this->isMethodAccessible($reflClass, '__set', 2); | ||
if ($accessible) { | ||
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY, $property, PropertyWriteInfo::VISIBILITY_PUBLIC, false); | ||
return new PropertyWriteInfo(PropertyWriteInfo::TYPE_PROPERTY, $property, PropertyWriteInfo::VISIBILITY_PUBLIC, false, false); | ||
mtarld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
$errors[] = $methodAccessibleErrors; | ||
|
@@ -894,6 +894,16 @@ | |
return [false, $errors]; | ||
} | ||
|
||
private function propertyHasHook(\ReflectionProperty $property, string $hookType): bool | ||
{ | ||
return \PHP_VERSION_ID >= 80400 && $property->hasHook(\PropertyHookType::from($hookType)); | ||
} | ||
|
||
private function propertyIsVirtual(\ReflectionProperty $property): bool | ||
{ | ||
return \PHP_VERSION_ID >= 80400 && $property->isVirtual(); | ||
} | ||
|
||
/** | ||
* Camelizes a given string. | ||
*/ | ||
|
@@ -975,7 +985,7 @@ | |
private function getWriteVisibilityForProperty(\ReflectionProperty $reflectionProperty): string | ||
{ | ||
if (\PHP_VERSION_ID >= 80400) { | ||
if ($reflectionProperty->isVirtual() && !$reflectionProperty->hasHook(\PropertyHookType::Set)) { | ||
Check failure on line 988 in src/Symfony/Component/PropertyInfo/Extractor/ReflectionExtractor.php
|
||
return PropertyWriteInfo::VISIBILITY_PRIVATE; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?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 HookedProperties | ||
{ | ||
public string $hookGetOnly { | ||
get => $this->hookGetOnly . ' (hooked on get)'; | ||
} | ||
public string $hookSetOnly { | ||
set(string $value) { | ||
$this->hookSetOnly = $value . ' (hooked on set)'; | ||
} | ||
} | ||
public string $hookBoth { | ||
get => $this->hookBoth . ' (hooked on get)'; | ||
set(string $value) { | ||
$this->hookBoth = $value . ' (hooked on set)'; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,6 @@ | |
class VirtualProperties | ||
{ | ||
public bool $virtualNoSetHook { get => true; } | ||
public bool $virtualSetHookOnly { set => $value; } | ||
public bool $virtualHook { get => true; set => $value; } | ||
public bool $virtualSetHookOnly { set (bool $value) { } } | ||
public bool $virtualHook { get => true; set (bool $value) { } } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated this fixtures because PHP does not report these properties as Virtual, see reproducer |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constants for a bit mask are generally defined in increasing order (so that new cases are added at the end of the list). This reverse order is unusual to me