diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index f7e4250ae50..f78d4deb17c 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1830,14 +1830,14 @@ public static function getAttribute(Environment $env, Source $source, $object, $ $classCache[$lcName = $lcMethods[$i]] = $method; if ('g' === $lcName[0] && str_starts_with($lcName, 'get')) { - $name = substr($method, 3); - $lcName = substr($lcName, 3); + $prefixLength = 3; + $lcName = substr($lcName, $prefixLength); } elseif ('i' === $lcName[0] && str_starts_with($lcName, 'is')) { - $name = substr($method, 2); - $lcName = substr($lcName, 2); + $prefixLength = 2; + $lcName = substr($lcName, $prefixLength); } elseif ('h' === $lcName[0] && str_starts_with($lcName, 'has')) { - $name = substr($method, 3); - $lcName = substr($lcName, 3); + $prefixLength = 3; + $lcName = substr($lcName, $prefixLength); if (\in_array('is'.$lcName, $lcMethods, true)) { continue; } @@ -1845,8 +1845,11 @@ public static function getAttribute(Environment $env, Source $source, $object, $ continue; } - // skip get() and is() methods (in which case, $name is empty) - if ($name) { + // skip get(), is() and has() methods (in which case, $lcName is empty) + if ($lcName) { + // camelCase name (e.g. getFooBar() -> fooBar) + $name = $lcName[0].substr($method, $prefixLength + 1); + if (!isset($classCache[$name])) { $classCache[$name] = $method; }