1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodingStandard_Sniffs_ControlStructures_AssignmentInConditionSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Alexander Obuhovich <[email protected]> |
10
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
11
|
|
|
* @link https://github.com/aik099/CodingStandard |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace CodingStandard\Sniffs\ControlStructures; |
15
|
|
|
|
16
|
|
|
use PHP_CodeSniffer\Files\File; |
17
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Checks that assignments in conditions are not used. |
21
|
|
|
* |
22
|
|
|
* @category PHP |
23
|
|
|
* @package PHP_CodeSniffer |
24
|
|
|
* @author Alexander Obuhovich <[email protected]> |
25
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
26
|
|
|
* @link https://github.com/aik099/CodingStandard |
27
|
|
|
*/ |
28
|
|
|
class AssignmentInConditionSniff implements Sniff |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* A list of tokenizers this sniff supports. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
public $supportedTokenizers = array('PHP'); |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns an array of tokens this test wants to listen for. |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
1 |
|
public function register() |
44
|
|
|
{ |
45
|
|
|
return array( |
46
|
1 |
|
T_IF, |
47
|
1 |
|
T_ELSEIF, |
48
|
1 |
|
); |
49
|
|
|
}//end register() |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Processes this test, when one of its tokens is encountered. |
54
|
|
|
* |
55
|
|
|
* @param File $phpcsFile The file being scanned. |
56
|
|
|
* @param int $stackPtr The position of the current token in the |
57
|
|
|
* stack passed in $tokens. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
1 |
|
public function process(File $phpcsFile, $stackPtr) |
62
|
|
|
{ |
63
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
64
|
|
|
|
65
|
1 |
|
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$equalOperator = $phpcsFile->findNext( |
70
|
1 |
|
T_EQUAL, |
71
|
1 |
|
$tokens[$stackPtr]['parenthesis_opener'], |
72
|
1 |
|
$tokens[$stackPtr]['parenthesis_closer'] |
73
|
1 |
|
); |
74
|
|
|
|
75
|
1 |
|
if ($equalOperator === false) { |
76
|
1 |
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$phpcsFile->addError( |
80
|
1 |
|
'Assignment in "'.$tokens[$stackPtr]['content'].'" control structure is forbidden', |
81
|
1 |
|
$equalOperator, |
82
|
|
|
'Forbidden' |
83
|
1 |
|
); |
84
|
1 |
|
}//end process() |
85
|
|
|
}//end class |
86
|
|
|
|