|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodeIgniter_Sniffs_Operators_LogicalOperatorAndSniff. |
|
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_Operators_LogicalOperatorAndSniff. |
|
17
|
|
|
* |
|
18
|
|
|
* Ensures that the logical operator 'AND' is in upper case and suggest the use of its symbolic equivalent. |
|
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\Operators; |
|
29
|
|
|
|
|
30
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
|
31
|
|
|
use PHP_CodeSniffer\Files\File; |
|
32
|
|
|
|
|
33
|
|
|
class LogicalOperatorAndSniff implements Sniff |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Returns an array of tokens this test wants to listen for: symbolic and literal operators and. |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
public function register() |
|
41
|
|
|
{ |
|
42
|
|
|
return array( |
|
43
|
|
|
T_LOGICAL_AND, |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
}//end register() |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
51
|
|
|
* |
|
52
|
|
|
* @param File $phpcsFile The current file being scanned. |
|
53
|
|
|
* @param int $stackPtr The position of the current token |
|
54
|
|
|
* in the stack passed in $tokens. |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public function process(File $phpcsFile, $stackPtr) |
|
59
|
|
|
{ |
|
60
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
61
|
|
|
|
|
62
|
|
|
$operator_token = $tokens[$stackPtr]; |
|
63
|
|
|
$operator_string = $operator_token['content']; |
|
64
|
|
|
$operator_code = $operator_token['code']; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
View Code Duplication |
if ($operator_string !== strtoupper($operator_string)) { |
|
|
|
|
|
|
67
|
|
|
$error_message = 'Logical operator should be in upper case;' |
|
68
|
|
|
. ' use "' . strtoupper($operator_string) |
|
69
|
|
|
. '" instead of "' . $operator_string . '"'; |
|
70
|
|
|
$phpcsFile->addError($error_message, $stackPtr, 'LowercaseLogicalOperator'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"'; |
|
74
|
|
|
$phpcsFile->addWarning($warning_message, $stackPtr, 'UseOfLiteralAndOperator'); |
|
75
|
|
|
|
|
76
|
|
|
}//end process() |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
}//end class |
|
80
|
|
|
|
|
81
|
|
|
?> |
|
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.