1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* \PHPCompatibility\Sniffs\PHP\ForbiddenBreakContinueVariableArguments. |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
* @author Wim Godden <[email protected]> |
10
|
|
|
* @copyright 2012 Cu.be Solutions bvba |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PHPCompatibility\Sniffs\PHP; |
14
|
|
|
|
15
|
|
|
use PHPCompatibility\Sniff; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* \PHPCompatibility\Sniffs\PHP\ForbiddenBreakContinueVariableArguments. |
19
|
|
|
* |
20
|
|
|
* Forbids variable arguments on break or continue statements. |
21
|
|
|
* |
22
|
|
|
* PHP version 5.4 |
23
|
|
|
* |
24
|
|
|
* @category PHP |
25
|
|
|
* @package PHPCompatibility |
26
|
|
|
* @author Wim Godden <[email protected]> |
27
|
|
|
* @copyright 2012 Cu.be Solutions bvba |
28
|
|
|
*/ |
29
|
|
|
class ForbiddenBreakContinueVariableArgumentsSniff extends Sniff |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Error types this sniff handles for forbidden break/continue arguments. |
33
|
|
|
* |
34
|
|
|
* Array key is the error code. Array value will be used as part of the error message. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $errorTypes = array( |
39
|
|
|
'variableArgument' => 'a variable argument', |
40
|
|
|
'zeroArgument' => '0 as an argument', |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns an array of tokens this test wants to listen for. |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function register() |
49
|
|
|
{ |
50
|
|
|
return array(T_BREAK, T_CONTINUE); |
51
|
|
|
|
52
|
|
|
}//end register() |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Processes this test, when one of its tokens is encountered. |
56
|
|
|
* |
57
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
58
|
|
|
* @param int $stackPtr The position of the current token in the |
59
|
|
|
* stack passed in $tokens. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
64
|
|
|
{ |
65
|
|
|
if ($this->supportsAbove('5.4') === false) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$tokens = $phpcsFile->getTokens(); |
70
|
|
|
$nextSemicolonToken = $phpcsFile->findNext(array(T_SEMICOLON, T_CLOSE_TAG), ($stackPtr), null, false); |
71
|
|
|
$errorType = ''; |
72
|
|
|
for ($curToken = $stackPtr + 1; $curToken < $nextSemicolonToken; $curToken++) { |
73
|
|
|
if ($tokens[$curToken]['type'] === 'T_STRING') { |
74
|
|
|
// If the next non-whitespace token after the string |
75
|
|
|
// is an opening parenthesis then it's a function call. |
76
|
|
|
$openBracket = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, $curToken + 1, null, true); |
77
|
|
|
if ($tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS) { |
78
|
|
|
$errorType = 'variableArgument'; |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} elseif (in_array($tokens[$curToken]['type'], array('T_VARIABLE', 'T_FUNCTION', 'T_CLOSURE'), true)) { |
83
|
|
|
$errorType = 'variableArgument'; |
84
|
|
|
break; |
85
|
|
|
|
86
|
|
|
} elseif ($tokens[$curToken]['type'] === 'T_LNUMBER' && $tokens[$curToken]['content'] === '0') { |
87
|
|
|
$errorType = 'zeroArgument'; |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($errorType !== '') { |
93
|
|
|
$error = 'Using %s on break or continue is forbidden since PHP 5.4'; |
94
|
|
|
$errorCode = $errorType.'Found'; |
95
|
|
|
$data = array($this->errorTypes[$errorType]); |
96
|
|
|
|
97
|
|
|
$phpcsFile->addError($error, $stackPtr, $errorCode, $data); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
}//end process() |
101
|
|
|
|
102
|
|
|
}//end class |
103
|
|
|
|