Completed
Branch fix-message-active-toggle (c2556b)
by
unknown
40:04 queued 29:40
created
src/Standards/Generic/Sniffs/CodeAnalysis/UnnecessaryFinalModifierSniff.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -30,56 +30,56 @@
 block discarded – undo
30 30
 {
31 31
 
32 32
 
33
-    /**
34
-     * Registers the tokens that this sniff wants to listen for.
35
-     *
36
-     * @return int[]
37
-     */
38
-    public function register()
39
-    {
40
-        return [T_CLASS];
41
-
42
-    }//end register()
43
-
44
-
45
-    /**
46
-     * Processes this test, when one of its tokens is encountered.
47
-     *
48
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
49
-     * @param int                         $stackPtr  The position of the current token
50
-     *                                               in the stack passed in $tokens.
51
-     *
52
-     * @return void
53
-     */
54
-    public function process(File $phpcsFile, $stackPtr)
55
-    {
56
-        $tokens = $phpcsFile->getTokens();
57
-        $token  = $tokens[$stackPtr];
58
-
59
-        // Skip for-statements without body.
60
-        if (isset($token['scope_opener']) === false) {
61
-            return;
62
-        }
63
-
64
-        // Fetch previous token.
65
-        $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
66
-
67
-        // Skip for non final class.
68
-        if ($prev === false || $tokens[$prev]['code'] !== T_FINAL) {
69
-            return;
70
-        }
71
-
72
-        $next = ++$token['scope_opener'];
73
-        $end  = --$token['scope_closer'];
74
-
75
-        for (; $next <= $end; ++$next) {
76
-            if ($tokens[$next]['code'] === T_FINAL) {
77
-                $error = 'Unnecessary FINAL modifier in FINAL class';
78
-                $phpcsFile->addWarning($error, $next, 'Found');
79
-            }
80
-        }
81
-
82
-    }//end process()
33
+	/**
34
+	 * Registers the tokens that this sniff wants to listen for.
35
+	 *
36
+	 * @return int[]
37
+	 */
38
+	public function register()
39
+	{
40
+		return [T_CLASS];
41
+
42
+	}//end register()
43
+
44
+
45
+	/**
46
+	 * Processes this test, when one of its tokens is encountered.
47
+	 *
48
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
49
+	 * @param int                         $stackPtr  The position of the current token
50
+	 *                                               in the stack passed in $tokens.
51
+	 *
52
+	 * @return void
53
+	 */
54
+	public function process(File $phpcsFile, $stackPtr)
55
+	{
56
+		$tokens = $phpcsFile->getTokens();
57
+		$token  = $tokens[$stackPtr];
58
+
59
+		// Skip for-statements without body.
60
+		if (isset($token['scope_opener']) === false) {
61
+			return;
62
+		}
63
+
64
+		// Fetch previous token.
65
+		$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
66
+
67
+		// Skip for non final class.
68
+		if ($prev === false || $tokens[$prev]['code'] !== T_FINAL) {
69
+			return;
70
+		}
71
+
72
+		$next = ++$token['scope_opener'];
73
+		$end  = --$token['scope_closer'];
74
+
75
+		for (; $next <= $end; ++$next) {
76
+			if ($tokens[$next]['code'] === T_FINAL) {
77
+				$error = 'Unnecessary FINAL modifier in FINAL class';
78
+				$phpcsFile->addWarning($error, $next, 'Found');
79
+			}
80
+		}
81
+
82
+	}//end process()
83 83
 
84 84
 
85 85
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php 1 patch
Indentation   +93 added lines, -93 removed lines patch added patch discarded remove patch
@@ -36,99 +36,99 @@
 block discarded – undo
36 36
 {
37 37
 
38 38
 
39
-    /**
40
-     * Registers the tokens that this sniff wants to listen for.
41
-     *
42
-     * @return int[]
43
-     */
44
-    public function register()
45
-    {
46
-        return [T_FOR];
47
-
48
-    }//end register()
49
-
50
-
51
-    /**
52
-     * Processes this test, when one of its tokens is encountered.
53
-     *
54
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
55
-     * @param int                         $stackPtr  The position of the current token
56
-     *                                               in the stack passed in $tokens.
57
-     *
58
-     * @return void
59
-     */
60
-    public function process(File $phpcsFile, $stackPtr)
61
-    {
62
-        $tokens = $phpcsFile->getTokens();
63
-        $token  = $tokens[$stackPtr];
64
-
65
-        // Skip for-loop without body.
66
-        if (isset($token['scope_opener']) === false) {
67
-            return;
68
-        }
69
-
70
-        // Find incrementors for outer loop.
71
-        $outer = $this->findIncrementers($tokens, $token);
72
-
73
-        // Skip if empty.
74
-        if (count($outer) === 0) {
75
-            return;
76
-        }
77
-
78
-        // Find nested for loops.
79
-        $start = ++$token['scope_opener'];
80
-        $end   = --$token['scope_closer'];
81
-
82
-        for (; $start <= $end; ++$start) {
83
-            if ($tokens[$start]['code'] !== T_FOR) {
84
-                continue;
85
-            }
86
-
87
-            $inner = $this->findIncrementers($tokens, $tokens[$start]);
88
-            $diff  = array_intersect($outer, $inner);
89
-
90
-            if (count($diff) !== 0) {
91
-                $error = 'Loop incrementor (%s) jumbling with inner loop';
92
-                $data  = [join(', ', $diff)];
93
-                $phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
94
-            }
95
-        }
96
-
97
-    }//end process()
98
-
99
-
100
-    /**
101
-     * Get all used variables in the incrementer part of a for statement.
102
-     *
103
-     * @param array(integer=>array) $tokens Array with all code sniffer tokens.
104
-     * @param array(string=>mixed)  $token  Current for loop token
105
-     *
106
-     * @return string[] List of all found incrementer variables.
107
-     */
108
-    protected function findIncrementers(array $tokens, array $token)
109
-    {
110
-        // Skip invalid statement.
111
-        if (isset($token['parenthesis_opener']) === false) {
112
-            return [];
113
-        }
114
-
115
-        $start = ++$token['parenthesis_opener'];
116
-        $end   = --$token['parenthesis_closer'];
117
-
118
-        $incrementers = [];
119
-        $semicolons   = 0;
120
-        for ($next = $start; $next <= $end; ++$next) {
121
-            $code = $tokens[$next]['code'];
122
-            if ($code === T_SEMICOLON) {
123
-                ++$semicolons;
124
-            } else if ($semicolons === 2 && $code === T_VARIABLE) {
125
-                $incrementers[] = $tokens[$next]['content'];
126
-            }
127
-        }
128
-
129
-        return $incrementers;
130
-
131
-    }//end findIncrementers()
39
+	/**
40
+	 * Registers the tokens that this sniff wants to listen for.
41
+	 *
42
+	 * @return int[]
43
+	 */
44
+	public function register()
45
+	{
46
+		return [T_FOR];
47
+
48
+	}//end register()
49
+
50
+
51
+	/**
52
+	 * Processes this test, when one of its tokens is encountered.
53
+	 *
54
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
55
+	 * @param int                         $stackPtr  The position of the current token
56
+	 *                                               in the stack passed in $tokens.
57
+	 *
58
+	 * @return void
59
+	 */
60
+	public function process(File $phpcsFile, $stackPtr)
61
+	{
62
+		$tokens = $phpcsFile->getTokens();
63
+		$token  = $tokens[$stackPtr];
64
+
65
+		// Skip for-loop without body.
66
+		if (isset($token['scope_opener']) === false) {
67
+			return;
68
+		}
69
+
70
+		// Find incrementors for outer loop.
71
+		$outer = $this->findIncrementers($tokens, $token);
72
+
73
+		// Skip if empty.
74
+		if (count($outer) === 0) {
75
+			return;
76
+		}
77
+
78
+		// Find nested for loops.
79
+		$start = ++$token['scope_opener'];
80
+		$end   = --$token['scope_closer'];
81
+
82
+		for (; $start <= $end; ++$start) {
83
+			if ($tokens[$start]['code'] !== T_FOR) {
84
+				continue;
85
+			}
86
+
87
+			$inner = $this->findIncrementers($tokens, $tokens[$start]);
88
+			$diff  = array_intersect($outer, $inner);
89
+
90
+			if (count($diff) !== 0) {
91
+				$error = 'Loop incrementor (%s) jumbling with inner loop';
92
+				$data  = [join(', ', $diff)];
93
+				$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
94
+			}
95
+		}
96
+
97
+	}//end process()
98
+
99
+
100
+	/**
101
+	 * Get all used variables in the incrementer part of a for statement.
102
+	 *
103
+	 * @param array(integer=>array) $tokens Array with all code sniffer tokens.
104
+	 * @param array(string=>mixed)  $token  Current for loop token
105
+	 *
106
+	 * @return string[] List of all found incrementer variables.
107
+	 */
108
+	protected function findIncrementers(array $tokens, array $token)
109
+	{
110
+		// Skip invalid statement.
111
+		if (isset($token['parenthesis_opener']) === false) {
112
+			return [];
113
+		}
114
+
115
+		$start = ++$token['parenthesis_opener'];
116
+		$end   = --$token['parenthesis_closer'];
117
+
118
+		$incrementers = [];
119
+		$semicolons   = 0;
120
+		for ($next = $start; $next <= $end; ++$next) {
121
+			$code = $tokens[$next]['code'];
122
+			if ($code === T_SEMICOLON) {
123
+				++$semicolons;
124
+			} else if ($semicolons === 2 && $code === T_VARIABLE) {
125
+				$incrementers[] = $tokens[$next]['content'];
126
+			}
127
+		}
128
+
129
+		return $incrementers;
130
+
131
+	}//end findIncrementers()
132 132
 
133 133
 
134 134
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php 1 patch
Indentation   +127 added lines, -127 removed lines patch added patch discarded remove patch
@@ -29,133 +29,133 @@
 block discarded – undo
29 29
 {
30 30
 
31 31
 
32
-    /**
33
-     * Registers the tokens that this sniff wants to listen for.
34
-     *
35
-     * @return int[]
36
-     */
37
-    public function register()
38
-    {
39
-        return [T_FUNCTION];
40
-
41
-    }//end register()
42
-
43
-
44
-    /**
45
-     * Processes this test, when one of its tokens is encountered.
46
-     *
47
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
48
-     * @param int                         $stackPtr  The position of the current token
49
-     *                                               in the stack passed in $tokens.
50
-     *
51
-     * @return void
52
-     */
53
-    public function process(File $phpcsFile, $stackPtr)
54
-    {
55
-        $tokens = $phpcsFile->getTokens();
56
-        $token  = $tokens[$stackPtr];
57
-
58
-        // Skip function without body.
59
-        if (isset($token['scope_opener']) === false) {
60
-            return;
61
-        }
62
-
63
-        // Get function name.
64
-        $methodName = $phpcsFile->getDeclarationName($stackPtr);
65
-
66
-        // Get all parameters from method signature.
67
-        $signature = [];
68
-        foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
69
-            $signature[] = $param['name'];
70
-        }
71
-
72
-        $next = ++$token['scope_opener'];
73
-        $end  = --$token['scope_closer'];
74
-
75
-        for (; $next <= $end; ++$next) {
76
-            $code = $tokens[$next]['code'];
77
-
78
-            if (isset(Tokens::$emptyTokens[$code]) === true) {
79
-                continue;
80
-            } else if ($code === T_RETURN) {
81
-                continue;
82
-            }
83
-
84
-            break;
85
-        }
86
-
87
-        // Any token except 'parent' indicates correct code.
88
-        if ($tokens[$next]['code'] !== T_PARENT) {
89
-            return;
90
-        }
91
-
92
-        // Find next non empty token index, should be double colon.
93
-        $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
94
-
95
-        // Skip for invalid code.
96
-        if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) {
97
-            return;
98
-        }
99
-
100
-        // Find next non empty token index, should be the function name.
101
-        $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
102
-
103
-        // Skip for invalid code or other method.
104
-        if ($next === false || $tokens[$next]['content'] !== $methodName) {
105
-            return;
106
-        }
107
-
108
-        // Find next non empty token index, should be the open parenthesis.
109
-        $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
110
-
111
-        // Skip for invalid code.
112
-        if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
113
-            return;
114
-        }
115
-
116
-        $parameters       = [''];
117
-        $parenthesisCount = 1;
118
-        $count            = count($tokens);
119
-        for (++$next; $next < $count; ++$next) {
120
-            $code = $tokens[$next]['code'];
121
-
122
-            if ($code === T_OPEN_PARENTHESIS) {
123
-                ++$parenthesisCount;
124
-            } else if ($code === T_CLOSE_PARENTHESIS) {
125
-                --$parenthesisCount;
126
-            } else if ($parenthesisCount === 1 && $code === T_COMMA) {
127
-                $parameters[] = '';
128
-            } else if (isset(Tokens::$emptyTokens[$code]) === false) {
129
-                $parameters[(count($parameters) - 1)] .= $tokens[$next]['content'];
130
-            }
131
-
132
-            if ($parenthesisCount === 0) {
133
-                break;
134
-            }
135
-        }//end for
136
-
137
-        $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
138
-        if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) {
139
-            return;
140
-        }
141
-
142
-        // Check rest of the scope.
143
-        for (++$next; $next <= $end; ++$next) {
144
-            $code = $tokens[$next]['code'];
145
-            // Skip for any other content.
146
-            if (isset(Tokens::$emptyTokens[$code]) === false) {
147
-                return;
148
-            }
149
-        }
150
-
151
-        $parameters = array_map('trim', $parameters);
152
-        $parameters = array_filter($parameters);
153
-
154
-        if (count($parameters) === count($signature) && $parameters === $signature) {
155
-            $phpcsFile->addWarning('Possible useless method overriding detected', $stackPtr, 'Found');
156
-        }
157
-
158
-    }//end process()
32
+	/**
33
+	 * Registers the tokens that this sniff wants to listen for.
34
+	 *
35
+	 * @return int[]
36
+	 */
37
+	public function register()
38
+	{
39
+		return [T_FUNCTION];
40
+
41
+	}//end register()
42
+
43
+
44
+	/**
45
+	 * Processes this test, when one of its tokens is encountered.
46
+	 *
47
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
48
+	 * @param int                         $stackPtr  The position of the current token
49
+	 *                                               in the stack passed in $tokens.
50
+	 *
51
+	 * @return void
52
+	 */
53
+	public function process(File $phpcsFile, $stackPtr)
54
+	{
55
+		$tokens = $phpcsFile->getTokens();
56
+		$token  = $tokens[$stackPtr];
57
+
58
+		// Skip function without body.
59
+		if (isset($token['scope_opener']) === false) {
60
+			return;
61
+		}
62
+
63
+		// Get function name.
64
+		$methodName = $phpcsFile->getDeclarationName($stackPtr);
65
+
66
+		// Get all parameters from method signature.
67
+		$signature = [];
68
+		foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
69
+			$signature[] = $param['name'];
70
+		}
71
+
72
+		$next = ++$token['scope_opener'];
73
+		$end  = --$token['scope_closer'];
74
+
75
+		for (; $next <= $end; ++$next) {
76
+			$code = $tokens[$next]['code'];
77
+
78
+			if (isset(Tokens::$emptyTokens[$code]) === true) {
79
+				continue;
80
+			} else if ($code === T_RETURN) {
81
+				continue;
82
+			}
83
+
84
+			break;
85
+		}
86
+
87
+		// Any token except 'parent' indicates correct code.
88
+		if ($tokens[$next]['code'] !== T_PARENT) {
89
+			return;
90
+		}
91
+
92
+		// Find next non empty token index, should be double colon.
93
+		$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
94
+
95
+		// Skip for invalid code.
96
+		if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) {
97
+			return;
98
+		}
99
+
100
+		// Find next non empty token index, should be the function name.
101
+		$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
102
+
103
+		// Skip for invalid code or other method.
104
+		if ($next === false || $tokens[$next]['content'] !== $methodName) {
105
+			return;
106
+		}
107
+
108
+		// Find next non empty token index, should be the open parenthesis.
109
+		$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
110
+
111
+		// Skip for invalid code.
112
+		if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
113
+			return;
114
+		}
115
+
116
+		$parameters       = [''];
117
+		$parenthesisCount = 1;
118
+		$count            = count($tokens);
119
+		for (++$next; $next < $count; ++$next) {
120
+			$code = $tokens[$next]['code'];
121
+
122
+			if ($code === T_OPEN_PARENTHESIS) {
123
+				++$parenthesisCount;
124
+			} else if ($code === T_CLOSE_PARENTHESIS) {
125
+				--$parenthesisCount;
126
+			} else if ($parenthesisCount === 1 && $code === T_COMMA) {
127
+				$parameters[] = '';
128
+			} else if (isset(Tokens::$emptyTokens[$code]) === false) {
129
+				$parameters[(count($parameters) - 1)] .= $tokens[$next]['content'];
130
+			}
131
+
132
+			if ($parenthesisCount === 0) {
133
+				break;
134
+			}
135
+		}//end for
136
+
137
+		$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
138
+		if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) {
139
+			return;
140
+		}
141
+
142
+		// Check rest of the scope.
143
+		for (++$next; $next <= $end; ++$next) {
144
+			$code = $tokens[$next]['code'];
145
+			// Skip for any other content.
146
+			if (isset(Tokens::$emptyTokens[$code]) === false) {
147
+				return;
148
+			}
149
+		}
150
+
151
+		$parameters = array_map('trim', $parameters);
152
+		$parameters = array_filter($parameters);
153
+
154
+		if (count($parameters) === count($signature) && $parameters === $signature) {
155
+			$phpcsFile->addWarning('Possible useless method overriding detected', $stackPtr, 'Found');
156
+		}
157
+
158
+	}//end process()
159 159
 
160 160
 
161 161
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/CodeAnalysis/AssignmentInConditionSniff.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -158,7 +158,7 @@
 block discarded – undo
158 158
             }
159 159
 
160 160
             $startPos = $hasAssignment;
161
-        } while ($startPos < $closer);
161
+        }while ($startPos < $closer);
162 162
 
163 163
     }//end process()
164 164
 
Please login to merge, or discard this patch.
Indentation   +146 added lines, -146 removed lines patch added patch discarded remove patch
@@ -20,152 +20,152 @@
 block discarded – undo
20 20
 class AssignmentInConditionSniff implements Sniff
21 21
 {
22 22
 
23
-    /**
24
-     * Assignment tokens to trigger on.
25
-     *
26
-     * Set in the register() method.
27
-     *
28
-     * @var array
29
-     */
30
-    protected $assignmentTokens = [];
31
-
32
-    /**
33
-     * The tokens that indicate the start of a condition.
34
-     *
35
-     * @var array
36
-     */
37
-    protected $conditionStartTokens = [];
38
-
39
-
40
-    /**
41
-     * Registers the tokens that this sniff wants to listen for.
42
-     *
43
-     * @return int[]
44
-     */
45
-    public function register()
46
-    {
47
-        $this->assignmentTokens = Tokens::$assignmentTokens;
48
-        unset($this->assignmentTokens[T_DOUBLE_ARROW]);
49
-
50
-        $starters = Tokens::$booleanOperators;
51
-        $starters[T_SEMICOLON]        = T_SEMICOLON;
52
-        $starters[T_OPEN_PARENTHESIS] = T_OPEN_PARENTHESIS;
53
-
54
-        $this->conditionStartTokens = $starters;
55
-
56
-        return [
57
-            T_IF,
58
-            T_ELSEIF,
59
-            T_FOR,
60
-            T_SWITCH,
61
-            T_CASE,
62
-            T_WHILE,
63
-            T_MATCH,
64
-        ];
65
-
66
-    }//end register()
67
-
68
-
69
-    /**
70
-     * Processes this test, when one of its tokens is encountered.
71
-     *
72
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
73
-     * @param int                         $stackPtr  The position of the current token
74
-     *                                               in the stack passed in $tokens.
75
-     *
76
-     * @return void
77
-     */
78
-    public function process(File $phpcsFile, $stackPtr)
79
-    {
80
-        $tokens = $phpcsFile->getTokens();
81
-        $token  = $tokens[$stackPtr];
82
-
83
-        // Find the condition opener/closer.
84
-        if ($token['code'] === T_FOR) {
85
-            if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
86
-                return;
87
-            }
88
-
89
-            $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($token['parenthesis_opener'] + 1), ($token['parenthesis_closer']));
90
-            if ($semicolon === false) {
91
-                return;
92
-            }
93
-
94
-            $opener = $semicolon;
95
-
96
-            $semicolon = $phpcsFile->findNext(T_SEMICOLON, ($opener + 1), ($token['parenthesis_closer']));
97
-            if ($semicolon === false) {
98
-                return;
99
-            }
100
-
101
-            $closer = $semicolon;
102
-            unset($semicolon);
103
-        } else if ($token['code'] === T_CASE) {
104
-            if (isset($token['scope_opener']) === false) {
105
-                return;
106
-            }
107
-
108
-            $opener = $stackPtr;
109
-            $closer = $token['scope_opener'];
110
-        } else {
111
-            if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
112
-                return;
113
-            }
114
-
115
-            $opener = $token['parenthesis_opener'];
116
-            $closer = $token['parenthesis_closer'];
117
-        }//end if
118
-
119
-        $startPos = $opener;
120
-
121
-        do {
122
-            $hasAssignment = $phpcsFile->findNext($this->assignmentTokens, ($startPos + 1), $closer);
123
-            if ($hasAssignment === false) {
124
-                return;
125
-            }
126
-
127
-            // Examine whether the left side is a variable.
128
-            $hasVariable       = false;
129
-            $conditionStart    = $startPos;
130
-            $altConditionStart = $phpcsFile->findPrevious($this->conditionStartTokens, ($hasAssignment - 1), $startPos);
131
-            if ($altConditionStart !== false) {
132
-                $conditionStart = $altConditionStart;
133
-            }
134
-
135
-            for ($i = $hasAssignment; $i > $conditionStart; $i--) {
136
-                if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
137
-                    continue;
138
-                }
139
-
140
-                // If this is a variable or array, we've seen all we need to see.
141
-                if ($tokens[$i]['code'] === T_VARIABLE || $tokens[$i]['code'] === T_CLOSE_SQUARE_BRACKET) {
142
-                    $hasVariable = true;
143
-                    break;
144
-                }
145
-
146
-                // If this is a function call or something, we are OK.
147
-                if ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS) {
148
-                    break;
149
-                }
150
-            }
151
-
152
-            if ($hasVariable === true) {
153
-                $errorCode = 'Found';
154
-                if ($token['code'] === T_WHILE) {
155
-                    $errorCode = 'FoundInWhileCondition';
156
-                }
157
-
158
-                $phpcsFile->addWarning(
159
-                    'Variable assignment found within a condition. Did you mean to do a comparison ?',
160
-                    $hasAssignment,
161
-                    $errorCode
162
-                );
163
-            }
164
-
165
-            $startPos = $hasAssignment;
166
-        } while ($startPos < $closer);
167
-
168
-    }//end process()
23
+	/**
24
+	 * Assignment tokens to trigger on.
25
+	 *
26
+	 * Set in the register() method.
27
+	 *
28
+	 * @var array
29
+	 */
30
+	protected $assignmentTokens = [];
31
+
32
+	/**
33
+	 * The tokens that indicate the start of a condition.
34
+	 *
35
+	 * @var array
36
+	 */
37
+	protected $conditionStartTokens = [];
38
+
39
+
40
+	/**
41
+	 * Registers the tokens that this sniff wants to listen for.
42
+	 *
43
+	 * @return int[]
44
+	 */
45
+	public function register()
46
+	{
47
+		$this->assignmentTokens = Tokens::$assignmentTokens;
48
+		unset($this->assignmentTokens[T_DOUBLE_ARROW]);
49
+
50
+		$starters = Tokens::$booleanOperators;
51
+		$starters[T_SEMICOLON]        = T_SEMICOLON;
52
+		$starters[T_OPEN_PARENTHESIS] = T_OPEN_PARENTHESIS;
53
+
54
+		$this->conditionStartTokens = $starters;
55
+
56
+		return [
57
+			T_IF,
58
+			T_ELSEIF,
59
+			T_FOR,
60
+			T_SWITCH,
61
+			T_CASE,
62
+			T_WHILE,
63
+			T_MATCH,
64
+		];
65
+
66
+	}//end register()
67
+
68
+
69
+	/**
70
+	 * Processes this test, when one of its tokens is encountered.
71
+	 *
72
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
73
+	 * @param int                         $stackPtr  The position of the current token
74
+	 *                                               in the stack passed in $tokens.
75
+	 *
76
+	 * @return void
77
+	 */
78
+	public function process(File $phpcsFile, $stackPtr)
79
+	{
80
+		$tokens = $phpcsFile->getTokens();
81
+		$token  = $tokens[$stackPtr];
82
+
83
+		// Find the condition opener/closer.
84
+		if ($token['code'] === T_FOR) {
85
+			if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
86
+				return;
87
+			}
88
+
89
+			$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($token['parenthesis_opener'] + 1), ($token['parenthesis_closer']));
90
+			if ($semicolon === false) {
91
+				return;
92
+			}
93
+
94
+			$opener = $semicolon;
95
+
96
+			$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($opener + 1), ($token['parenthesis_closer']));
97
+			if ($semicolon === false) {
98
+				return;
99
+			}
100
+
101
+			$closer = $semicolon;
102
+			unset($semicolon);
103
+		} else if ($token['code'] === T_CASE) {
104
+			if (isset($token['scope_opener']) === false) {
105
+				return;
106
+			}
107
+
108
+			$opener = $stackPtr;
109
+			$closer = $token['scope_opener'];
110
+		} else {
111
+			if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
112
+				return;
113
+			}
114
+
115
+			$opener = $token['parenthesis_opener'];
116
+			$closer = $token['parenthesis_closer'];
117
+		}//end if
118
+
119
+		$startPos = $opener;
120
+
121
+		do {
122
+			$hasAssignment = $phpcsFile->findNext($this->assignmentTokens, ($startPos + 1), $closer);
123
+			if ($hasAssignment === false) {
124
+				return;
125
+			}
126
+
127
+			// Examine whether the left side is a variable.
128
+			$hasVariable       = false;
129
+			$conditionStart    = $startPos;
130
+			$altConditionStart = $phpcsFile->findPrevious($this->conditionStartTokens, ($hasAssignment - 1), $startPos);
131
+			if ($altConditionStart !== false) {
132
+				$conditionStart = $altConditionStart;
133
+			}
134
+
135
+			for ($i = $hasAssignment; $i > $conditionStart; $i--) {
136
+				if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === true) {
137
+					continue;
138
+				}
139
+
140
+				// If this is a variable or array, we've seen all we need to see.
141
+				if ($tokens[$i]['code'] === T_VARIABLE || $tokens[$i]['code'] === T_CLOSE_SQUARE_BRACKET) {
142
+					$hasVariable = true;
143
+					break;
144
+				}
145
+
146
+				// If this is a function call or something, we are OK.
147
+				if ($tokens[$i]['code'] === T_CLOSE_PARENTHESIS) {
148
+					break;
149
+				}
150
+			}
151
+
152
+			if ($hasVariable === true) {
153
+				$errorCode = 'Found';
154
+				if ($token['code'] === T_WHILE) {
155
+					$errorCode = 'FoundInWhileCondition';
156
+				}
157
+
158
+				$phpcsFile->addWarning(
159
+					'Variable assignment found within a condition. Did you mean to do a comparison ?',
160
+					$hasAssignment,
161
+					$errorCode
162
+				);
163
+			}
164
+
165
+			$startPos = $hasAssignment;
166
+		} while ($startPos < $closer);
167
+
168
+	}//end process()
169 169
 
170 170
 
171 171
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php 1 patch
Indentation   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -30,62 +30,62 @@
 block discarded – undo
30 30
 {
31 31
 
32 32
 
33
-    /**
34
-     * Registers the tokens that this sniff wants to listen for.
35
-     *
36
-     * @return int[]
37
-     */
38
-    public function register()
39
-    {
40
-        return [T_FOR];
41
-
42
-    }//end register()
43
-
44
-
45
-    /**
46
-     * Processes this test, when one of its tokens is encountered.
47
-     *
48
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
49
-     * @param int                         $stackPtr  The position of the current token
50
-     *                                               in the stack passed in $tokens.
51
-     *
52
-     * @return void
53
-     */
54
-    public function process(File $phpcsFile, $stackPtr)
55
-    {
56
-        $tokens = $phpcsFile->getTokens();
57
-        $token  = $tokens[$stackPtr];
58
-
59
-        // Skip invalid statement.
60
-        if (isset($token['parenthesis_opener']) === false) {
61
-            return;
62
-        }
63
-
64
-        $next = ++$token['parenthesis_opener'];
65
-        $end  = --$token['parenthesis_closer'];
66
-
67
-        $parts = [
68
-            0,
69
-            0,
70
-            0,
71
-        ];
72
-        $index = 0;
73
-
74
-        for (; $next <= $end; ++$next) {
75
-            $code = $tokens[$next]['code'];
76
-            if ($code === T_SEMICOLON) {
77
-                ++$index;
78
-            } else if (isset(Tokens::$emptyTokens[$code]) === false) {
79
-                ++$parts[$index];
80
-            }
81
-        }
82
-
83
-        if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
84
-            $error = 'This FOR loop can be simplified to a WHILE loop';
85
-            $phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
86
-        }
87
-
88
-    }//end process()
33
+	/**
34
+	 * Registers the tokens that this sniff wants to listen for.
35
+	 *
36
+	 * @return int[]
37
+	 */
38
+	public function register()
39
+	{
40
+		return [T_FOR];
41
+
42
+	}//end register()
43
+
44
+
45
+	/**
46
+	 * Processes this test, when one of its tokens is encountered.
47
+	 *
48
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
49
+	 * @param int                         $stackPtr  The position of the current token
50
+	 *                                               in the stack passed in $tokens.
51
+	 *
52
+	 * @return void
53
+	 */
54
+	public function process(File $phpcsFile, $stackPtr)
55
+	{
56
+		$tokens = $phpcsFile->getTokens();
57
+		$token  = $tokens[$stackPtr];
58
+
59
+		// Skip invalid statement.
60
+		if (isset($token['parenthesis_opener']) === false) {
61
+			return;
62
+		}
63
+
64
+		$next = ++$token['parenthesis_opener'];
65
+		$end  = --$token['parenthesis_closer'];
66
+
67
+		$parts = [
68
+			0,
69
+			0,
70
+			0,
71
+		];
72
+		$index = 0;
73
+
74
+		for (; $next <= $end; ++$next) {
75
+			$code = $tokens[$next]['code'];
76
+			if ($code === T_SEMICOLON) {
77
+				++$index;
78
+			} else if (isset(Tokens::$emptyTokens[$code]) === false) {
79
+				++$parts[$index];
80
+			}
81
+		}
82
+
83
+		if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
84
+			$error = 'This FOR loop can be simplified to a WHILE loop';
85
+			$phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
86
+		}
87
+
88
+	}//end process()
89 89
 
90 90
 
91 91
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/PHP/DeprecatedFunctionsSniff.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -58,7 +58,7 @@
 block discarded – undo
58 58
      *
59 59
      * @return void
60 60
      */
61
-    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
61
+    protected function addError($phpcsFile, $stackPtr, $function, $pattern = null)
62 62
     {
63 63
         $data  = [$function];
64 64
         $error = 'Function %s() has been deprecated';
Please login to merge, or discard this patch.
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -13,61 +13,61 @@
 block discarded – undo
13 13
 class DeprecatedFunctionsSniff extends ForbiddenFunctionsSniff
14 14
 {
15 15
 
16
-    /**
17
-     * A list of forbidden functions with their alternatives.
18
-     *
19
-     * The value is NULL if no alternative exists. IE, the
20
-     * function should just not be used.
21
-     *
22
-     * @var array<string, string|null>
23
-     */
24
-    public $forbiddenFunctions = [];
16
+	/**
17
+	 * A list of forbidden functions with their alternatives.
18
+	 *
19
+	 * The value is NULL if no alternative exists. IE, the
20
+	 * function should just not be used.
21
+	 *
22
+	 * @var array<string, string|null>
23
+	 */
24
+	public $forbiddenFunctions = [];
25 25
 
26 26
 
27
-    /**
28
-     * Constructor.
29
-     *
30
-     * Uses the Reflection API to get a list of deprecated functions.
31
-     */
32
-    public function __construct()
33
-    {
34
-        $functions = get_defined_functions();
27
+	/**
28
+	 * Constructor.
29
+	 *
30
+	 * Uses the Reflection API to get a list of deprecated functions.
31
+	 */
32
+	public function __construct()
33
+	{
34
+		$functions = get_defined_functions();
35 35
 
36
-        foreach ($functions['internal'] as $functionName) {
37
-            $function = new \ReflectionFunction($functionName);
36
+		foreach ($functions['internal'] as $functionName) {
37
+			$function = new \ReflectionFunction($functionName);
38 38
 
39
-            if ($function->isDeprecated() === true) {
40
-                $this->forbiddenFunctions[$functionName] = null;
41
-            }
42
-        }
39
+			if ($function->isDeprecated() === true) {
40
+				$this->forbiddenFunctions[$functionName] = null;
41
+			}
42
+		}
43 43
 
44
-    }//end __construct()
44
+	}//end __construct()
45 45
 
46 46
 
47
-    /**
48
-     * Generates the error or warning for this sniff.
49
-     *
50
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
51
-     * @param int                         $stackPtr  The position of the forbidden function
52
-     *                                               in the token array.
53
-     * @param string                      $function  The name of the forbidden function.
54
-     * @param string                      $pattern   The pattern used for the match.
55
-     *
56
-     * @return void
57
-     */
58
-    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
59
-    {
60
-        $data  = [$function];
61
-        $error = 'Function %s() has been deprecated';
62
-        $type  = 'Deprecated';
47
+	/**
48
+	 * Generates the error or warning for this sniff.
49
+	 *
50
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
51
+	 * @param int                         $stackPtr  The position of the forbidden function
52
+	 *                                               in the token array.
53
+	 * @param string                      $function  The name of the forbidden function.
54
+	 * @param string                      $pattern   The pattern used for the match.
55
+	 *
56
+	 * @return void
57
+	 */
58
+	protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
59
+	{
60
+		$data  = [$function];
61
+		$error = 'Function %s() has been deprecated';
62
+		$type  = 'Deprecated';
63 63
 
64
-        if ($this->error === true) {
65
-            $phpcsFile->addError($error, $stackPtr, $type, $data);
66
-        } else {
67
-            $phpcsFile->addWarning($error, $stackPtr, $type, $data);
68
-        }
64
+		if ($this->error === true) {
65
+			$phpcsFile->addError($error, $stackPtr, $type, $data);
66
+		} else {
67
+			$phpcsFile->addWarning($error, $stackPtr, $type, $data);
68
+		}
69 69
 
70
-    }//end addError()
70
+	}//end addError()
71 71
 
72 72
 
73 73
 }//end class
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/Generic/Sniffs/PHP/BacktickOperatorSniff.php 1 patch
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -16,33 +16,33 @@
 block discarded – undo
16 16
 {
17 17
 
18 18
 
19
-    /**
20
-     * Returns an array of tokens this test wants to listen for.
21
-     *
22
-     * @return array
23
-     */
24
-    public function register()
25
-    {
26
-        return [T_BACKTICK];
27
-
28
-    }//end register()
29
-
30
-
31
-    /**
32
-     * Processes this sniff, when one of its tokens is encountered.
33
-     *
34
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35
-     * @param int                         $stackPtr  The position of the current token in
36
-     *                                               the stack passed in $tokens.
37
-     *
38
-     * @return void
39
-     */
40
-    public function process(File $phpcsFile, $stackPtr)
41
-    {
42
-        $error = 'Use of the backtick operator is forbidden';
43
-        $phpcsFile->addError($error, $stackPtr, 'Found');
44
-
45
-    }//end process()
19
+	/**
20
+	 * Returns an array of tokens this test wants to listen for.
21
+	 *
22
+	 * @return array
23
+	 */
24
+	public function register()
25
+	{
26
+		return [T_BACKTICK];
27
+
28
+	}//end register()
29
+
30
+
31
+	/**
32
+	 * Processes this sniff, when one of its tokens is encountered.
33
+	 *
34
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35
+	 * @param int                         $stackPtr  The position of the current token in
36
+	 *                                               the stack passed in $tokens.
37
+	 *
38
+	 * @return void
39
+	 */
40
+	public function process(File $phpcsFile, $stackPtr)
41
+	{
42
+		$error = 'Use of the backtick operator is forbidden';
43
+		$phpcsFile->addError($error, $stackPtr, 'Found');
44
+
45
+	}//end process()
46 46
 
47 47
 
48 48
 }//end class
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/Generic/Sniffs/PHP/DiscourageGotoSniff.php 1 patch
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -16,35 +16,35 @@
 block discarded – undo
16 16
 {
17 17
 
18 18
 
19
-    /**
20
-     * Returns an array of tokens this test wants to listen for.
21
-     *
22
-     * @return array
23
-     */
24
-    public function register()
25
-    {
26
-        return [
27
-            T_GOTO,
28
-            T_GOTO_LABEL,
29
-        ];
30
-
31
-    }//end register()
32
-
33
-
34
-    /**
35
-     * Processes this sniff, when one of its tokens is encountered.
36
-     *
37
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
38
-     * @param int                         $stackPtr  The position of the current token in
39
-     *                                               the stack passed in $tokens.
40
-     *
41
-     * @return void
42
-     */
43
-    public function process(File $phpcsFile, $stackPtr)
44
-    {
45
-        $phpcsFile->addWarning('Use of the GOTO language construct is discouraged', $stackPtr, 'Found');
46
-
47
-    }//end process()
19
+	/**
20
+	 * Returns an array of tokens this test wants to listen for.
21
+	 *
22
+	 * @return array
23
+	 */
24
+	public function register()
25
+	{
26
+		return [
27
+			T_GOTO,
28
+			T_GOTO_LABEL,
29
+		];
30
+
31
+	}//end register()
32
+
33
+
34
+	/**
35
+	 * Processes this sniff, when one of its tokens is encountered.
36
+	 *
37
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
38
+	 * @param int                         $stackPtr  The position of the current token in
39
+	 *                                               the stack passed in $tokens.
40
+	 *
41
+	 * @return void
42
+	 */
43
+	public function process(File $phpcsFile, $stackPtr)
44
+	{
45
+		$phpcsFile->addWarning('Use of the GOTO language construct is discouraged', $stackPtr, 'Found');
46
+
47
+	}//end process()
48 48
 
49 49
 
50 50
 }//end class
Please login to merge, or discard this patch.
src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -194,7 +194,7 @@
 block discarded – undo
194 194
      *
195 195
      * @return void
196 196
      */
197
-    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
197
+    protected function addError($phpcsFile, $stackPtr, $function, $pattern = null)
198 198
     {
199 199
         $data  = [$function];
200 200
         $error = 'The use of function %s() is ';
Please login to merge, or discard this patch.
Indentation   +217 added lines, -217 removed lines patch added patch discarded remove patch
@@ -18,223 +18,223 @@
 block discarded – undo
18 18
 class ForbiddenFunctionsSniff implements Sniff
19 19
 {
20 20
 
21
-    /**
22
-     * A list of forbidden functions with their alternatives.
23
-     *
24
-     * The value is NULL if no alternative exists. IE, the
25
-     * function should just not be used.
26
-     *
27
-     * @var array<string, string|null>
28
-     */
29
-    public $forbiddenFunctions = [
30
-        'sizeof' => 'count',
31
-        'delete' => 'unset',
32
-    ];
33
-
34
-    /**
35
-     * A cache of forbidden function names, for faster lookups.
36
-     *
37
-     * @var string[]
38
-     */
39
-    protected $forbiddenFunctionNames = [];
40
-
41
-    /**
42
-     * If true, forbidden functions will be considered regular expressions.
43
-     *
44
-     * @var boolean
45
-     */
46
-    protected $patternMatch = false;
47
-
48
-    /**
49
-     * If true, an error will be thrown; otherwise a warning.
50
-     *
51
-     * @var boolean
52
-     */
53
-    public $error = true;
54
-
55
-
56
-    /**
57
-     * Returns an array of tokens this test wants to listen for.
58
-     *
59
-     * @return array
60
-     */
61
-    public function register()
62
-    {
63
-        // Everyone has had a chance to figure out what forbidden functions
64
-        // they want to check for, so now we can cache out the list.
65
-        $this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
66
-
67
-        if ($this->patternMatch === true) {
68
-            foreach ($this->forbiddenFunctionNames as $i => $name) {
69
-                $this->forbiddenFunctionNames[$i] = '/'.$name.'/i';
70
-            }
71
-
72
-            return [T_STRING];
73
-        }
74
-
75
-        // If we are not pattern matching, we need to work out what
76
-        // tokens to listen for.
77
-        $hasHaltCompiler = false;
78
-        $string          = '<?php ';
79
-        foreach ($this->forbiddenFunctionNames as $name) {
80
-            if ($name === '__halt_compiler') {
81
-                $hasHaltCompiler = true;
82
-            } else {
83
-                $string .= $name.'();';
84
-            }
85
-        }
86
-
87
-        if ($hasHaltCompiler === true) {
88
-            $string .= '__halt_compiler();';
89
-        }
90
-
91
-        $register = [];
92
-
93
-        $tokens = token_get_all($string);
94
-        array_shift($tokens);
95
-        foreach ($tokens as $token) {
96
-            if (is_array($token) === true) {
97
-                $register[] = $token[0];
98
-            }
99
-        }
100
-
101
-        $this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
102
-        $this->forbiddenFunctions     = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);
103
-
104
-        return array_unique($register);
105
-
106
-    }//end register()
107
-
108
-
109
-    /**
110
-     * Processes this test, when one of its tokens is encountered.
111
-     *
112
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
113
-     * @param int                         $stackPtr  The position of the current token in
114
-     *                                               the stack passed in $tokens.
115
-     *
116
-     * @return void
117
-     */
118
-    public function process(File $phpcsFile, $stackPtr)
119
-    {
120
-        $tokens = $phpcsFile->getTokens();
121
-
122
-        $ignore = [
123
-            T_DOUBLE_COLON             => true,
124
-            T_OBJECT_OPERATOR          => true,
125
-            T_NULLSAFE_OBJECT_OPERATOR => true,
126
-            T_FUNCTION                 => true,
127
-            T_CONST                    => true,
128
-            T_PUBLIC                   => true,
129
-            T_PRIVATE                  => true,
130
-            T_PROTECTED                => true,
131
-            T_AS                       => true,
132
-            T_NEW                      => true,
133
-            T_INSTEADOF                => true,
134
-            T_NS_SEPARATOR             => true,
135
-            T_IMPLEMENTS               => true,
136
-        ];
137
-
138
-        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
139
-
140
-        // If function call is directly preceded by a NS_SEPARATOR it points to the
141
-        // global namespace, so we should still catch it.
142
-        if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR) {
143
-            $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($prevToken - 1), null, true);
144
-            if ($tokens[$prevToken]['code'] === T_STRING) {
145
-                // Not in the global namespace.
146
-                return;
147
-            }
148
-        }
149
-
150
-        if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
151
-            // Not a call to a PHP function.
152
-            return;
153
-        }
154
-
155
-        $nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
156
-        if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
157
-            // Not a call to a PHP function.
158
-            return;
159
-        }
160
-
161
-        if ($tokens[$stackPtr]['code'] === T_STRING && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) {
162
-            // Not a call to a PHP function.
163
-            return;
164
-        }
165
-
166
-        $function = strtolower($tokens[$stackPtr]['content']);
167
-        $pattern  = null;
168
-
169
-        if ($this->patternMatch === true) {
170
-            $count   = 0;
171
-            $pattern = preg_replace(
172
-                $this->forbiddenFunctionNames,
173
-                $this->forbiddenFunctionNames,
174
-                $function,
175
-                1,
176
-                $count
177
-            );
178
-
179
-            if ($count === 0) {
180
-                return;
181
-            }
182
-
183
-            // Remove the pattern delimiters and modifier.
184
-            $pattern = substr($pattern, 1, -2);
185
-        } else {
186
-            if (in_array($function, $this->forbiddenFunctionNames, true) === false) {
187
-                return;
188
-            }
189
-        }//end if
190
-
191
-        $this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content'], $pattern);
192
-
193
-    }//end process()
194
-
195
-
196
-    /**
197
-     * Generates the error or warning for this sniff.
198
-     *
199
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
200
-     * @param int                         $stackPtr  The position of the forbidden function
201
-     *                                               in the token array.
202
-     * @param string                      $function  The name of the forbidden function.
203
-     * @param string                      $pattern   The pattern used for the match.
204
-     *
205
-     * @return void
206
-     */
207
-    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
208
-    {
209
-        $data  = [$function];
210
-        $error = 'The use of function %s() is ';
211
-        if ($this->error === true) {
212
-            $type   = 'Found';
213
-            $error .= 'forbidden';
214
-        } else {
215
-            $type   = 'Discouraged';
216
-            $error .= 'discouraged';
217
-        }
218
-
219
-        if ($pattern === null) {
220
-            $pattern = strtolower($function);
221
-        }
222
-
223
-        if ($this->forbiddenFunctions[$pattern] !== null
224
-            && $this->forbiddenFunctions[$pattern] !== 'null'
225
-        ) {
226
-            $type  .= 'WithAlternative';
227
-            $data[] = $this->forbiddenFunctions[$pattern];
228
-            $error .= '; use %s() instead';
229
-        }
230
-
231
-        if ($this->error === true) {
232
-            $phpcsFile->addError($error, $stackPtr, $type, $data);
233
-        } else {
234
-            $phpcsFile->addWarning($error, $stackPtr, $type, $data);
235
-        }
236
-
237
-    }//end addError()
21
+	/**
22
+	 * A list of forbidden functions with their alternatives.
23
+	 *
24
+	 * The value is NULL if no alternative exists. IE, the
25
+	 * function should just not be used.
26
+	 *
27
+	 * @var array<string, string|null>
28
+	 */
29
+	public $forbiddenFunctions = [
30
+		'sizeof' => 'count',
31
+		'delete' => 'unset',
32
+	];
33
+
34
+	/**
35
+	 * A cache of forbidden function names, for faster lookups.
36
+	 *
37
+	 * @var string[]
38
+	 */
39
+	protected $forbiddenFunctionNames = [];
40
+
41
+	/**
42
+	 * If true, forbidden functions will be considered regular expressions.
43
+	 *
44
+	 * @var boolean
45
+	 */
46
+	protected $patternMatch = false;
47
+
48
+	/**
49
+	 * If true, an error will be thrown; otherwise a warning.
50
+	 *
51
+	 * @var boolean
52
+	 */
53
+	public $error = true;
54
+
55
+
56
+	/**
57
+	 * Returns an array of tokens this test wants to listen for.
58
+	 *
59
+	 * @return array
60
+	 */
61
+	public function register()
62
+	{
63
+		// Everyone has had a chance to figure out what forbidden functions
64
+		// they want to check for, so now we can cache out the list.
65
+		$this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
66
+
67
+		if ($this->patternMatch === true) {
68
+			foreach ($this->forbiddenFunctionNames as $i => $name) {
69
+				$this->forbiddenFunctionNames[$i] = '/'.$name.'/i';
70
+			}
71
+
72
+			return [T_STRING];
73
+		}
74
+
75
+		// If we are not pattern matching, we need to work out what
76
+		// tokens to listen for.
77
+		$hasHaltCompiler = false;
78
+		$string          = '<?php ';
79
+		foreach ($this->forbiddenFunctionNames as $name) {
80
+			if ($name === '__halt_compiler') {
81
+				$hasHaltCompiler = true;
82
+			} else {
83
+				$string .= $name.'();';
84
+			}
85
+		}
86
+
87
+		if ($hasHaltCompiler === true) {
88
+			$string .= '__halt_compiler();';
89
+		}
90
+
91
+		$register = [];
92
+
93
+		$tokens = token_get_all($string);
94
+		array_shift($tokens);
95
+		foreach ($tokens as $token) {
96
+			if (is_array($token) === true) {
97
+				$register[] = $token[0];
98
+			}
99
+		}
100
+
101
+		$this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
102
+		$this->forbiddenFunctions     = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);
103
+
104
+		return array_unique($register);
105
+
106
+	}//end register()
107
+
108
+
109
+	/**
110
+	 * Processes this test, when one of its tokens is encountered.
111
+	 *
112
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
113
+	 * @param int                         $stackPtr  The position of the current token in
114
+	 *                                               the stack passed in $tokens.
115
+	 *
116
+	 * @return void
117
+	 */
118
+	public function process(File $phpcsFile, $stackPtr)
119
+	{
120
+		$tokens = $phpcsFile->getTokens();
121
+
122
+		$ignore = [
123
+			T_DOUBLE_COLON             => true,
124
+			T_OBJECT_OPERATOR          => true,
125
+			T_NULLSAFE_OBJECT_OPERATOR => true,
126
+			T_FUNCTION                 => true,
127
+			T_CONST                    => true,
128
+			T_PUBLIC                   => true,
129
+			T_PRIVATE                  => true,
130
+			T_PROTECTED                => true,
131
+			T_AS                       => true,
132
+			T_NEW                      => true,
133
+			T_INSTEADOF                => true,
134
+			T_NS_SEPARATOR             => true,
135
+			T_IMPLEMENTS               => true,
136
+		];
137
+
138
+		$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
139
+
140
+		// If function call is directly preceded by a NS_SEPARATOR it points to the
141
+		// global namespace, so we should still catch it.
142
+		if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR) {
143
+			$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($prevToken - 1), null, true);
144
+			if ($tokens[$prevToken]['code'] === T_STRING) {
145
+				// Not in the global namespace.
146
+				return;
147
+			}
148
+		}
149
+
150
+		if (isset($ignore[$tokens[$prevToken]['code']]) === true) {
151
+			// Not a call to a PHP function.
152
+			return;
153
+		}
154
+
155
+		$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
156
+		if (isset($ignore[$tokens[$nextToken]['code']]) === true) {
157
+			// Not a call to a PHP function.
158
+			return;
159
+		}
160
+
161
+		if ($tokens[$stackPtr]['code'] === T_STRING && $tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) {
162
+			// Not a call to a PHP function.
163
+			return;
164
+		}
165
+
166
+		$function = strtolower($tokens[$stackPtr]['content']);
167
+		$pattern  = null;
168
+
169
+		if ($this->patternMatch === true) {
170
+			$count   = 0;
171
+			$pattern = preg_replace(
172
+				$this->forbiddenFunctionNames,
173
+				$this->forbiddenFunctionNames,
174
+				$function,
175
+				1,
176
+				$count
177
+			);
178
+
179
+			if ($count === 0) {
180
+				return;
181
+			}
182
+
183
+			// Remove the pattern delimiters and modifier.
184
+			$pattern = substr($pattern, 1, -2);
185
+		} else {
186
+			if (in_array($function, $this->forbiddenFunctionNames, true) === false) {
187
+				return;
188
+			}
189
+		}//end if
190
+
191
+		$this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content'], $pattern);
192
+
193
+	}//end process()
194
+
195
+
196
+	/**
197
+	 * Generates the error or warning for this sniff.
198
+	 *
199
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
200
+	 * @param int                         $stackPtr  The position of the forbidden function
201
+	 *                                               in the token array.
202
+	 * @param string                      $function  The name of the forbidden function.
203
+	 * @param string                      $pattern   The pattern used for the match.
204
+	 *
205
+	 * @return void
206
+	 */
207
+	protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
208
+	{
209
+		$data  = [$function];
210
+		$error = 'The use of function %s() is ';
211
+		if ($this->error === true) {
212
+			$type   = 'Found';
213
+			$error .= 'forbidden';
214
+		} else {
215
+			$type   = 'Discouraged';
216
+			$error .= 'discouraged';
217
+		}
218
+
219
+		if ($pattern === null) {
220
+			$pattern = strtolower($function);
221
+		}
222
+
223
+		if ($this->forbiddenFunctions[$pattern] !== null
224
+			&& $this->forbiddenFunctions[$pattern] !== 'null'
225
+		) {
226
+			$type  .= 'WithAlternative';
227
+			$data[] = $this->forbiddenFunctions[$pattern];
228
+			$error .= '; use %s() instead';
229
+		}
230
+
231
+		if ($this->error === true) {
232
+			$phpcsFile->addError($error, $stackPtr, $type, $data);
233
+		} else {
234
+			$phpcsFile->addWarning($error, $stackPtr, $type, $data);
235
+		}
236
+
237
+	}//end addError()
238 238
 
239 239
 
240 240
 }//end class
Please login to merge, or discard this patch.