-
-
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.4
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 | ||||||
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.
Suggested change
|
||||||
|
||||||
7.0 | ||||||
--- | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -47,6 +47,10 @@ | |||||||||
/** @var int Allow magic __call methods */ | ||||||||||
public const MAGIC_CALL = ReflectionExtractor::ALLOW_MAGIC_CALL; | ||||||||||
|
||||||||||
public const BYPASS_PROPERTY_HOOK_NONE = 0; | ||||||||||
public const BYPASS_PROPERTY_HOOK_WRITE = 1 << 0; | ||||||||||
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. 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 |
||||||||||
public const BYPASS_PROPERTY_HOOK_READ = 1 << 1; | ||||||||||
|
||||||||||
public const DO_NOT_THROW = 0; | ||||||||||
public const THROW_ON_INVALID_INDEX = 1; | ||||||||||
public const THROW_ON_INVALID_PROPERTY_PATH = 2; | ||||||||||
|
@@ -67,6 +71,7 @@ | |||||||||
private PropertyWriteInfoExtractorInterface $writeInfoExtractor; | ||||||||||
private array $readPropertyCache = []; | ||||||||||
private array $writePropertyCache = []; | ||||||||||
private int $bypassPropertyHooks; | ||||||||||
|
||||||||||
/** | ||||||||||
* Should not be used by application code. Use | ||||||||||
|
@@ -77,19 +82,24 @@ | |||||||||
* or self::DISALLOW_MAGIC_METHODS for none | ||||||||||
* @param int $throw A bitwise combination of the THROW_* constants | ||||||||||
* to specify when exceptions should be thrown | ||||||||||
* @param int-mask-of<self::BYPASS_PROPERTY_HOOK_*> $bypassPropertyHooks A bitwise combination of the BYPASS_PROPERTY_HOOK_* constants | ||||||||||
* to specify the hooks you want to bypass, | ||||||||||
* or self::BYPASS_PROPERTY_HOOK_NONE for none | ||||||||||
Comment on lines
+85
to
+87
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. looks like the previous arguments could also nicely benefit from similar int-mask-of the text looks redundant to me:
Suggested change
|
||||||||||
*/ | ||||||||||
public function __construct( | ||||||||||
private int $magicMethodsFlags = self::MAGIC_GET | self::MAGIC_SET, | ||||||||||
int $throw = self::THROW_ON_INVALID_PROPERTY_PATH, | ||||||||||
?CacheItemPoolInterface $cacheItemPool = null, | ||||||||||
?PropertyReadInfoExtractorInterface $readInfoExtractor = null, | ||||||||||
?PropertyWriteInfoExtractorInterface $writeInfoExtractor = null, | ||||||||||
int $bypassPropertyHooks = self::BYPASS_PROPERTY_HOOK_NONE, | ||||||||||
) { | ||||||||||
$this->ignoreInvalidIndices = 0 === ($throw & self::THROW_ON_INVALID_INDEX); | ||||||||||
$this->cacheItemPool = $cacheItemPool instanceof NullAdapter ? null : $cacheItemPool; // Replace the NullAdapter by the null value | ||||||||||
$this->ignoreInvalidProperty = 0 === ($throw & self::THROW_ON_INVALID_PROPERTY_PATH); | ||||||||||
$this->readInfoExtractor = $readInfoExtractor ?? new ReflectionExtractor([], null, null, false); | ||||||||||
$this->writeInfoExtractor = $writeInfoExtractor ?? new ReflectionExtractor(['set'], null, null, false); | ||||||||||
$this->bypassPropertyHooks = \PHP_VERSION_ID >= 80400 ? $bypassPropertyHooks : self::BYPASS_PROPERTY_HOOK_NONE; | ||||||||||
} | ||||||||||
|
||||||||||
public function getValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): mixed | ||||||||||
|
@@ -414,21 +424,30 @@ | |||||||||
throw $e; | ||||||||||
} | ||||||||||
} elseif (PropertyReadInfo::TYPE_PROPERTY === $type) { | ||||||||||
if (!isset($object->$name) && !\array_key_exists($name, (array) $object)) { | ||||||||||
$valueSet = false; | ||||||||||
$bypassHooks = $this->bypassPropertyHooks & self::BYPASS_PROPERTY_HOOK_READ && $access->hasHook() && !$access->isVirtual(); | ||||||||||
$initialValueNotSet = !isset($object->$name) && !\array_key_exists($name, (array) $object); | ||||||||||
if ($initialValueNotSet || $bypassHooks) { | ||||||||||
try { | ||||||||||
$r = new \ReflectionProperty($class, $name); | ||||||||||
|
||||||||||
if ($r->isPublic() && !$r->hasType()) { | ||||||||||
if ($initialValueNotSet && $r->isPublic() && !$r->hasType()) { | ||||||||||
throw new UninitializedPropertyException(\sprintf('The property "%s::$%s" is not initialized.', $class, $name)); | ||||||||||
} | ||||||||||
|
||||||||||
if ($bypassHooks) { | ||||||||||
$result[self::VALUE] = $r->getRawValue($object); | ||||||||||
Check failure on line 438 in src/Symfony/Component/PropertyAccess/PropertyAccessor.php
|
||||||||||
xavierleune marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
$valueSet = true; | ||||||||||
} | ||||||||||
} catch (\ReflectionException $e) { | ||||||||||
if (!$ignoreInvalidProperty) { | ||||||||||
throw new NoSuchPropertyException(\sprintf('Can\'t get a way to read the property "%s" in class "%s".', $property, $class)); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
$result[self::VALUE] = $object->$name; | ||||||||||
if (!$valueSet) { | ||||||||||
$result[self::VALUE] = $object->$name; | ||||||||||
} | ||||||||||
|
||||||||||
if (isset($zval[self::REF]) && $access->canBeReference()) { | ||||||||||
$result[self::REF] = &$object->$name; | ||||||||||
|
@@ -531,7 +550,12 @@ | |||||||||
if (PropertyWriteInfo::TYPE_METHOD === $type) { | ||||||||||
$object->{$mutator->getName()}($value); | ||||||||||
} elseif (PropertyWriteInfo::TYPE_PROPERTY === $type) { | ||||||||||
$object->{$mutator->getName()} = $value; | ||||||||||
if ($this->bypassPropertyHooks & self::BYPASS_PROPERTY_HOOK_WRITE) { | ||||||||||
$r = new \ReflectionProperty($class, $mutator->getName()); | ||||||||||
$r->setRawValue($object, $value); | ||||||||||
Check failure on line 555 in src/Symfony/Component/PropertyAccess/PropertyAccessor.php
|
||||||||||
xavierleune marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
} else { | ||||||||||
$object->{$mutator->getName()} = $value; | ||||||||||
} | ||||||||||
} elseif (PropertyWriteInfo::TYPE_ADDER_AND_REMOVER === $type) { | ||||||||||
$this->writeCollection($zval, $property, $value, $mutator->getAdderInfo(), $mutator->getRemoverInfo()); | ||||||||||
} | ||||||||||
|
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 | ||||
---|---|---|---|---|---|---|
|
@@ -17,7 +17,7 @@ | |||||
], | ||||||
"require": { | ||||||
"php": ">=8.2", | ||||||
"symfony/property-info": "^6.4|^7.0" | ||||||
"symfony/property-info": "^7.3" | ||||||
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.
Suggested change
|
||||||
}, | ||||||
"require-dev": { | ||||||
"symfony/cache": "^6.4|^7.0" | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ CHANGELOG | |||||
7.3 | ||||||
--- | ||||||
|
||||||
* Gather data from property hooks in ReflectionExtractor | ||||||
xavierleune marked this conversation as resolved.
Show resolved
Hide resolved
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.
Suggested change
|
||||||
* Add support for `non-positive-int`, `non-negative-int` and `non-zero-int` PHPStan types to `PhpStanExtractor` | ||||||
* Add `PropertyDescriptionExtractorInterface` to `PhpStanExtractor` | ||||||
* Deprecate the `Type` class, use `Symfony\Component\TypeInfo\Type` class from `symfony/type-info` instead | ||||||
|
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)); | ||
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'd prefer inlining these two methods: they'd look needles to me on 8.0 |
||
} | ||
|
||
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 <[email protected]> | ||
* | ||
* 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.