@@ -33,6 +33,8 @@ class ReferenceUsedNamesAfterUsageSniff implements \PHP_CodeSniffer\Sniffs\Sniff
3333
3434 public ?int $ count = null ;
3535 public ?int $ length = null ;
36+ public ?int $ lineLength = null ;
37+ public ?int $ lineClassLength = null ;
3638
3739 /**
3840 * @return array<int, (int|string)>
@@ -165,9 +167,17 @@ public function process(File $phpcsFile, $openTagPointer) : void
165167 $ shouldBeUsed = $ isFullyQualified ;
166168 }
167169
170+ $ tokens = $ phpcsFile ->getTokens ();
171+ 172+ $ start = TokenHelper::findFirstTokenOnLine ($ phpcsFile , $ reference ->startPointer );
173+ $ end = TokenHelper::findLastTokenOnLine ($ phpcsFile , $ reference ->startPointer );
174+ $ lineLength = \strlen (TokenHelper::getContent ($ phpcsFile , $ start , $ end ));
175+ 168176 if (!$ shouldBeUsed
169177 || ($ this ->count !== null && $ referenced [$ canonicalName ] < $ this ->count )
170178 && ($ this ->length !== null && $ this ->length > \strlen ($ canonicalName ))
179+ && ($ this ->lineLength !== null && $ this ->lineClassLength !== null && ($ this ->lineClassLength >= \strlen ($ canonicalName )
180+ || $ this ->lineLength >= $ lineLength ))
171181 ) {
172182 continue ;
173183 }
@@ -185,6 +195,12 @@ public function process(File $phpcsFile, $openTagPointer) : void
185195 . $ this ->length . ' symbols. ' ;
186196 }
187197
198+ if ($ this ->lineLength !== null && $ this ->lineClassLength !== null && \strlen ($ canonicalName ) > $ this ->lineClassLength
199+ && $ lineLength > $ this ->lineLength ) {
200+ $ reason = 'because line length is more than ' . $ this ->lineLength
201+ . ' symbols and class length is more than ' . $ this ->lineClassLength . ' symbols. ' ;
202+ }
203+ 188204 $ referenceErrors [] = (object ) [
189205 'reference ' => $ reference ,
190206 'canonicalName ' => $ canonicalName ,
@@ -210,7 +226,7 @@ public function process(File $phpcsFile, $openTagPointer) : void
210226 $ startPointer = $ reference ->startPointer ;
211227 $ canonicalName = $ referenceData ->canonicalName ;
212228 $ useStatements = UseStatementHelper::getUseStatementsForPointer ($ phpcsFile , $ reference ->startPointer );
213- [$ nameToReference , $ isConflicting ] = $ this ->getNormalizedClassName ($ reference ->name , $ useStatements );
229+ [$ nameToReference , $ isConflicting ] = $ this ->getNormalizedClassName ($ reference ->name , $ useStatements, $ phpcsFile );
214230 $ canonicalNameToReference = \strtolower ($ nameToReference );
215231
216232 $ canBeFixed = \array_reduce (
@@ -437,40 +453,58 @@ private function getReferences(\PHP_CodeSniffer\Files\File $phpcsFile, int $open
437453 return $ references ;
438454 }
439455
440- private function getNormalizedClassName (string $ name , array $ useStatements ) : array
456+ private function getUniqueNameFromNamespace (string $ first , string $ second ) : array
441457 {
442- $ unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName ($ name );
458+ $ firstSplit = \explode ('\\' , \ltrim ($ first , '\\' ));
459+ $ secondSplit = \explode ('\\' , \ltrim ($ second , '\\' ));
443460
444- foreach ( $ useStatements as $ useStatement ) {
445- $ useStatementUnqualified = NamespaceHelper:: getUnqualifiedNameFromFullyQualifiedName ( $ useStatement -> getFullyQualifiedTypeName ()) ;
461+ $ i = 0 ;
462+ $ toUse = null ;
446463
447- if ($ unqualifiedName !== $ useStatementUnqualified ) {
448- continue ;
464+ foreach ($ firstSplit as $ value ) {
465+ if (!isset ($ secondSplit [$ i ])) {
466+ break ;
449467 }
450468
451- $ nameSplit = \explode ( '\\' , \ltrim ( $ name , '\\' ));
452- $ useStatementSplit = \explode ( '\\' , \ltrim ( $ useStatement -> getFullyQualifiedTypeName (), '\\' ) );
469+ if ( \substr ( $ value , 0 , 1 ) !== \substr ( $ secondSplit [ $ i ], 0 , 1 )) {
470+ $ toUse = \substr ( $ value , 0 , 1 );
453471
454- $ i = 0 ;
455- $ toUse = null ;
472+ break ;
473+ }
456474
457- foreach ($ nameSplit as $ value ) {
458- if (!isset ($ useStatementSplit [$ i ])) {
459- break ;
460- }
475+ $ i ++;
476+ }
461477
462- if (\substr ($ value , 0 , 1 ) !== \substr ($ useStatementSplit [$ i ], 0 , 1 )) {
463- $ toUse = \substr ($ value , 0 , 1 );
478+ $ unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName ($ first );
464479
465- break ;
466- }
480+ return $ toUse === null
481+ ? [$ unqualifiedName , false ]
482+ : [$ toUse . $ unqualifiedName , true ];
483+ }
484+ 485+ private function getNormalizedClassName (string $ name , array $ useStatements , File $ phpcsFile ) : array
486+ {
487+ $ unqualifiedName = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName ($ name );
488+ $ className = ClassHelper::getName ($ phpcsFile , TokenHelper::findNext ($ phpcsFile , \T_CLASS , 0 ));
489+ 490+ if ($ className === $ unqualifiedName ) {
491+ return $ this ->getUniqueNameFromNamespace (
492+ $ name ,
493+ ClassHelper::getFullyQualifiedName ($ phpcsFile , TokenHelper::findNext ($ phpcsFile , \T_CLASS , 0 )),
494+ );
495+ }
496+ 497+ foreach ($ useStatements as $ useStatement ) {
498+ $ useStatementUnqualified = NamespaceHelper::getUnqualifiedNameFromFullyQualifiedName ($ useStatement ->getFullyQualifiedTypeName ());
467499
468- $ i ++;
500+ if ($ unqualifiedName !== $ useStatementUnqualified ) {
501+ continue ;
469502 }
470503
471- return $ toUse === null
472- ? [$ unqualifiedName , false ]
473- : [$ toUse . $ unqualifiedName , true ];
504+ return $ this ->getUniqueNameFromNamespace (
505+ $ name ,
506+ $ useStatement ->getFullyQualifiedTypeName (),
507+ );
474508 }
475509
476510 return [$ unqualifiedName , false ];
0 commit comments