1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodeIgniter_Sniffs_Operators_UppercaseLogicalOperatorOrSniff. |
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_UppercaseLogicalOperatorOrSniff. |
17
|
|
|
* |
18
|
|
|
* Ensures that the logical operator 'OR' is in upper cases and 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 UppercaseLogicalOperatorOrSniff implements Sniff |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Returns an array of tokens this test wants to listen for: literal and symbolic operators or. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function register() |
41
|
|
|
{ |
42
|
|
|
return array( |
43
|
|
|
T_BOOLEAN_OR, |
44
|
|
|
T_LOGICAL_OR, |
45
|
|
|
); |
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
|
|
|
$operator_token = $tokens[$stackPtr]; |
64
|
|
|
$operator_string = $operator_token['content']; |
65
|
|
|
$operator_code = $operator_token['code']; |
66
|
|
|
|
67
|
|
|
if ($operator_code == T_BOOLEAN_OR) { |
68
|
|
|
$error_message = 'Logical operator "' . $operator_string |
69
|
|
|
. '" is prohibited; use "OR" instead'; |
70
|
|
|
$phpcsFile->addError($error_message, $stackPtr, 'UseOf||InsteadOfOR'); |
71
|
|
|
} |
72
|
|
|
// it is literal, if it is not symbolic |
73
|
|
View Code Duplication |
else if ($operator_string !== strtoupper($operator_string)) { |
|
|
|
|
74
|
|
|
$error_message = 'Logical operator should be in upper case;' |
75
|
|
|
. ' use "' . strtoupper($operator_string) |
76
|
|
|
. '" instead of "' . $operator_string . '"'; |
77
|
|
|
$phpcsFile->addError($error_message, $stackPtr, 'UseOfLowercaseOr'); |
78
|
|
|
} |
79
|
|
|
}//end process() |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
}//end class |
83
|
|
|
|
84
|
|
|
?> |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.