Skip to content

Commit e751fb4

Browse files
committed
Add better error codes
1 parent 83ef2a0 commit e751fb4

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/Standards/Squiz/Sniffs/WhiteSpace/ScopeKeywordSpacingSniff.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,52 @@ public function process(File $phpcsFile, $stackPtr)
138138
}
139139

140140
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+
141180
$error = 'Scope keyword "%s" must be followed by a single space; found %s';
142181
$data = [
143182
$tokens[$stackPtr]['content'],
144183
$spacing,
145184
];
146185

147-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Incorrect', $data);
186+
$fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode, $data);
148187
if ($fix === true) {
149188
if ($spacing === 0) {
150189
$phpcsFile->fixer->addContent($stackPtr, ' ');

0 commit comments

Comments
 (0)