Skip to content

Commit

Permalink
Rely on reflection to access null properties
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Oct 31, 2024
1 parent 494f010 commit d8f39e6
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ final class CoreExtension extends AbstractExtension
private $numberFormat = [0, '.', ','];
private $timezone = null;

private static $propertyCheckers = [];

/**
* Sets the default format to be used by the date filter.
*
Expand Down Expand Up @@ -1626,9 +1628,11 @@ public static function getAttribute(Environment $env, Source $source, $object, $
if (Template::METHOD_CALL !== $type) {
$arrayItem = \is_bool($item) || \is_float($item) ? (int) $item : $item;

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,7 +1701,21 @@ public static function getAttribute(Environment $env, Source $source, $object, $

// object property
if (Template::METHOD_CALL !== $type) {
if (isset($object->$item) || \array_key_exists((string) $item, (array) $object)) {
if (isset($object->$item)
|| (self::$propertyCheckers[$object::class][$item] ??= self::getPropertyChecker($object::class, $item))($object, $item)
) {
if ($isDefinedTest) {
return true;
}

if ($sandboxed) {
$env->getExtension(SandboxExtension::class)->checkPropertyAllowed($object, $item, $lineno, $source);
}

return $object->$item;
}

if ($object instanceof \DateTimeInterface && \in_array($item, ['date', 'timezone', 'timezone_type'], true)) {
if ($isDefinedTest) {
return true;
}
Expand All @@ -1706,7 +1724,7 @@ public static function getAttribute(Environment $env, Source $source, $object, $
$env->getExtension(SandboxExtension::class)->checkPropertyAllowed($object, $item, $lineno, $source);
}

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

if (\defined($object::class.'::'.$item)) {
Expand Down Expand Up @@ -2055,4 +2073,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 ??= property_exists(...);
}

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

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

return $false ??= static fn () => false;
}

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

0 comments on commit d8f39e6

Please sign in to comment.