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