Skip to content

Commit

Permalink
early return
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Mar 14, 2023
1 parent 537a740 commit f5eb5a8
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/Driver/AbstractClassDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,41 @@ protected function loadClassFromFile(string $mappingFile): ?string
$content = file_get_contents($mappingFile);
$tokens = token_get_all($content !== false ? $content : '');

$class = '';

$next = $this->findNextToken($tokens, [\T_NAMESPACE]);
if ($next !== null) {
$validTokenTypes = $this->getValidTokenTypes();
if ($next === null) {
return null;
}

while (
\array_key_exists($next + 1, $tokens)
&& \is_array($tokens[$next + 1])
&& \in_array($tokens[$next + 1][0], $validTokenTypes, true)
) {
$class .= trim($tokens[$next + 1][1]);
$validTokenTypes = $this->getValidTokenTypes();
$class = '';

++$next;
}
while (
\array_key_exists($next + 1, $tokens)
&& \is_array($tokens[$next + 1])
&& \in_array($tokens[$next + 1][0], $validTokenTypes, true)
) {
$class .= trim($tokens[$next + 1][1]);

// Exclude traits and interfaces
if ($this->findNextToken($tokens, [\T_TRAIT, \T_INTERFACE], $next + 1) !== null) {
return null;
}
++$next;
}

$next = $this->findNextToken($tokens, [\T_CLASS], $next + 1, \T_DOUBLE_COLON);
if ($next === null) {
return null;
}
// Exclude traits and interfaces
if ($this->findNextToken($tokens, [\T_TRAIT, \T_INTERFACE], $next + 1) !== null) {
return null;
}

$next = $this->findNextToken($tokens, [\T_STRING], $next + 1);
if ($next !== null) {
$class .= '\\' . $tokens[$next][1];
}
$next = $this->findNextToken($tokens, [\T_CLASS], $next + 1, \T_DOUBLE_COLON);
if ($next === null) {
return null;
}

$next = $this->findNextToken($tokens, [\T_STRING], $next + 1);
if ($next === null) {
return null;
}

$class .= '\\' . $tokens[$next][1];

/** @var class-string<object> $class */
return $class;
}
Expand Down

0 comments on commit f5eb5a8

Please sign in to comment.