3
* Squiz_Sniffs_ControlStructures_InlineControlStructureSniff.
8
* @package PHP_CodeSniffer
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @author Marc McIntyre <mmcintyre@squiz.net>
11
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13
* @link http://pear.php.net/package/PHP_CodeSniffer
17
* Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff.
19
* Tests the spacing of shorthand IF statements.
22
* @package PHP_CodeSniffer
23
* @author Greg Sherwood <gsherwood@squiz.net>
24
* @author Marc McIntyre <mmcintyre@squiz.net>
25
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
26
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
27
* @version Release: 1.5.5
28
* @link http://pear.php.net/package/PHP_CodeSniffer
30
class Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff implements PHP_CodeSniffer_Sniff
35
* Returns an array of tokens this test wants to listen for.
39
public function register()
41
return array(T_INLINE_THEN);
47
* Processes this sniff, when one of its tokens is encountered.
49
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
50
* @param int $stackPtr The position of the current token in the
51
* stack passed in $tokens.
55
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
57
$tokens = $phpcsFile->getTokens();
59
// Find the opening bracket of the inline IF.
61
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
62
$parens = $tokens[$stackPtr]['nested_parenthesis'];
63
$i = array_pop($parens);
64
if (isset($tokens[$i]['parenthesis_owner']) === true) {
65
// The parenthesis are owned by a token like an array or
66
// function, so are not just used for grouping.
72
// Could not find the beginning of the statement. Probably not
73
// wrapped with brackets, so assume it ends with a
74
// semicolon (end of statement) or comma (end of array value).
75
$else = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1));
76
$statementEnd = $phpcsFile->findNext(array(T_SEMICOLON, T_COMMA), ($else + 1));
78
$statementEnd = $tokens[$i]['parenthesis_closer'];
81
// Make sure it's all on the same line.
82
if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) {
83
$error = 'Inline shorthand IF statement must be declared on a single line';
84
$phpcsFile->addError($error, $stackPtr, 'NotSingleLine');
88
// Make sure there are spaces around the question mark.
89
$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
90
$contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
91
if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) {
92
$error = 'Inline shorthand IF statement requires brackets around comparison';
93
$phpcsFile->addError($error, $stackPtr, 'NoBrackets');
97
$spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
98
if ($spaceBefore !== 1) {
99
$error = 'Inline shorthand IF statement requires 1 space before THEN; %s found';
100
$data = array($spaceBefore);
101
$phpcsFile->addError($error, $stackPtr, 'SpacingBeforeThen', $data);
104
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
105
if ($spaceAfter !== 1) {
106
$error = 'Inline shorthand IF statement requires 1 space after THEN; %s found';
107
$data = array($spaceAfter);
108
$phpcsFile->addError($error, $stackPtr, 'SpacingAfterThen', $data);
111
// Make sure the ELSE has the correct spacing.
112
$inlineElse = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false);
113
$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true);
114
$contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true);
116
$spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
117
if ($spaceBefore !== 1) {
118
$error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
119
$data = array($spaceBefore);
120
$phpcsFile->addError($error, $inlineElse, 'SpacingBeforeElse', $data);
123
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1));
124
if ($spaceAfter !== 1) {
125
$error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found';
126
$data = array($spaceAfter);
127
$phpcsFile->addError($error, $inlineElse, 'SpacingAfterElse', $data);