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 Squiz Pty Ltd (ABN 77 084 670 600)
12
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
* @version CVS: $Id: InlineIfDeclarationSniff.php,v 1.4 2007/10/15 04:46:21 squiz Exp $
14
* @link http://pear.php.net/package/PHP_CodeSniffer
18
* Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff.
20
* Tests the spacing of shorthand IF statements.
23
* @package PHP_CodeSniffer
24
* @author Greg Sherwood <gsherwood@squiz.net>
25
* @author Marc McIntyre <mmcintyre@squiz.net>
26
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
27
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
28
* @version Release: 1.0.1
29
* @link http://pear.php.net/package/PHP_CodeSniffer
31
class Squiz_Sniffs_ControlStructures_InlineIfDeclarationSniff implements PHP_CodeSniffer_Sniff
36
* Returns an array of tokens this test wants to listen for.
40
public function register()
42
return array(T_INLINE_THEN);
48
* Processes this sniff, when one of its tokens is encountered.
50
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
51
* @param int $stackPtr The position of the current token in the
52
* stack passed in $tokens.
56
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
$tokens = $phpcsFile->getTokens();
60
// Find the opening bracket of the inline IF.
61
for ($i = ($stackPtr - 1); $i > 0; $i--) {
62
if (isset($tokens[$i]['parenthesis_opener']) === true && $tokens[$i]['parenthesis_opener'] < $i) {
63
$i = $tokens[$i]['parenthesis_opener'];
67
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
73
// Could not find the begining of the statement, so we can't
74
// find the end either.
78
$statementEnd = $tokens[$i]['parenthesis_closer'];
80
// Make sure it's all on the same line.
81
if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) {
82
$error = 'Inline shorthand IF statement must be declared on a single line';
83
$phpcsFile->addError($error, $stackPtr);
87
// Make sure there are spaces around the question mark.
88
$contentBefore = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true);
89
$contentAfter = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true);
90
if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) {
91
$error = 'Inline shorthand IF statement requires brackets around comparison';
92
$phpcsFile->addError($error, $stackPtr);
96
$spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
97
if ($spaceBefore !== 1) {
98
$error = "Inline shorthand IF statement requires 1 space before THEN; $spaceBefore found.";
99
$phpcsFile->addError($error, $stackPtr);
102
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
103
if ($spaceAfter !== 1) {
104
$error = "Inline shorthand IF statement requires 1 space after THEN; $spaceAfter found.";
105
$phpcsFile->addError($error, $stackPtr);
108
// If there is an else in this condition, make sure it has correct spacing.
109
$inlineElse = $phpcsFile->findNext(array(T_COLON), ($stackPtr + 1), $statementEnd, false);
110
if ($inlineElse === false) {
111
// No else condition.
115
$contentBefore = $phpcsFile->findPrevious(array(T_WHITESPACE), ($inlineElse - 1), null, true);
116
$contentAfter = $phpcsFile->findNext(array(T_WHITESPACE), ($inlineElse + 1), null, true);
118
$spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + strlen($tokens[$contentBefore]['content'])));
119
if ($spaceBefore !== 1) {
120
$error = "Inline shorthand IF statement requires 1 space before ELSE; $spaceBefore found.";
121
$phpcsFile->addError($error, $inlineElse);
124
$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1));
125
if ($spaceAfter !== 1) {
126
$error = "Inline shorthand IF statement requires 1 space after ELSE; $spaceAfter found.";
127
$phpcsFile->addError($error, $inlineElse);