@@ -55,11 +55,11 @@ public function isRisky(): bool
5555
5656 public function fix (\SplFileInfo $ file , Tokens $ tokens ): void
5757 {
58- $ isNamespaced = false ;
58+ $ namespaceStartIndex = null ;
5959
6060 for ($ index = 1 ; $ index < $ tokens ->count (); $ index ++) {
6161 if ($ tokens [$ index ]->isGivenKind (\T_NAMESPACE )) {
62- $ isNamespaced = true ;
62+ $ namespaceStartIndex = $ index ;
6363 continue ;
6464 }
6565
@@ -76,7 +76,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7676 continue ;
7777 }
7878
79- $ this ->addStringableInterface ($ tokens , $ index , $ isNamespaced );
79+ $ this ->addStringableInterface ($ tokens , $ index , $ namespaceStartIndex );
8080 }
8181 }
8282
@@ -101,7 +101,7 @@ private function doesHaveToStringMethod(Tokens $tokens, int $classStartIndex, in
101101 return false ;
102102 }
103103
104- private function addStringableInterface (Tokens $ tokens , int $ classIndex , bool $ isNamespaced ): void
104+ private function addStringableInterface (Tokens $ tokens , int $ classIndex , ? int $ namespaceStartIndex ): void
105105 {
106106 $ implementsIndex = $ tokens ->getNextTokenOfKind ($ classIndex , ['{ ' , [\T_IMPLEMENTS ]]);
107107 \assert (\is_int ($ implementsIndex ));
@@ -126,7 +126,7 @@ private function addStringableInterface(Tokens $tokens, int $classIndex, bool $i
126126
127127 $ implementsEndIndex = $ tokens ->getNextTokenOfKind ($ implementsIndex , ['{ ' ]);
128128 \assert (\is_int ($ implementsEndIndex ));
129- if ($ this ->isStringableAlreadyUsed ($ tokens , $ implementsIndex + 1 , $ implementsEndIndex - 1 , $ isNamespaced )) {
129+ if ($ this ->isStringableAlreadyUsed ($ tokens , $ implementsIndex + 1 , $ implementsEndIndex - 1 , $ namespaceStartIndex )) {
130130 return ;
131131 }
132132
@@ -144,7 +144,7 @@ private function addStringableInterface(Tokens $tokens, int $classIndex, bool $i
144144 );
145145 }
146146
147- private function isStringableAlreadyUsed (Tokens $ tokens , int $ implementsStartIndex , int $ implementsEndIndex , bool $ isNamespaced ): bool
147+ private function isStringableAlreadyUsed (Tokens $ tokens , int $ implementsStartIndex , int $ implementsEndIndex , ? int $ namespaceStartIndex ): bool
148148 {
149149 for ($ index = $ implementsStartIndex ; $ index < $ implementsEndIndex ; $ index ++) {
150150 if (!$ tokens [$ index ]->equals ([\T_STRING , 'Stringable ' ], false )) {
@@ -162,9 +162,35 @@ private function isStringableAlreadyUsed(Tokens $tokens, int $implementsStartInd
162162 return true ;
163163 }
164164 } else {
165- if (! $ isNamespaced ) {
165+ if ($ namespaceStartIndex === null ) {
166166 return true ;
167167 }
168+ if ($ this ->isStringableImported ($ tokens , $ namespaceStartIndex , $ index )) {
169+ return true ;
170+ }
171+ }
172+ }
173+
174+ return false ;
175+ }
176+
177+ private function isStringableImported (Tokens $ tokens , int $ startIndex , int $ endIndex ): bool
178+ {
179+ for ($ index = $ startIndex ; $ index < $ endIndex ; $ index ++) {
180+ if (!$ tokens [$ index ]->equals ([\T_STRING , 'Stringable ' ], false )) {
181+ continue ;
182+ }
183+
184+ $ useIndex = $ tokens ->getPrevMeaningfulToken ($ index );
185+ \assert (\is_int ($ useIndex ));
186+
187+ if ($ tokens [$ useIndex ]->isGivenKind (\T_NS_SEPARATOR )) {
188+ $ useIndex = $ tokens ->getPrevMeaningfulToken ($ useIndex );
189+ \assert (\is_int ($ useIndex ));
190+ }
191+
192+ if ($ tokens [$ useIndex ]->isGivenKind (\T_USE )) {
193+ return true ;
168194 }
169195 }
170196
0 commit comments