1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodeIgniter_Sniffs_WhiteSpace_ElseOnNewLineSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Thomas Ernest <[email protected]> |
10
|
|
|
* @copyright 2006 Thomas Ernest |
11
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
12
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CodeIgniter_Sniffs_WhiteSpace_ElseOnNewLineSniff. |
17
|
|
|
* |
18
|
|
|
* Ensures that control structures else and elseif stand on new lines. |
19
|
|
|
* |
20
|
|
|
* @category PHP |
21
|
|
|
* @package PHP_CodeSniffer |
22
|
|
|
* @author Thomas Ernest <[email protected]> |
23
|
|
|
* @copyright 2006 Thomas Ernest |
24
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
25
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace CodeIgniter\Sniffs\WhiteSpace; |
29
|
|
|
|
30
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
31
|
|
|
use PHP_CodeSniffer\Files\File; |
32
|
|
|
|
33
|
|
|
class ElseOnNewLineSniff implements Sniff |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns an array of tokens this test wants to listen for. |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function register() |
42
|
|
|
{ |
43
|
|
|
return array( |
44
|
|
|
T_ELSE, |
45
|
|
|
T_ELSEIF, |
46
|
|
|
); |
47
|
|
|
}//end register() |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Processes this test, when one of its tokens is encountered. |
52
|
|
|
* |
53
|
|
|
* @param File $phpcsFile The current file being scanned. |
54
|
|
|
* @param int $stackPtr The position of the current token |
55
|
|
|
* in the stack passed in $tokens. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function process(File $phpcsFile, $stackPtr) |
60
|
|
|
{ |
61
|
|
|
$tokens = $phpcsFile->getTokens(); |
62
|
|
|
|
63
|
|
|
$else_token = $tokens[$stackPtr]; |
64
|
|
|
$previous_non_blank_token_ptr = $phpcsFile->findPrevious(array(T_WHITESPACE), $stackPtr - 1, null, true); |
65
|
|
|
|
66
|
|
|
if (false === $previous_non_blank_token_ptr) { |
67
|
|
|
// else is no preceded with any symbol, but it is not the responsibility of this sniff. |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$previous_non_blank_token = $tokens[$previous_non_blank_token_ptr]; |
72
|
|
|
if ($previous_non_blank_token['line'] === $else_token['line']) { |
73
|
|
|
$error = '"' . $else_token['content'] . '" should be on a new line.'; |
74
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
}//end process() |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
}//end class |
81
|
|
|
|
82
|
|
|
?> |
|
|
|
|
This check looks for function calls that miss required arguments.