Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef7876e

Browse files
committed
speed up accessing object properties
1 parent 1aa652e commit ef7876e

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,16 @@ public function __construct(bool $magicCall = false, bool $throwExceptionOnInval
8282
*/
8383
public function getValue($objectOrArray, $propertyPath)
8484
{
85-
$propertyPath = $this->getPropertyPath($propertyPath);
86-
8785
$zval = [
8886
self::VALUE => $objectOrArray,
8987
];
88+
89+
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
90+
return $this->readProperty($zval, $propertyPath)[self::VALUE];
91+
}
92+
93+
$propertyPath = $this->getPropertyPath($propertyPath);
94+
9095
$propertyValues = $this->readPropertiesUntil($zval, $propertyPath, $propertyPath->getLength(), $this->ignoreInvalidIndices);
9196

9297
return $propertyValues[\count($propertyValues) - 1][self::VALUE];
@@ -97,6 +102,22 @@ public function getValue($objectOrArray, $propertyPath)
97102
*/
98103
public function setValue(&$objectOrArray, $propertyPath, $value)
99104
{
105+
if (\is_object($objectOrArray) && false === strpbrk((string) $propertyPath, '.[')) {
106+
$zval = [
107+
self::VALUE => $objectOrArray,
108+
];
109+
110+
try {
111+
$this->writeProperty($zval, $propertyPath, $value);
112+
113+
return;
114+
} catch (\TypeError $e) {
115+
self::throwInvalidArgumentException($e->getMessage(), $e->getTrace(), 0, $propertyPath);
116+
// It wasn't thrown in this class so rethrow it
117+
throw $e;
118+
}
119+
}
120+
100121
$propertyPath = $this->getPropertyPath($propertyPath);
101122

102123
$zval = [

0 commit comments

Comments
 (0)