Skip to content

Commit

Permalink
Merge pull request #3280 from samsonasik/file-locator-optimize
Browse files Browse the repository at this point in the history
optimize FileLocator autoloader : use foreach instead of for with count when possible
  • Loading branch information
michalsn authored Jul 12, 2020
2 parents 5c5bc1a + 5841dce commit 0e6eac7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,34 +171,38 @@ public function getClassname(string $file) : string
{
$php = file_get_contents($file);
$tokens = token_get_all($php);
$count = count($tokens);
$dlm = false;
$namespace = '';
$class_name = '';

for ($i = 2; $i < $count; $i++)
foreach ($tokens as $i => $token)
{
if ((isset($tokens[$i - 2][1]) && ($tokens[$i - 2][1] === 'phpnamespace' || $tokens[$i - 2][1] === 'namespace')) || ($dlm && $tokens[$i - 1][0] === T_NS_SEPARATOR && $tokens[$i][0] === T_STRING))
if ($i < 2)
{
continue;
}

if ((isset($tokens[$i - 2][1]) && ($tokens[$i - 2][1] === 'phpnamespace' || $tokens[$i - 2][1] === 'namespace')) || ($dlm && $tokens[$i - 1][0] === T_NS_SEPARATOR && $token[0] === T_STRING))
{
if (! $dlm)
{
$namespace = 0;
}
if (isset($tokens[$i][1]))
if (isset($token[1]))
{
$namespace = $namespace ? $namespace . '\\' . $tokens[$i][1] : $tokens[$i][1];
$namespace = $namespace ? $namespace . '\\' . $token[1] : $token[1];
$dlm = true;
}
}
elseif ($dlm && ($tokens[$i][0] !== T_NS_SEPARATOR) && ($tokens[$i][0] !== T_STRING))
elseif ($dlm && ($token[0] !== T_NS_SEPARATOR) && ($token[0] !== T_STRING))
{
$dlm = false;
}
if (($tokens[$i - 2][0] === T_CLASS || (isset($tokens[$i - 2][1]) && $tokens[$i - 2][1] === 'phpclass'))
&& $tokens[$i - 1][0] === T_WHITESPACE
&& $tokens[$i][0] === T_STRING)
&& $token[0] === T_STRING)
{
$class_name = $tokens[$i][1];
$class_name = $token[1];
break;
}
}
Expand Down

0 comments on commit 0e6eac7

Please sign in to comment.