forked from symplify/coding-standard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassNameResolver.php
More file actions
65 lines (50 loc) · 1.6 KB
/
ClassNameResolver.php
File metadata and controls
65 lines (50 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Symplify\CodingStandard\Fixer\Naming;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
final class ClassNameResolver
{
/**
* @var array<string, string>
*/
private array $classNameByFilePath = [];
/**
* @param Tokens<Token> $tokens
*/
public function resolveClassName(SplFileInfo $splFileInfo, Tokens $tokens): ?string
{
$filePath = $splFileInfo->getRealPath();
if (isset($this->classNameByFilePath[$filePath])) {
return $this->classNameByFilePath[$filePath];
}
$classLikeName = $this->resolveFromTokens($tokens);
if (! is_string($classLikeName)) {
return null;
}
$this->classNameByFilePath[$filePath] = $classLikeName;
return $classLikeName;
}
/**
* @param Tokens<Token> $tokens
*/
private function resolveFromTokens(Tokens $tokens): ?string
{
foreach ($tokens as $position => $token) {
if (! $token->isGivenKind([T_CLASS, T_TRAIT, T_INTERFACE])) {
continue;
}
$nextNextMeaningfulTokenIndex = $tokens->getNextMeaningfulToken($position + 1);
if (null === $nextNextMeaningfulTokenIndex) {
continue;
}
$nextNextMeaningfulToken = $tokens[$nextNextMeaningfulTokenIndex];
// skip anonymous classes
if (! $nextNextMeaningfulToken->isGivenKind(T_STRING)) {
continue;
}
return $nextNextMeaningfulToken->getContent();
}
return null;
}
}