Skip to content

Commit 548a019

Browse files
committed
Improve readability
1 parent 38c4414 commit 548a019

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/Symfony/Compiler/Discovery/ClassFinder.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@ private function find(callable $predicate): array
3535
$classes = [];
3636

3737
foreach ($this->findPhpFiles() as $phpFile) {
38-
$classes[] = $this->findClassesInFile($predicate, $phpFile);
38+
$classes[] = $this->findClassInFile($phpFile);
3939
}
4040

41-
$classes = \array_merge([], ...$classes);
41+
$classes = \array_filter($classes, function (?string $class) use ($predicate) {
42+
return !empty($class) && $predicate($class);
43+
});
4244

4345
\sort($classes);
4446

4547
return $classes;
4648
}
4749

48-
private function findClassesInFile(callable $predicate, \SplFileInfo $phpFile): array
50+
private function findClassInFile(\SplFileInfo $phpFile): ?string
4951
{
5052
// @see https://stackoverflow.com/a/27440555/330267
51-
$classes = [];
5253
$tokens = \token_get_all(\file_get_contents($phpFile->getRealPath()));
5354
$namespace = '';
5455

@@ -57,33 +58,36 @@ private function findClassesInFile(callable $predicate, \SplFileInfo $phpFile):
5758
continue;
5859
}
5960
if ($this->isNamespaceToken($tokens, $index)) {
60-
$index += 2; // Skip namespace keyword and whitespace
61-
while (isset($tokens[$index]) && \is_array($tokens[$index]) && T_WHITESPACE !== $tokens[$index][0]) {
61+
while ($this->isNotWhitespaceToken($tokens, $index)) {
6262
$namespace .= $tokens[$index++][1];
6363
}
6464
}
65-
if ($this->isClassDefinitionToken($tokens, $index)) {
66-
$index += 2; // Skip class keyword and whitespace
67-
$class = $namespace . '\\' . $tokens[$index][1];
68-
if ($predicate($class)) {
69-
$classes[] = $class;
70-
}
71-
72-
break;
65+
if ($this->isClassNameToken($tokens, $index)) {
66+
return $namespace . '\\' . $tokens[$index][1];
7367
}
7468
}
7569

76-
return $classes;
70+
return null;
7771
}
7872

7973
private function isNamespaceToken($tokens, int $index): bool
8074
{
81-
return T_NAMESPACE === $tokens[$index][0];
75+
return $this->extractTokens($tokens, $index - 2, 3) === [T_NAMESPACE, T_WHITESPACE, T_STRING];
76+
}
77+
78+
private function isClassNameToken($tokens, int $index): bool
79+
{
80+
return $this->extractTokens($tokens, $index - 2, 3) === [T_CLASS, T_WHITESPACE, T_STRING];
81+
}
82+
83+
private function extractTokens($tokens, int $startIndex, int $count): array
84+
{
85+
return \array_column(\array_slice($tokens, $startIndex, $count), 0);
8286
}
8387

84-
private function isClassDefinitionToken($tokens, int $index): bool
88+
private function isNotWhitespaceToken($tokens, int $index): bool
8589
{
86-
return T_CLASS === $tokens[$index][0] && T_WHITESPACE === $tokens[$index + 1][0] && T_STRING === $tokens[$index + 2][0];
90+
return isset($tokens[$index]) && \is_array($tokens[$index]) && T_WHITESPACE !== $tokens[$index][0];
8791
}
8892

8993
/**

0 commit comments

Comments
 (0)