@@ -138,13 +138,52 @@ public function process(File $phpcsFile, $stackPtr)
138
138
}
139
139
140
140
if ($ spacing !== 1 ) {
141
+ // Set a context error code by checking if this is a class property,
142
+ // method, closure or class declaration.
143
+ $ ignoreTokens = (Tokens::$ emptyTokens + Tokens::$ methodPrefixes + [
144
+ T_READONLY => T_READONLY ,
145
+ // Ignore all the tokens in type hints.
146
+ T_STRING => T_STRING ,
147
+ T_NULLABLE => T_NULLABLE ,
148
+ T_BITWISE_AND => T_BITWISE_AND ,
149
+ T_TYPE_INTERSECTION => T_TYPE_INTERSECTION ,
150
+ T_TYPE_UNION => T_TYPE_UNION ,
151
+ T_TYPE_OPEN_PARENTHESIS => T_TYPE_OPEN_PARENTHESIS ,
152
+ T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS ,
153
+ ]);
154
+ $ targetContext = $ phpcsFile ->findNext ($ ignoreTokens , ($ stackPtr + 1 ), null , true , null , true );
155
+ $ errorCode = 'Incorrect ' ;
156
+ if (isset ($ tokens [$ targetContext ]['code ' ]) === true ) {
157
+ switch ($ tokens [$ targetContext ]['code ' ]) {
158
+ case T_VARIABLE :
159
+ $ errorCode = 'Property ' ;
160
+ break ;
161
+
162
+ case T_FUNCTION :
163
+ $ errorCode = 'Method ' ;
164
+ break ;
165
+
166
+ case T_CLOSURE :
167
+ $ errorCode = 'Closure ' ;
168
+ break ;
169
+
170
+ case T_CLASS :
171
+ $ errorCode = 'Class ' ;
172
+ break ;
173
+
174
+ case T_ANON_CLASS :
175
+ $ errorCode = 'AnonymousClass ' ;
176
+ break ;
177
+ }//end switch
178
+ }//end if
179
+
141
180
$ error = 'Scope keyword "%s" must be followed by a single space; found %s ' ;
142
181
$ data = [
143
182
$ tokens [$ stackPtr ]['content ' ],
144
183
$ spacing ,
145
184
];
146
185
147
- $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , ' Incorrect ' , $ data );
186
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , $ errorCode , $ data );
148
187
if ($ fix === true ) {
149
188
if ($ spacing === 0 ) {
150
189
$ phpcsFile ->fixer ->addContent ($ stackPtr , ' ' );
0 commit comments