1111
1212namespace PhpCsFixerCustomFixers \Fixer ;
1313
14+ use PhpCsFixer \Fixer \Casing \ClassReferenceNameCasingFixer ;
15+ use PhpCsFixer \Fixer \DeprecatedFixerInterface ;
1416use PhpCsFixer \FixerDefinition \CodeSample ;
1517use PhpCsFixer \FixerDefinition \FixerDefinition ;
1618use PhpCsFixer \FixerDefinition \FixerDefinitionInterface ;
17- use PhpCsFixer \Tokenizer \Analyzer \NamespacesAnalyzer ;
18- use PhpCsFixer \Tokenizer \CT ;
19- use PhpCsFixer \Tokenizer \Token ;
2019use PhpCsFixer \Tokenizer \Tokens ;
2120
22- final class InternalClassCasingFixer extends AbstractFixer
21+ /**
22+ * @deprecated
23+ */
24+ final class InternalClassCasingFixer extends AbstractFixer implements DeprecatedFixerInterface
2325{
26+ /** @var ClassReferenceNameCasingFixer */
27+ private $ classReferenceNameCasingFixer ;
28+
29+ public function __construct ()
30+ {
31+ $ this ->classReferenceNameCasingFixer = new ClassReferenceNameCasingFixer ();
32+ }
33+
2434 public function getDefinition (): FixerDefinitionInterface
2535 {
2636 return new FixerDefinition (
@@ -31,99 +41,26 @@ public function getDefinition(): FixerDefinitionInterface
3141
3242 public function getPriority (): int
3343 {
34- return 0 ;
44+ return $ this -> classReferenceNameCasingFixer -> getPriority () ;
3545 }
3646
3747 public function isCandidate (Tokens $ tokens ): bool
3848 {
39- return $ tokens -> isTokenKindFound (\ T_STRING );
49+ return $ this -> classReferenceNameCasingFixer -> isCandidate ( $ tokens );
4050 }
4151
4252 public function isRisky (): bool
4353 {
44- return false ;
54+ return $ this -> classReferenceNameCasingFixer -> isRisky () ;
4555 }
4656
4757 public function fix (\SplFileInfo $ file , Tokens $ tokens ): void
4858 {
49- $ namespaces = (new NamespacesAnalyzer ())->getDeclarations ($ tokens );
50-
51- foreach ($ namespaces as $ namespace ) {
52- $ this ->fixCasing ($ tokens , $ namespace ->getScopeStartIndex (), $ namespace ->getScopeEndIndex (), $ namespace ->getFullName () === '' );
53- }
54- }
55-
56- private function fixCasing (Tokens $ tokens , int $ startIndex , int $ endIndex , bool $ isInGlobalNamespace ): void
57- {
58- for ($ index = $ startIndex ; $ index < $ endIndex ; $ index ++) {
59- if (!$ tokens [$ index ]->isGivenKind (\T_STRING )) {
60- continue ;
61- }
62-
63- if (!$ this ->isGlobalClassUsage ($ tokens , $ index , $ isInGlobalNamespace )) {
64- continue ;
65- }
66-
67- $ correctCase = $ this ->getCorrectCase ($ tokens [$ index ]->getContent ());
68-
69- if ($ correctCase === $ tokens [$ index ]->getContent ()) {
70- continue ;
71- }
72-
73- $ tokens [$ index ] = new Token ([\T_STRING , $ correctCase ]);
74- }
75- }
76-
77- private function isGlobalClassUsage (Tokens $ tokens , int $ index , bool $ isInGlobalNamespace ): bool
78- {
79- $ prevIndex = $ tokens ->getPrevMeaningfulToken ($ index );
80- \assert (\is_int ($ prevIndex ));
81-
82- if ($ tokens [$ prevIndex ]->isGivenKind (\T_NS_SEPARATOR )) {
83- $ prevIndex = $ tokens ->getPrevMeaningfulToken ($ prevIndex );
84- \assert (\is_int ($ prevIndex ));
85-
86- if ($ tokens [$ prevIndex ]->isGivenKind (\T_STRING )) {
87- return false ;
88- }
89- } elseif (!$ isInGlobalNamespace ) {
90- return false ;
91- }
92-
93- if ($ tokens [$ prevIndex ]->isGivenKind ([\T_AS , \T_CLASS , \T_CONST , \T_DOUBLE_COLON , \T_OBJECT_OPERATOR , CT ::T_USE_TRAIT ])) {
94- return false ;
95- }
96-
97- $ nextIndex = $ tokens ->getNextMeaningfulToken ($ index );
98- \assert (\is_int ($ nextIndex ));
99-
100- if ($ tokens [$ nextIndex ]->isGivenKind (\T_NS_SEPARATOR )) {
101- return false ;
102- }
103-
104- return $ tokens [$ prevIndex ]->isGivenKind ([\T_NEW ]) || !$ tokens [$ nextIndex ]->equals ('( ' );
59+ $ this ->classReferenceNameCasingFixer ->fix ($ file , $ tokens );
10560 }
10661
107- private function getCorrectCase ( string $ className ): string
62+ public function getSuccessorsNames ( ): array
10863 {
109- /** @var null|array<string, string> $classes */
110- static $ classes ;
111-
112- if ($ classes === null ) {
113- $ classes = [];
114- foreach (\get_declared_classes () as $ class ) {
115- if ((new \ReflectionClass ($ class ))->isInternal ()) {
116- $ classes [\strtolower ($ class )] = $ class ;
117- }
118- }
119- }
120-
121- $ lowercaseClassName = \strtolower ($ className );
122-
123- if (!\array_key_exists ($ lowercaseClassName , $ classes )) {
124- return $ className ;
125- }
126-
127- return $ classes [$ lowercaseClassName ];
64+ return [$ this ->classReferenceNameCasingFixer ->getName ()];
12865 }
12966}
0 commit comments