Skip to content

Commit

Permalink
Merge pull request #298 from jrfnl/Issue-297-sniff-errornames
Browse files Browse the repository at this point in the history
Add granular error/warning names - issue #297, #140
  • Loading branch information
westonruter committed Jan 14, 2015
2 parents f1423c4 + 0e532eb commit 47f76ee
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 57 deletions.
44 changes: 22 additions & 22 deletions WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}

if ( ! isset( $tokens[$stackPtr]['parenthesis_opener'] ) ) {
$phpcsFile->addError('Missing parenthesis opener.', $stackPtr);
$phpcsFile->addError('Missing parenthesis opener.', $stackPtr, 'NoParenthesis');
return;
}
$arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
if ( ! isset( $tokens[$arrayStart]['parenthesis_closer'] ) ) {
$phpcsFile->addError('Missing parenthesis closer.', $arrayStart);
$phpcsFile->addError('Missing parenthesis closer.', $arrayStart, 'NoParenthesis');
return;
}
$arrayEnd = $tokens[$arrayStart]['parenthesis_closer'];
Expand Down Expand Up @@ -93,7 +93,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[($openBracket + 1)]['code'] !== T_WHITESPACE && $tokens[($openBracket + 1)]['code'] !== T_CLOSE_PARENTHESIS) {
// Checking this: $value = my_function([*]...).
$warning = 'No space after opening parenthesis of array is bad style';
$phpcsFile->addWarning($warning, $stackPtr);
$phpcsFile->addWarning($warning, $stackPtr, 'NoSpaceAfterOpenParenthesis');
}

$closer = $tokens[$openBracket]['parenthesis_closer'];
Expand All @@ -109,7 +109,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// $value = my_function( ).
if ($between !== $closer) {
$warning = 'No space before closing parenthesis of array is bad style';
$phpcsFile->addWarning($warning, $closer);
$phpcsFile->addWarning($warning, $closer, 'NoSpaceBeforeCloseParenthesis');
}
}

Expand All @@ -130,26 +130,26 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[($nextArrow - 1)]['code'] !== T_WHITESPACE) {
$content = $tokens[($nextArrow - 1)]['content'];
$error = "Expected 1 space between \"$content\" and double arrow; 0 found";
$phpcsFile->addError($error, $nextArrow);
$phpcsFile->addError($error, $nextArrow, 'NoSpaceBeforeDoubleArrow');
} else {
$spaceLength = strlen($tokens[($nextArrow - 1)]['content']);
if ($spaceLength !== 1) {
$content = $tokens[($nextArrow - 2)]['content'];
$error = "Expected 1 space between \"$content\" and double arrow; $spaceLength found";
$phpcsFile->addError($error, $nextArrow);
$phpcsFile->addError($error, $nextArrow, 'SpaceBeforeDoubleArrow');
}
}

if ($tokens[($nextArrow + 1)]['code'] !== T_WHITESPACE) {
$content = $tokens[($nextArrow + 1)]['content'];
$error = "Expected 1 space between double arrow and \"$content\"; 0 found";
$phpcsFile->addError($error, $nextArrow);
$phpcsFile->addError($error, $nextArrow, 'NoSpaceAfterDoubleArrow');
} else {
$spaceLength = strlen($tokens[($nextArrow + 1)]['content']);
if ($spaceLength !== 1) {
$content = $tokens[($nextArrow + 2)]['content'];
$error = "Expected 1 space between double arrow and \"$content\"; $spaceLength found";
$phpcsFile->addError($error, $nextArrow);
$phpcsFile->addError($error, $nextArrow, 'SpaceAfterDoubleArrow');
}
}
}//end while
Expand All @@ -163,21 +163,21 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE) {
$content = $tokens[($comma + 1)]['content'];
$error = "Expected 1 space between comma and \"$content\"; 0 found";
$phpcsFile->addError($error, $comma);
$phpcsFile->addError($error, $comma, 'NoSpaceAfterComma');
} else {
$spaceLength = strlen($tokens[($comma + 1)]['content']);
if ($spaceLength !== 1) {
$content = $tokens[($comma + 2)]['content'];
$error = "Expected 1 space between comma and \"$content\"; $spaceLength found";
$phpcsFile->addError($error, $comma);
$phpcsFile->addError($error, $comma, 'SpaceAfterComma');
}
}

if ($tokens[($comma - 1)]['code'] === T_WHITESPACE) {
$content = $tokens[($comma - 2)]['content'];
$spaceLength = strlen($tokens[($comma - 1)]['content']);
$error = "Expected 0 spaces between \"$content\" and comma; $spaceLength found";
$phpcsFile->addError($error, $comma);
$phpcsFile->addError($error, $comma, 'SpaceBeforeComma');
}
}//end foreach
}//end if
Expand Down Expand Up @@ -222,7 +222,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$content = $tokens[($nextToken - 2)]['content'];
$spaceLength = strlen($tokens[($nextToken - 1)]['content']);
$error = "Expected 0 spaces between \"$content\" and comma; $spaceLength found";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'SpaceBeforeComma');
}

// Find the value, which will be the first token on the line,
Expand Down Expand Up @@ -250,7 +250,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($tokens[$nextToken]['code'] === T_DOUBLE_ARROW) {
if ($singleUsed === true) {
$error = 'Key specified for array entry; first entry has no key';
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'KeySpecified');
return;
}

Expand Down Expand Up @@ -325,7 +325,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$trailingContent = $phpcsFile->findPrevious(array(T_WHITESPACE, T_COMMENT), ($arrayEnd - 1), $lastIndex, true);
if ($tokens[$trailingContent]['code'] !== T_COMMA) {
$error = 'Comma required after last value in array declaration';
$phpcsFile->addError($error, $trailingContent);
$phpcsFile->addError($error, $trailingContent, 'NoCommaAfterLast');
}

foreach ($indices as $value) {
Expand All @@ -342,7 +342,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// was indented and not flush with the opening parenthesis.
if ($tokens[$value['value']]['column'] !== ($keywordStart + 1)) {
$error = 'Array value not aligned correctly; expected '.($keywordStart + 1).' spaces but found '.$tokens[$value['value']]['column'];
$phpcsFile->addError($error, $value['value']);
$phpcsFile->addError($error, $value['value'], 'ValueNotAligned');
}
}
*/
Expand Down Expand Up @@ -380,36 +380,36 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if (isset($index['index']) === false) {
// Array value only.
if (($tokens[$index['value']]['line'] === $tokens[$stackPtr]['line']) && ($numValues > 1)) {
$phpcsFile->addError('The first value in a multi-value array must be on a new line', $stackPtr);
$phpcsFile->addError('The first value in a multi-value array must be on a new line', $stackPtr, 'FirstValueNoNewline');
}

continue;
}

if (($tokens[$index['index']]['line'] === $tokens[$stackPtr]['line'])) {
$phpcsFile->addError('The first index in a multi-value array must be on a new line', $stackPtr);
$phpcsFile->addError('The first index in a multi-value array must be on a new line', $stackPtr, 'FirstIndexNoNewline');
continue;
}

/*
if ($tokens[$index['index']]['column'] !== $indicesStart) {
$phpcsFile->addError('Array key not aligned correctly; expected '.$indicesStart.' spaces but found '.$tokens[$index['index']]['column'], $index['index']);
$phpcsFile->addError('Array key not aligned correctly; expected '.$indicesStart.' spaces but found '.$tokens[$index['index']]['column'], $index['index'], 'KeyNotAligned');
continue;
}
if ($tokens[$index['arrow']]['column'] !== $arrowStart) {
$expected = ($arrowStart - (strlen($index['index_content']) + $tokens[$index['index']]['column']));
$expected .= ($expected === 1) ? ' space' : ' spaces';
$found = ($tokens[$index['arrow']]['column'] - (strlen($index['index_content']) + $tokens[$index['index']]['column']));
$phpcsFile->addError("Array double arrow not aligned correctly; expected $expected but found $found", $index['arrow']);
$phpcsFile->addError("Array double arrow not aligned correctly; expected $expected but found $found", $index['arrow'], 'DoubleArrowNotAligned');
continue;
}
if ($tokens[$index['value']]['column'] !== $valueStart) {
$expected = ($valueStart - (strlen($tokens[$index['arrow']]['content']) + $tokens[$index['arrow']]['column']));
$expected .= ($expected === 1) ? ' space' : ' spaces';
$found = ($tokens[$index['value']]['column'] - (strlen($tokens[$index['arrow']]['content']) + $tokens[$index['arrow']]['column']));
$phpcsFile->addError("Array value not aligned correctly; expected $expected but found $found", $index['arrow']);
$phpcsFile->addError("Array value not aligned correctly; expected $expected but found $found", $index['arrow'], 'ValueNotAligned');
}
*/

Expand Down Expand Up @@ -439,7 +439,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
if ( $fail ) {
$error = 'Each line in an array declaration must end in a comma';
$phpcsFile->addError($error, $index['value']);
$phpcsFile->addError($error, $index['value'], 'NoComma');
}
}

Expand All @@ -448,7 +448,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$content = $tokens[($nextComma - 2)]['content'];
$spaceLength = strlen($tokens[($nextComma - 1)]['content']);
$error = "Expected 0 spaces between \"$content\" and comma; $spaceLength found";
$phpcsFile->addError($error, $nextComma);
$phpcsFile->addError($error, $nextComma, 'SpaceBeforeComma');
}
}
}//end foreach
Expand Down
6 changes: 3 additions & 3 deletions WordPress/Sniffs/Arrays/ArrayKeySpacingRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr )

$token = $tokens[ $stackPtr ];
if ( ! isset( $token['bracket_closer'] ) ) {
$phpcsFile->addWarning( 'Missing bracket closer.', $stackPtr );
$phpcsFile->addWarning( 'Missing bracket closer.', $stackPtr, 'MissingBracketCloser' );
return;
}

Expand All @@ -55,11 +55,11 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr )
// It should have spaces only if it only has strings or numbers as the key
if ( $need_spaces && ! ( $spaced1 && $spaced2 ) ) {
$error = 'Array keys should be surrounded by spaces unless they contain a string or an integer.';
$phpcsFile->addWarning( $error, $stackPtr );
$phpcsFile->addWarning( $error, $stackPtr, 'NoSpacesAroundArrayKeys' );
}
elseif( ! $need_spaces && ( $spaced1 || $spaced2 ) ) {
$error = 'Array keys should NOT be surrounded by spaces if they only contain a string or an integer.';
$phpcsFile->addWarning( $error, $stackPtr );
$phpcsFile->addWarning( $error, $stackPtr, 'SpacesAroundArrayKeys' );
}

}//end process()
Expand Down
4 changes: 2 additions & 2 deletions WordPress/Sniffs/Classes/ValidClassNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$error = 'Possible parse error: ';
$error .= $tokens[$stackPtr]['content'];
$error .= ' missing opening or closing brace';
$phpcsFile->addWarning($error, $stackPtr);
$phpcsFile->addWarning($error, $stackPtr, 'MissingBrace');
return;
}

Expand All @@ -81,7 +81,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($valid === false) {
$type = ucfirst($tokens[$stackPtr]['content']);
$error = "$type name \"$name\" is not in camel caps format";
$phpcsFile->addError($error, $stackPtr);
$phpcsFile->addError($error, $stackPtr, 'NotCamelCaps');
}

}//end process()
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Sniffs/Files/FileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if (strpos($fileName, '_') !== false) {
$expected = str_replace('_', '-', $fileName);
$error = ucfirst('Filename "'.$fileName.'" with underscores found; use '.$expected.' instead');
$phpcsFile->addError($error, $stackPtr);
$phpcsFile->addError($error, $stackPtr, 'UnderscoresNotAllowed');
}

}//end process()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$gap = strlen($tokens[($nextParam + 1)]['content']);
$arg = $tokens[$nextParam]['content'];
$error = "Expected 1 space between argument \"$arg\" and equals sign; ".($gap - 1)." found";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'SpaceBeforeEquals');
}

if ($tokens[($nextToken + 1)]['code'] !== T_WHITESPACE) {
$gap = strlen($tokens[($nextToken + 1)]['content']);
$arg = $tokens[$nextParam]['content'];
$error = "Expected 1 space between default value and equals sign for argument \"$arg\";";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'SpaceAfterEquals');
}
}

Expand All @@ -91,7 +91,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$space = strlen($tokens[($nextComma - 1)]['content']);
$arg = $tokens[$nextParam]['content'];
$error = "Expected 0 spaces between argument \"$arg\" and comma; $space found";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'SpaceBeforeComma');
}
}

Expand Down Expand Up @@ -124,28 +124,28 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)

if ($gap !== 1) {
$error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'SpacingAfterHint');
}

if ($multiLine === false) {
if ($tokens[($comma + 1)]['code'] !== T_WHITESPACE) {
$error = "Expected 1 space between comma and type hint \"$hint\"; 0 found";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'NoSpaceBeforeHint');
} else {
$gap = strlen($tokens[($comma + 1)]['content']);
if ($gap !== 1) {
$error = "Expected 1 space between comma and type hint \"$hint\"; $gap found";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'SpacingBeforeHint');
}
}
}
} else if ($multiLine === false && $gap !== 1) {
$error = "Expected 1 space between comma and argument \"$arg\"; $gap found";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'SpacingBeforeArg');
}//end if
} else {
$error = "Expected 1 space between comma and argument \"$arg\"; 0 found";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'NoSpaceBeforeArg');
}//end if
} else {
// First argument in function declaration.
Expand All @@ -167,7 +167,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)

if ($gap !== 1) {
$error = "Expected 1 space between type hint and argument \"$arg\"; $gap found";
$phpcsFile->addError($error, $nextToken);
$phpcsFile->addError($error, $nextToken, 'SpacingAfterHint');
}
}
}//end if
Expand Down
6 changes: 3 additions & 3 deletions WordPress/Sniffs/NamingConventions/ValidFunctionNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $st
$suggested = str_replace('__', '_', $suggested);

$error = "Function name \"$functionName\" is in camel caps format, try '".$suggested."'";
$phpcsFile->addError($error, $stackPtr);
$phpcsFile->addError($error, $stackPtr, 'FunctionNameInvalid');
}

}//end processTokenOutsideScope()
Expand All @@ -82,7 +82,7 @@ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $sta
$magicPart = substr($methodName, 2);
if (in_array($magicPart, $this->_magicMethods) === false) {
$error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
$phpcsFile->addError($error, $stackPtr);
$phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore');
}

return;
Expand Down Expand Up @@ -123,7 +123,7 @@ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $sta
$suggested = str_replace('__', '_', $suggested);

$error = "Function name \"$methodName\" is in camel caps format, try '".$suggested."'";
$phpcsFile->addError($error, $stackPtr);
$phpcsFile->addError($error, $stackPtr, 'FunctionNameInvalid');
}

}//end processTokenWithinScope()
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Sniffs/VIP/AdminBarRemovalSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr )
$tokens = $phpcsFile->getTokens();

if ( in_array( trim( $tokens[$stackPtr]['content'], '"\'' ), array( 'show_admin_bar' ) ) ) {
$phpcsFile->addError( 'Removal of admin bar is prohibited.', $stackPtr );
$phpcsFile->addError( 'Removal of admin bar is prohibited.', $stackPtr, 'RemovalDetected');
}
}//end process()

Expand Down
Loading

0 comments on commit 47f76ee

Please sign in to comment.