|
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
|
|
|
/** |
|
14
|
|
|
* PHPCompatibility_Sniffs_PHP_ForbiddenBreakContinueVariableArguments. |
|
15
|
|
|
* |
|
16
|
|
|
* Forbids variable arguments on break or continue statements. |
|
17
|
|
|
* |
|
18
|
|
|
* PHP version 5.4 |
|
19
|
|
|
* |
|
20
|
|
|
* @category PHP |
|
21
|
|
|
* @package PHPCompatibility |
|
22
|
|
|
* @author Wim Godden <[email protected]> |
|
23
|
|
|
* @copyright 2012 Cu.be Solutions bvba |
|
24
|
|
|
*/ |
|
25
|
|
|
class PHPCompatibility_Sniffs_PHP_ForbiddenBreakContinueVariableArgumentsSniff extends PHPCompatibility_Sniff |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Error types this sniff handles for forbidden break/continue arguments. |
|
29
|
|
|
* |
|
30
|
|
|
* Array key is the error code. Array value will be used as part of the error message. |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $errorTypes = array( |
|
35
|
|
|
'variableArgument' => 'a variable argument', |
|
36
|
|
|
'zeroArgument' => '0 as an argument', |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function register() |
|
45
|
|
|
{ |
|
46
|
|
|
return array(T_BREAK, T_CONTINUE); |
|
47
|
|
|
|
|
48
|
|
|
}//end register() |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
52
|
|
|
* |
|
53
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
54
|
|
|
* @param int $stackPtr The position of the current token in the |
|
55
|
|
|
* stack passed in $tokens. |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->supportsAbove('5.4') === false) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
66
|
|
|
$nextSemicolonToken = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr), null, false); |
|
67
|
|
|
$errorType = ''; |
|
68
|
|
|
for ($curToken = $stackPtr + 1; $curToken < $nextSemicolonToken; $curToken++) { |
|
69
|
|
|
if ($tokens[$curToken]['type'] === 'T_STRING') { |
|
70
|
|
|
// If the next non-whitespace token after the string |
|
71
|
|
|
// is an opening parenthesis then it's a function call. |
|
72
|
|
|
$openBracket = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $curToken + 1, null, true); |
|
73
|
|
|
if ($tokens[$openBracket]['code'] === T_OPEN_PARENTHESIS) { |
|
74
|
|
|
$errorType = 'variableArgument'; |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} elseif (in_array($tokens[$curToken]['type'], array('T_VARIABLE', 'T_FUNCTION', 'T_CLOSURE'), true)) { |
|
79
|
|
|
$errorType = 'variableArgument'; |
|
80
|
|
|
break; |
|
81
|
|
|
|
|
82
|
|
|
} elseif ($tokens[$curToken]['type'] === 'T_LNUMBER' && $tokens[$curToken]['content'] === '0') { |
|
83
|
|
|
$errorType = 'zeroArgument'; |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
} elseif ($tokens[$curToken]['type'] === 'T_LNUMBER') { |
|
|
|
|
|
|
87
|
|
|
$errorType = 'variableArgument'; |
|
88
|
|
|
break; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ($errorType !== '') { |
|
|
|
|
|
|
94
|
|
|
$error = 'Using %s on break or continue is forbidden since PHP 5.4'; |
|
95
|
|
|
$errorCode = $errorType.'Found'; |
|
|
|
|
|
|
96
|
|
|
$data = array($this->errorTypes[$errorType]); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$phpcsFile->addError($error, $stackPtr, $errorCode, $data); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
}//end process() |
|
102
|
|
|
|
|
103
|
|
|
}//end class |
|
104
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.