@@ -44,17 +44,38 @@ public function process(File $phpcsFile, $stackPtr): void
4444 $ lastUsePtr = null ;
4545
4646 while ($ usePtr = $ phpcsFile ->findNext ([T_USE , T_CLASS , T_INTERFACE , T_TRAIT ], $ usePtr + 1 )) {
47- if ($ tokens [$ usePtr ]['code ' ] !== T_USE ) {
47+ $ token = $ tokens [$ usePtr ];
48+
49+ if ($ token ['code ' ] !== T_USE ) {
4850 break ;
4951 }
5052
53+ $ tokenLevel = $ token ['level ' ];
54+ $ shouldBeMinSpaces = $ tokenLevel * 4 ;
55+
56+ if ($ token ['column ' ] > $ shouldBeMinSpaces + 1 ) {
57+ $ phpcsFile ->addFixableError (sprintf (
58+ 'Line indented incorrectly; expected %d spaces, found %d ' ,
59+ $ shouldBeMinSpaces ,
60+ $ token ['column ' ] - 1 ,
61+ ), $ usePtr , 'IndentIncorrect ' );
62+
63+ $ phpcsFile ->fixer ->beginChangeset ();
64+ $ phpcsFile ->fixer
65+ ->replaceToken ($ usePtr - 1 , $ shouldBeMinSpaces > 0 ? str_repeat (' ' , $ shouldBeMinSpaces ) : "\n" );
66+ $ phpcsFile ->fixer ->endChangeset ();
67+ }
68+
5169 if ($ nsStartPtr = $ phpcsFile ->findNext ([T_NS_SEPARATOR , T_STRING ], $ usePtr + 1 )) {
5270 if ($ nsEndPtr = $ phpcsFile ->findNext ([T_NS_SEPARATOR , T_STRING ], $ nsStartPtr + 1 , null , true )) {
5371 $ namespace = $ phpcsFile ->getTokensAsString ($ nsStartPtr , $ nsEndPtr - $ nsStartPtr );
54- $ uses [$ nsStartPtr ] = $ namespace ;
72+ $ uses [$ tokenLevel ][ $ nsStartPtr ] = $ namespace ;
5573
5674 if ($ lastUsePtr !== null ) {
57- $ padding = $ tokens [$ usePtr ]['line ' ] - $ tokens [$ lastUsePtr ]['line ' ];
75+ $ padding = $ tokenLevel === $ tokens [$ lastUsePtr ]['level ' ]
76+ ? $ token ['line ' ] - $ tokens [$ lastUsePtr ]['line ' ]
77+ : 1 ;
78+
5879 if ($ padding === 0 ) {
5980 $ error = 'Each USE statement must be on a line by itself ' ;
6081 $ phpcsFile ->addError ($ error , $ usePtr , 'SameLine ' );
@@ -68,7 +89,7 @@ public function process(File $phpcsFile, $stackPtr): void
6889 break ;
6990 }
7091
71- if ($ tokens [$ i ]['line ' ] < $ tokens [ $ usePtr ] ['line ' ]) {
92+ if ($ tokens [$ i ]['line ' ] < $ token ['line ' ]) {
7293 $ phpcsFile ->fixer ->replaceToken ($ i , '' );
7394 }
7495 }
@@ -83,20 +104,24 @@ public function process(File $phpcsFile, $stackPtr): void
83104 }
84105 }
85106
86- $ orderedUses = array_values ($ uses );
87- sort ($ orderedUses , SORT_STRING | SORT_FLAG_CASE );
107+
88108 $ replacements = [];
89109
90- foreach ($ uses as $ ptr => $ use ) {
91- $ orderedUse = current ( $ orderedUses );
92- next ($ orderedUses );
110+ foreach ($ uses as $ usesList ) {
111+ $ orderedUses = array_values ( $ usesList );
112+ sort ($ orderedUses, SORT_STRING | SORT_FLAG_CASE );
93113
94- if ($ orderedUse !== $ use ) {
95- $ error = ' USE statements must be ordered; here should be %s ' ;
96- $ data = [ $ orderedUse ] ;
114+ foreach ($ usesList as $ ptr => $ use ) {
115+ $ orderedUse = current ( $ orderedUses ) ;
116+ next ( $ orderedUses ) ;
97117
98- if ($ phpcsFile ->addFixableError ($ error , $ ptr , 'Unordered ' , $ data )) {
99- $ replacements [$ ptr ] = $ orderedUse ;
118+ if ($ orderedUse !== $ use ) {
119+ $ error = 'USE statements must be ordered; here should be %s ' ;
120+ $ data = [$ orderedUse ];
121+
122+ if ($ phpcsFile ->addFixableError ($ error , $ ptr , 'Unordered ' , $ data )) {
123+ $ replacements [$ ptr ] = $ orderedUse ;
124+ }
100125 }
101126 }
102127 }
0 commit comments