Skip to content

Commit 0dad342

Browse files
committed
Update scanner
1 parent fa9ed96 commit 0dad342

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

lib/Adapter/Simple/ClassScanner.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,10 @@
1111
*/
1212
class ClassScanner
1313
{
14-
public function getClassNameFromFile($file)
14+
public function getClassNameFromFile(string $file): ?string
1515
{
16-
if (!file_exists($file)) {
17-
return null;
18-
}
19-
2016
$fp = fopen($file, 'r');
2117

22-
if (false === $fp) {
23-
throw new RuntimeException(sprintf(
24-
'Could not open file "%s"',
25-
$file
26-
));
27-
}
28-
2918
$class = $namespace = $buffer = '';
3019
$i = 0;
3120

@@ -38,29 +27,31 @@ public function getClassNameFromFile($file)
3827
for ($line = 0; $line <= 20; $line++) {
3928
$buffer .= fgets($fp);
4029
}
41-
$tokens = @token_get_all($buffer);
30+
$tokens = @\token_get_all($buffer);
4231

4332
if (strpos($buffer, '{') === false) {
4433
continue;
4534
}
4635

4736
for (; $i < count($tokens); $i++) {
48-
if ($tokens[$i][0] === \T_NAMESPACE) {
37+
if ($tokens[$i][0] === T_NAMESPACE) {
4938
for ($j = $i + 1; $j < count($tokens); $j++) {
5039
$tokenId = $tokens[$j][0];
51-
if ($tokenId === T_STRING || $tokenId === 314) {
40+
$namespaceToken = defined('T_NAME_QUALIFIED') ? T_NAME_QUALIFIED : T_STRING;
41+
42+
if ($tokenId === T_STRING || $tokenId === $namespaceToken) {
5243
$namespace .= '\\' . $tokens[$j][1];
5344
} elseif ($tokens[$j] === '{' || $tokens[$j] === ';') {
5445
break;
5546
}
5647
}
5748
}
5849

59-
$token = $tokens[$i][0];
60-
if ($token === \T_INTERFACE || $token === \T_CLASS || $token === \T_TRAIT) {
50+
if ($tokens[$i][0] === T_CLASS) {
6151
for ($j = $i + 1; $j < count($tokens); $j++) {
62-
if ($tokens[$j][0] === \T_STRING) {
52+
if ($tokens[$j][0] === T_STRING) {
6353
$class = $tokens[$i + 2][1];
54+
6455
break 2;
6556
}
6657
}
@@ -72,8 +63,6 @@ public function getClassNameFromFile($file)
7263
return null;
7364
}
7465

75-
fclose($fp);
76-
77-
return ltrim($namespace . '\\' . $class, '\\');
66+
return $namespace . '\\' . $class;
7867
}
7968
}

lib/Adapter/Simple/SimpleClassToFile.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ public function classToFileCandidates(ClassName $className): FilePathCandidates
4141
$iterator = new RegexIterator($iterator, $pattern);
4242

4343
foreach ($iterator as $phpFile) {
44-
if (ClassName::fromString(
45-
$this->classScanner->getClassNameFromFile($phpFile->getPathName())
46-
) == $className) {
44+
$className = $this->classScanner->getClassNameFromFile($phpFile->getPathName());
45+
if (ClassName::fromString($className) == $className) {
4746
$candidates[] = FilePath::fromString($phpFile->getPathName());
4847
}
4948
}

0 commit comments

Comments
 (0)