Skip to content

Commit f8d742e

Browse files
committed
CoreExtension::getAttribute: small improvement regarding getter/isser/hasser
1 parent 115114b commit f8d742e

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/Extension/CoreExtension.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,23 +1830,26 @@ public static function getAttribute(Environment $env, Source $source, $object, $
18301830
$classCache[$lcName = $lcMethods[$i]] = $method;
18311831

18321832
if ('g' === $lcName[0] && str_starts_with($lcName, 'get')) {
1833-
$name = substr($method, 3);
1834-
$lcName = substr($lcName, 3);
1833+
$prefixLength = 3;
1834+
$lcName = substr($lcName, $prefixLength);
18351835
} elseif ('i' === $lcName[0] && str_starts_with($lcName, 'is')) {
1836-
$name = substr($method, 2);
1837-
$lcName = substr($lcName, 2);
1836+
$prefixLength = 2;
1837+
$lcName = substr($lcName, $prefixLength);
18381838
} elseif ('h' === $lcName[0] && str_starts_with($lcName, 'has')) {
1839-
$name = substr($method, 3);
1840-
$lcName = substr($lcName, 3);
1839+
$prefixLength = 3;
1840+
$lcName = substr($lcName, $prefixLength);
18411841
if (\in_array('is'.$lcName, $lcMethods, true)) {
18421842
continue;
18431843
}
18441844
} else {
18451845
continue;
18461846
}
18471847

1848-
// skip get() and is() methods (in which case, $name is empty)
1849-
if ($name) {
1848+
// skip get(), is() and has() methods (in which case, $lcName is empty)
1849+
if ($lcName) {
1850+
// camelCase name (e.g. getFooBar() -> fooBar)
1851+
$name = $lcName[0].substr($method, $prefixLength + 1);
1852+
18501853
if (!isset($classCache[$name])) {
18511854
$classCache[$name] = $method;
18521855
}

0 commit comments

Comments
 (0)