Skip to content
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

Rely on reflection to access null properties #4425

Merged
merged 1 commit into from
Nov 7, 2024
Merged
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
51 changes: 45 additions & 6 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ public static function getAttribute(Environment $env, Source $source, $object, $

// array
if (Template::METHOD_CALL !== $type) {
$arrayItem = \is_bool($item) || \is_float($item) ? (int) $item : $item;
$arrayItem = \is_bool($item) || \is_float($item) ? (int) $item : $item = (string) $item;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string casting breaks my ArrayAccess object using objects as keys, when using myCollection[object] in twig.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arf. Can you please submit a fix as a PR together with a test case?


if ($sandboxed && $object instanceof \ArrayAccess && !\in_array($object::class, self::ARRAY_LIKE_CLASSES, true)) {
try {
Expand All @@ -1652,9 +1652,11 @@ public static function getAttribute(Environment $env, Source $source, $object, $
}
}

if (((\is_array($object) || $object instanceof \ArrayObject) && (isset($object[$arrayItem]) || \array_key_exists($arrayItem, (array) $object)))
|| ($object instanceof \ArrayAccess && isset($object[$arrayItem]))
) {
if (match (true) {
\is_array($object) => \array_key_exists($arrayItem, $object),
$object instanceof \ArrayAccess => $object->offsetExists($arrayItem),
default => false,
}) {
if ($isDefinedTest) {
return true;
}
Expand Down Expand Up @@ -1697,6 +1699,8 @@ public static function getAttribute(Environment $env, Source $source, $object, $
}
}

$item = (string) $item;

if (!\is_object($object)) {
if ($isDefinedTest) {
return false;
Expand Down Expand Up @@ -1731,12 +1735,24 @@ public static function getAttribute(Environment $env, Source $source, $object, $
}
}

if (isset($object->$item) || \array_key_exists((string) $item, (array) $object)) {
static $propertyCheckers = [];

if (isset($object->$item)
|| ($propertyCheckers[$object::class][$item] ??= self::getPropertyChecker($object::class, $item))($object, $item)
) {
if ($isDefinedTest) {
return true;
}

return isset($object->$item) ? $object->$item : ((array) $object)[(string) $item];
return $object->$item;
}

if ($object instanceof \DateTimeInterface && \in_array($item, ['date', 'timezone', 'timezone_type'], true)) {
if ($isDefinedTest) {
return true;
}

return ((array) $object)[$item];
}

if (\defined($object::class.'::'.$item)) {
Expand Down Expand Up @@ -2099,4 +2115,27 @@ public static function parseAttributeFunction(Parser $parser, Node $fakeNode, $a

return new GetAttrExpression($args[0], $args[1], $args[2] ?? null, Template::ANY_CALL, $line);
}

private static function getPropertyChecker(string $class, string $property): \Closure
{
static $classReflectors = [];

$class = $classReflectors[$class] ??= new \ReflectionClass($class);

if (!$class->hasProperty($property)) {
static $propertyExists;

return $propertyExists ??= \Closure::fromCallable('property_exists');
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
}

$property = $class->getProperty($property);

if (!$property->isPublic()) {
static $false;

return $false ??= static fn () => false;
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
}

return static fn ($object) => $property->isInitialized($object);
}
}
Loading