1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_ForbiddenBreakContinueOutsideLoop. |
4
|
|
|
* |
5
|
|
|
* PHP version 7 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PHPCompatibility_Sniffs_PHP_ForbiddenBreakContinueOutsideLoop. |
14
|
|
|
* |
15
|
|
|
* Forbids use of break or continue statements outside of looping structures. |
16
|
|
|
* |
17
|
|
|
* PHP version 7 |
18
|
|
|
* |
19
|
|
|
* @category PHP |
20
|
|
|
* @package PHPCompatibility |
21
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class PHPCompatibility_Sniffs_PHP_ForbiddenBreakContinueOutsideLoopSniff extends PHPCompatibility_Sniff |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Token codes of control structure in which usage of break/continue is valid. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $validLoopStructures = array( |
32
|
|
|
T_FOR => true, |
33
|
|
|
T_FOREACH => true, |
34
|
|
|
T_WHILE => true, |
35
|
|
|
T_DO => true, |
36
|
|
|
T_SWITCH => true, |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Token codes which did not correctly get a condition assigned in older PHPCS versions. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $backCompat = array( |
45
|
|
|
T_CASE => true, |
46
|
|
|
T_DEFAULT => true, |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns an array of tokens this test wants to listen for. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function register() |
55
|
|
|
{ |
56
|
|
|
return array( |
57
|
|
|
T_BREAK, |
58
|
|
|
T_CONTINUE, |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
}//end register() |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Processes this test, when one of its tokens is encountered. |
65
|
|
|
* |
66
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
67
|
|
|
* @param int $stackPtr The position of the current token in the |
68
|
|
|
* stack passed in $tokens. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
73
|
|
|
{ |
74
|
|
|
$tokens = $phpcsFile->getTokens(); |
75
|
|
|
$token = $tokens[$stackPtr]; |
76
|
|
|
|
77
|
|
|
// Check if the break/continue is within a valid loop structure. |
78
|
|
|
if (empty($token['conditions']) === false) { |
79
|
|
|
foreach ($token['conditions'] as $tokenCode) { |
80
|
|
|
if (isset($this->validLoopStructures[$tokenCode]) === true) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
else { |
86
|
|
|
// Deal with older PHPCS versions. |
87
|
|
|
if (isset($token['scope_condition']) === true && isset($this->backCompat[$tokens[$token['scope_condition']]['code']]) === true) { |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// If we're still here, no valid loop structure container has been found, so throw an error. |
93
|
|
|
$error = "Using '%s' outside of a loop or switch structure is invalid"; |
94
|
|
|
$isError = false; |
95
|
|
|
$data = array( |
96
|
|
|
$token['content'], |
97
|
|
|
); |
98
|
|
|
if ($this->supportsAbove('7.0')) { |
99
|
|
|
$isError = true; |
100
|
|
|
$error .= ' and will throw a fatal error since PHP 7.0'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
if ($isError === true) { |
|
|
|
|
104
|
|
|
$phpcsFile->addError($error, $stackPtr, 'Found', $data); |
105
|
|
|
} else { |
106
|
|
|
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
}//end process() |
110
|
|
|
|
111
|
|
|
}//end class |
112
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.