Completed
Branch BUG/required-message-fields (8f9492)
by
unknown
10:53 queued 20s
created
src/Standards/Squiz/Sniffs/ControlStructures/SwitchDeclarationSniff.php 1 patch
Indentation   +283 added lines, -283 removed lines patch added patch discarded remove patch
@@ -16,289 +16,289 @@
 block discarded – undo
16 16
 class SwitchDeclarationSniff implements Sniff
17 17
 {
18 18
 
19
-    /**
20
-     * A list of tokenizers this sniff supports.
21
-     *
22
-     * @var array
23
-     */
24
-    public $supportedTokenizers = [
25
-        'PHP',
26
-        'JS',
27
-    ];
28
-
29
-    /**
30
-     * The number of spaces code should be indented.
31
-     *
32
-     * @var integer
33
-     */
34
-    public $indent = 4;
35
-
36
-
37
-    /**
38
-     * Returns an array of tokens this test wants to listen for.
39
-     *
40
-     * @return array
41
-     */
42
-    public function register()
43
-    {
44
-        return [T_SWITCH];
45
-
46
-    }//end register()
47
-
48
-
49
-    /**
50
-     * Processes this test, when one of its tokens is encountered.
51
-     *
52
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
53
-     * @param int                         $stackPtr  The position of the current token in the
54
-     *                                               stack passed in $tokens.
55
-     *
56
-     * @return void
57
-     */
58
-    public function process(File $phpcsFile, $stackPtr)
59
-    {
60
-        $tokens = $phpcsFile->getTokens();
61
-
62
-        // We can't process SWITCH statements unless we know where they start and end.
63
-        if (isset($tokens[$stackPtr]['scope_opener']) === false
64
-            || isset($tokens[$stackPtr]['scope_closer']) === false
65
-        ) {
66
-            return;
67
-        }
68
-
69
-        $switch        = $tokens[$stackPtr];
70
-        $nextCase      = $stackPtr;
71
-        $caseAlignment = ($switch['column'] + $this->indent);
72
-        $caseCount     = 0;
73
-        $foundDefault  = false;
74
-
75
-        while (($nextCase = $phpcsFile->findNext([T_CASE, T_DEFAULT, T_SWITCH], ($nextCase + 1), $switch['scope_closer'])) !== false) {
76
-            // Skip nested SWITCH statements; they are handled on their own.
77
-            if ($tokens[$nextCase]['code'] === T_SWITCH) {
78
-                $nextCase = $tokens[$nextCase]['scope_closer'];
79
-                continue;
80
-            }
81
-
82
-            if ($tokens[$nextCase]['code'] === T_DEFAULT) {
83
-                $type         = 'Default';
84
-                $foundDefault = true;
85
-            } else {
86
-                $type = 'Case';
87
-                $caseCount++;
88
-            }
89
-
90
-            if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) {
91
-                $expected = strtolower($tokens[$nextCase]['content']);
92
-                $error    = strtoupper($type).' keyword must be lowercase; expected "%s" but found "%s"';
93
-                $data     = [
94
-                    $expected,
95
-                    $tokens[$nextCase]['content'],
96
-                ];
97
-
98
-                $fix = $phpcsFile->addFixableError($error, $nextCase, $type.'NotLower', $data);
99
-                if ($fix === true) {
100
-                    $phpcsFile->fixer->replaceToken($nextCase, $expected);
101
-                }
102
-            }
103
-
104
-            if ($tokens[$nextCase]['column'] !== $caseAlignment) {
105
-                $error = strtoupper($type).' keyword must be indented '.$this->indent.' spaces from SWITCH keyword';
106
-                $fix   = $phpcsFile->addFixableError($error, $nextCase, $type.'Indent');
107
-
108
-                if ($fix === true) {
109
-                    $padding = str_repeat(' ', ($caseAlignment - 1));
110
-                    if ($tokens[$nextCase]['column'] === 1
111
-                        || $tokens[($nextCase - 1)]['code'] !== T_WHITESPACE
112
-                    ) {
113
-                        $phpcsFile->fixer->addContentBefore($nextCase, $padding);
114
-                    } else {
115
-                        $phpcsFile->fixer->replaceToken(($nextCase - 1), $padding);
116
-                    }
117
-                }
118
-            }
119
-
120
-            if ($type === 'Case'
121
-                && ($tokens[($nextCase + 1)]['type'] !== 'T_WHITESPACE'
122
-                || $tokens[($nextCase + 1)]['content'] !== ' ')
123
-            ) {
124
-                $error = 'CASE keyword must be followed by a single space';
125
-                $fix   = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase');
126
-                if ($fix === true) {
127
-                    if ($tokens[($nextCase + 1)]['type'] !== 'T_WHITESPACE') {
128
-                        $phpcsFile->fixer->addContent($nextCase, ' ');
129
-                    } else {
130
-                        $phpcsFile->fixer->replaceToken(($nextCase + 1), ' ');
131
-                    }
132
-                }
133
-            }
134
-
135
-            if (isset($tokens[$nextCase]['scope_opener']) === false) {
136
-                $error = 'Possible parse error: CASE missing opening colon';
137
-                $phpcsFile->addWarning($error, $nextCase, 'MissingColon');
138
-                continue;
139
-            }
140
-
141
-            $opener = $tokens[$nextCase]['scope_opener'];
142
-            if ($tokens[($opener - 1)]['type'] === 'T_WHITESPACE') {
143
-                $error = 'There must be no space before the colon in a '.strtoupper($type).' statement';
144
-                $fix   = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon'.$type);
145
-                if ($fix === true) {
146
-                    $phpcsFile->fixer->replaceToken(($opener - 1), '');
147
-                }
148
-            }
149
-
150
-            $nextBreak = $tokens[$nextCase]['scope_closer'];
151
-            if ($tokens[$nextBreak]['code'] === T_BREAK
152
-                || $tokens[$nextBreak]['code'] === T_RETURN
153
-                || $tokens[$nextBreak]['code'] === T_CONTINUE
154
-                || $tokens[$nextBreak]['code'] === T_THROW
155
-                || $tokens[$nextBreak]['code'] === T_EXIT
156
-            ) {
157
-                if ($tokens[$nextBreak]['scope_condition'] === $nextCase) {
158
-                    // Only need to check a couple of things once, even if the
159
-                    // break is shared between multiple case statements, or even
160
-                    // the default case.
161
-                    if ($tokens[$nextBreak]['column'] !== $caseAlignment) {
162
-                        $error = 'Case breaking statement must be indented '.$this->indent.' spaces from SWITCH keyword';
163
-                        $fix   = $phpcsFile->addFixableError($error, $nextBreak, 'BreakIndent');
164
-
165
-                        if ($fix === true) {
166
-                            $padding = str_repeat(' ', ($caseAlignment - 1));
167
-                            if ($tokens[$nextBreak]['column'] === 1
168
-                                || $tokens[($nextBreak - 1)]['code'] !== T_WHITESPACE
169
-                            ) {
170
-                                $phpcsFile->fixer->addContentBefore($nextBreak, $padding);
171
-                            } else {
172
-                                $phpcsFile->fixer->replaceToken(($nextBreak - 1), $padding);
173
-                            }
174
-                        }
175
-                    }
176
-
177
-                    $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextBreak - 1), $stackPtr, true);
178
-                    if ($tokens[$prev]['line'] !== ($tokens[$nextBreak]['line'] - 1)) {
179
-                        $error = 'Blank lines are not allowed before case breaking statements';
180
-                        $phpcsFile->addError($error, $nextBreak, 'SpacingBeforeBreak');
181
-                    }
182
-
183
-                    $nextLine  = $tokens[$tokens[$stackPtr]['scope_closer']]['line'];
184
-                    $semicolon = $phpcsFile->findEndOfStatement($nextBreak);
185
-                    for ($i = ($semicolon + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) {
186
-                        if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
187
-                            $nextLine = $tokens[$i]['line'];
188
-                            break;
189
-                        }
190
-                    }
191
-
192
-                    if ($type === 'Case') {
193
-                        // Ensure the BREAK statement is followed by
194
-                        // a single blank line, or the end switch brace.
195
-                        if ($nextLine !== ($tokens[$semicolon]['line'] + 2) && $i !== $tokens[$stackPtr]['scope_closer']) {
196
-                            $error = 'Case breaking statements must be followed by a single blank line';
197
-                            $fix   = $phpcsFile->addFixableError($error, $nextBreak, 'SpacingAfterBreak');
198
-                            if ($fix === true) {
199
-                                $phpcsFile->fixer->beginChangeset();
200
-                                for ($i = ($semicolon + 1); $i <= $tokens[$stackPtr]['scope_closer']; $i++) {
201
-                                    if ($tokens[$i]['line'] === $nextLine) {
202
-                                        $phpcsFile->fixer->addNewlineBefore($i);
203
-                                        break;
204
-                                    }
205
-
206
-                                    if ($tokens[$i]['line'] === $tokens[$semicolon]['line']) {
207
-                                        continue;
208
-                                    }
209
-
210
-                                    $phpcsFile->fixer->replaceToken($i, '');
211
-                                }
212
-
213
-                                $phpcsFile->fixer->endChangeset();
214
-                            }
215
-                        }//end if
216
-                    } else {
217
-                        // Ensure the BREAK statement is not followed by a blank line.
218
-                        if ($nextLine !== ($tokens[$semicolon]['line'] + 1)) {
219
-                            $error = 'Blank lines are not allowed after the DEFAULT case\'s breaking statement';
220
-                            $phpcsFile->addError($error, $nextBreak, 'SpacingAfterDefaultBreak');
221
-                        }
222
-                    }//end if
223
-
224
-                    $caseLine = $tokens[$nextCase]['line'];
225
-                    $nextLine = $tokens[$nextBreak]['line'];
226
-                    for ($i = ($opener + 1); $i < $nextBreak; $i++) {
227
-                        if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
228
-                            $nextLine = $tokens[$i]['line'];
229
-                            break;
230
-                        }
231
-                    }
232
-
233
-                    if ($nextLine !== ($caseLine + 1)) {
234
-                        $error = 'Blank lines are not allowed after '.strtoupper($type).' statements';
235
-                        $phpcsFile->addError($error, $nextCase, 'SpacingAfter'.$type);
236
-                    }
237
-                }//end if
238
-
239
-                if ($tokens[$nextBreak]['code'] === T_BREAK) {
240
-                    if ($type === 'Case') {
241
-                        // Ensure empty CASE statements are not allowed.
242
-                        // They must have some code content in them. A comment is not enough.
243
-                        // But count RETURN statements as valid content if they also
244
-                        // happen to close the CASE statement.
245
-                        $foundContent = false;
246
-                        for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) {
247
-                            if ($tokens[$i]['code'] === T_CASE) {
248
-                                $i = $tokens[$i]['scope_opener'];
249
-                                continue;
250
-                            }
251
-
252
-                            if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === false) {
253
-                                $foundContent = true;
254
-                                break;
255
-                            }
256
-                        }
257
-
258
-                        if ($foundContent === false) {
259
-                            $error = 'Empty CASE statements are not allowed';
260
-                            $phpcsFile->addError($error, $nextCase, 'EmptyCase');
261
-                        }
262
-                    } else {
263
-                        // Ensure empty DEFAULT statements are not allowed.
264
-                        // They must (at least) have a comment describing why
265
-                        // the default case is being ignored.
266
-                        $foundContent = false;
267
-                        for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) {
268
-                            if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
269
-                                $foundContent = true;
270
-                                break;
271
-                            }
272
-                        }
273
-
274
-                        if ($foundContent === false) {
275
-                            $error = 'Comment required for empty DEFAULT case';
276
-                            $phpcsFile->addError($error, $nextCase, 'EmptyDefault');
277
-                        }
278
-                    }//end if
279
-                }//end if
280
-            } else if ($type === 'Default') {
281
-                $error = 'DEFAULT case must have a breaking statement';
282
-                $phpcsFile->addError($error, $nextCase, 'DefaultNoBreak');
283
-            }//end if
284
-        }//end while
285
-
286
-        if ($foundDefault === false) {
287
-            $error = 'All SWITCH statements must contain a DEFAULT case';
288
-            $phpcsFile->addError($error, $stackPtr, 'MissingDefault');
289
-        }
290
-
291
-        if ($tokens[$switch['scope_closer']]['column'] !== $switch['column']) {
292
-            $error = 'Closing brace of SWITCH statement must be aligned with SWITCH keyword';
293
-            $phpcsFile->addError($error, $switch['scope_closer'], 'CloseBraceAlign');
294
-        }
295
-
296
-        if ($caseCount === 0) {
297
-            $error = 'SWITCH statements must contain at least one CASE statement';
298
-            $phpcsFile->addError($error, $stackPtr, 'MissingCase');
299
-        }
300
-
301
-    }//end process()
19
+	/**
20
+	 * A list of tokenizers this sniff supports.
21
+	 *
22
+	 * @var array
23
+	 */
24
+	public $supportedTokenizers = [
25
+		'PHP',
26
+		'JS',
27
+	];
28
+
29
+	/**
30
+	 * The number of spaces code should be indented.
31
+	 *
32
+	 * @var integer
33
+	 */
34
+	public $indent = 4;
35
+
36
+
37
+	/**
38
+	 * Returns an array of tokens this test wants to listen for.
39
+	 *
40
+	 * @return array
41
+	 */
42
+	public function register()
43
+	{
44
+		return [T_SWITCH];
45
+
46
+	}//end register()
47
+
48
+
49
+	/**
50
+	 * Processes this test, when one of its tokens is encountered.
51
+	 *
52
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
53
+	 * @param int                         $stackPtr  The position of the current token in the
54
+	 *                                               stack passed in $tokens.
55
+	 *
56
+	 * @return void
57
+	 */
58
+	public function process(File $phpcsFile, $stackPtr)
59
+	{
60
+		$tokens = $phpcsFile->getTokens();
61
+
62
+		// We can't process SWITCH statements unless we know where they start and end.
63
+		if (isset($tokens[$stackPtr]['scope_opener']) === false
64
+			|| isset($tokens[$stackPtr]['scope_closer']) === false
65
+		) {
66
+			return;
67
+		}
68
+
69
+		$switch        = $tokens[$stackPtr];
70
+		$nextCase      = $stackPtr;
71
+		$caseAlignment = ($switch['column'] + $this->indent);
72
+		$caseCount     = 0;
73
+		$foundDefault  = false;
74
+
75
+		while (($nextCase = $phpcsFile->findNext([T_CASE, T_DEFAULT, T_SWITCH], ($nextCase + 1), $switch['scope_closer'])) !== false) {
76
+			// Skip nested SWITCH statements; they are handled on their own.
77
+			if ($tokens[$nextCase]['code'] === T_SWITCH) {
78
+				$nextCase = $tokens[$nextCase]['scope_closer'];
79
+				continue;
80
+			}
81
+
82
+			if ($tokens[$nextCase]['code'] === T_DEFAULT) {
83
+				$type         = 'Default';
84
+				$foundDefault = true;
85
+			} else {
86
+				$type = 'Case';
87
+				$caseCount++;
88
+			}
89
+
90
+			if ($tokens[$nextCase]['content'] !== strtolower($tokens[$nextCase]['content'])) {
91
+				$expected = strtolower($tokens[$nextCase]['content']);
92
+				$error    = strtoupper($type).' keyword must be lowercase; expected "%s" but found "%s"';
93
+				$data     = [
94
+					$expected,
95
+					$tokens[$nextCase]['content'],
96
+				];
97
+
98
+				$fix = $phpcsFile->addFixableError($error, $nextCase, $type.'NotLower', $data);
99
+				if ($fix === true) {
100
+					$phpcsFile->fixer->replaceToken($nextCase, $expected);
101
+				}
102
+			}
103
+
104
+			if ($tokens[$nextCase]['column'] !== $caseAlignment) {
105
+				$error = strtoupper($type).' keyword must be indented '.$this->indent.' spaces from SWITCH keyword';
106
+				$fix   = $phpcsFile->addFixableError($error, $nextCase, $type.'Indent');
107
+
108
+				if ($fix === true) {
109
+					$padding = str_repeat(' ', ($caseAlignment - 1));
110
+					if ($tokens[$nextCase]['column'] === 1
111
+						|| $tokens[($nextCase - 1)]['code'] !== T_WHITESPACE
112
+					) {
113
+						$phpcsFile->fixer->addContentBefore($nextCase, $padding);
114
+					} else {
115
+						$phpcsFile->fixer->replaceToken(($nextCase - 1), $padding);
116
+					}
117
+				}
118
+			}
119
+
120
+			if ($type === 'Case'
121
+				&& ($tokens[($nextCase + 1)]['type'] !== 'T_WHITESPACE'
122
+				|| $tokens[($nextCase + 1)]['content'] !== ' ')
123
+			) {
124
+				$error = 'CASE keyword must be followed by a single space';
125
+				$fix   = $phpcsFile->addFixableError($error, $nextCase, 'SpacingAfterCase');
126
+				if ($fix === true) {
127
+					if ($tokens[($nextCase + 1)]['type'] !== 'T_WHITESPACE') {
128
+						$phpcsFile->fixer->addContent($nextCase, ' ');
129
+					} else {
130
+						$phpcsFile->fixer->replaceToken(($nextCase + 1), ' ');
131
+					}
132
+				}
133
+			}
134
+
135
+			if (isset($tokens[$nextCase]['scope_opener']) === false) {
136
+				$error = 'Possible parse error: CASE missing opening colon';
137
+				$phpcsFile->addWarning($error, $nextCase, 'MissingColon');
138
+				continue;
139
+			}
140
+
141
+			$opener = $tokens[$nextCase]['scope_opener'];
142
+			if ($tokens[($opener - 1)]['type'] === 'T_WHITESPACE') {
143
+				$error = 'There must be no space before the colon in a '.strtoupper($type).' statement';
144
+				$fix   = $phpcsFile->addFixableError($error, $nextCase, 'SpaceBeforeColon'.$type);
145
+				if ($fix === true) {
146
+					$phpcsFile->fixer->replaceToken(($opener - 1), '');
147
+				}
148
+			}
149
+
150
+			$nextBreak = $tokens[$nextCase]['scope_closer'];
151
+			if ($tokens[$nextBreak]['code'] === T_BREAK
152
+				|| $tokens[$nextBreak]['code'] === T_RETURN
153
+				|| $tokens[$nextBreak]['code'] === T_CONTINUE
154
+				|| $tokens[$nextBreak]['code'] === T_THROW
155
+				|| $tokens[$nextBreak]['code'] === T_EXIT
156
+			) {
157
+				if ($tokens[$nextBreak]['scope_condition'] === $nextCase) {
158
+					// Only need to check a couple of things once, even if the
159
+					// break is shared between multiple case statements, or even
160
+					// the default case.
161
+					if ($tokens[$nextBreak]['column'] !== $caseAlignment) {
162
+						$error = 'Case breaking statement must be indented '.$this->indent.' spaces from SWITCH keyword';
163
+						$fix   = $phpcsFile->addFixableError($error, $nextBreak, 'BreakIndent');
164
+
165
+						if ($fix === true) {
166
+							$padding = str_repeat(' ', ($caseAlignment - 1));
167
+							if ($tokens[$nextBreak]['column'] === 1
168
+								|| $tokens[($nextBreak - 1)]['code'] !== T_WHITESPACE
169
+							) {
170
+								$phpcsFile->fixer->addContentBefore($nextBreak, $padding);
171
+							} else {
172
+								$phpcsFile->fixer->replaceToken(($nextBreak - 1), $padding);
173
+							}
174
+						}
175
+					}
176
+
177
+					$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($nextBreak - 1), $stackPtr, true);
178
+					if ($tokens[$prev]['line'] !== ($tokens[$nextBreak]['line'] - 1)) {
179
+						$error = 'Blank lines are not allowed before case breaking statements';
180
+						$phpcsFile->addError($error, $nextBreak, 'SpacingBeforeBreak');
181
+					}
182
+
183
+					$nextLine  = $tokens[$tokens[$stackPtr]['scope_closer']]['line'];
184
+					$semicolon = $phpcsFile->findEndOfStatement($nextBreak);
185
+					for ($i = ($semicolon + 1); $i < $tokens[$stackPtr]['scope_closer']; $i++) {
186
+						if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
187
+							$nextLine = $tokens[$i]['line'];
188
+							break;
189
+						}
190
+					}
191
+
192
+					if ($type === 'Case') {
193
+						// Ensure the BREAK statement is followed by
194
+						// a single blank line, or the end switch brace.
195
+						if ($nextLine !== ($tokens[$semicolon]['line'] + 2) && $i !== $tokens[$stackPtr]['scope_closer']) {
196
+							$error = 'Case breaking statements must be followed by a single blank line';
197
+							$fix   = $phpcsFile->addFixableError($error, $nextBreak, 'SpacingAfterBreak');
198
+							if ($fix === true) {
199
+								$phpcsFile->fixer->beginChangeset();
200
+								for ($i = ($semicolon + 1); $i <= $tokens[$stackPtr]['scope_closer']; $i++) {
201
+									if ($tokens[$i]['line'] === $nextLine) {
202
+										$phpcsFile->fixer->addNewlineBefore($i);
203
+										break;
204
+									}
205
+
206
+									if ($tokens[$i]['line'] === $tokens[$semicolon]['line']) {
207
+										continue;
208
+									}
209
+
210
+									$phpcsFile->fixer->replaceToken($i, '');
211
+								}
212
+
213
+								$phpcsFile->fixer->endChangeset();
214
+							}
215
+						}//end if
216
+					} else {
217
+						// Ensure the BREAK statement is not followed by a blank line.
218
+						if ($nextLine !== ($tokens[$semicolon]['line'] + 1)) {
219
+							$error = 'Blank lines are not allowed after the DEFAULT case\'s breaking statement';
220
+							$phpcsFile->addError($error, $nextBreak, 'SpacingAfterDefaultBreak');
221
+						}
222
+					}//end if
223
+
224
+					$caseLine = $tokens[$nextCase]['line'];
225
+					$nextLine = $tokens[$nextBreak]['line'];
226
+					for ($i = ($opener + 1); $i < $nextBreak; $i++) {
227
+						if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
228
+							$nextLine = $tokens[$i]['line'];
229
+							break;
230
+						}
231
+					}
232
+
233
+					if ($nextLine !== ($caseLine + 1)) {
234
+						$error = 'Blank lines are not allowed after '.strtoupper($type).' statements';
235
+						$phpcsFile->addError($error, $nextCase, 'SpacingAfter'.$type);
236
+					}
237
+				}//end if
238
+
239
+				if ($tokens[$nextBreak]['code'] === T_BREAK) {
240
+					if ($type === 'Case') {
241
+						// Ensure empty CASE statements are not allowed.
242
+						// They must have some code content in them. A comment is not enough.
243
+						// But count RETURN statements as valid content if they also
244
+						// happen to close the CASE statement.
245
+						$foundContent = false;
246
+						for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) {
247
+							if ($tokens[$i]['code'] === T_CASE) {
248
+								$i = $tokens[$i]['scope_opener'];
249
+								continue;
250
+							}
251
+
252
+							if (isset(Tokens::$emptyTokens[$tokens[$i]['code']]) === false) {
253
+								$foundContent = true;
254
+								break;
255
+							}
256
+						}
257
+
258
+						if ($foundContent === false) {
259
+							$error = 'Empty CASE statements are not allowed';
260
+							$phpcsFile->addError($error, $nextCase, 'EmptyCase');
261
+						}
262
+					} else {
263
+						// Ensure empty DEFAULT statements are not allowed.
264
+						// They must (at least) have a comment describing why
265
+						// the default case is being ignored.
266
+						$foundContent = false;
267
+						for ($i = ($tokens[$nextCase]['scope_opener'] + 1); $i < $nextBreak; $i++) {
268
+							if ($tokens[$i]['type'] !== 'T_WHITESPACE') {
269
+								$foundContent = true;
270
+								break;
271
+							}
272
+						}
273
+
274
+						if ($foundContent === false) {
275
+							$error = 'Comment required for empty DEFAULT case';
276
+							$phpcsFile->addError($error, $nextCase, 'EmptyDefault');
277
+						}
278
+					}//end if
279
+				}//end if
280
+			} else if ($type === 'Default') {
281
+				$error = 'DEFAULT case must have a breaking statement';
282
+				$phpcsFile->addError($error, $nextCase, 'DefaultNoBreak');
283
+			}//end if
284
+		}//end while
285
+
286
+		if ($foundDefault === false) {
287
+			$error = 'All SWITCH statements must contain a DEFAULT case';
288
+			$phpcsFile->addError($error, $stackPtr, 'MissingDefault');
289
+		}
290
+
291
+		if ($tokens[$switch['scope_closer']]['column'] !== $switch['column']) {
292
+			$error = 'Closing brace of SWITCH statement must be aligned with SWITCH keyword';
293
+			$phpcsFile->addError($error, $switch['scope_closer'], 'CloseBraceAlign');
294
+		}
295
+
296
+		if ($caseCount === 0) {
297
+			$error = 'SWITCH statements must contain at least one CASE statement';
298
+			$phpcsFile->addError($error, $stackPtr, 'MissingCase');
299
+		}
300
+
301
+	}//end process()
302 302
 
303 303
 
304 304
 }//end class
Please login to merge, or discard this patch.
src/Standards/Squiz/Sniffs/ControlStructures/ElseIfDeclarationSniff.php 1 patch
Indentation   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -16,36 +16,36 @@
 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_ELSEIF];
27
-
28
-    }//end register()
29
-
30
-
31
-    /**
32
-     * Processes this test, 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 the
36
-     *                                               stack passed in $tokens.
37
-     *
38
-     * @return void
39
-     */
40
-    public function process(File $phpcsFile, $stackPtr)
41
-    {
42
-        $error = 'Usage of ELSEIF not allowed; use ELSE IF instead';
43
-        $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NotAllowed');
44
-        if ($fix === true) {
45
-            $phpcsFile->fixer->replaceToken($stackPtr, 'else if');
46
-        }
47
-
48
-    }//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_ELSEIF];
27
+
28
+	}//end register()
29
+
30
+
31
+	/**
32
+	 * Processes this test, 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 the
36
+	 *                                               stack passed in $tokens.
37
+	 *
38
+	 * @return void
39
+	 */
40
+	public function process(File $phpcsFile, $stackPtr)
41
+	{
42
+		$error = 'Usage of ELSEIF not allowed; use ELSE IF instead';
43
+		$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NotAllowed');
44
+		if ($fix === true) {
45
+			$phpcsFile->fixer->replaceToken($stackPtr, 'else if');
46
+		}
47
+
48
+	}//end process()
49 49
 
50 50
 
51 51
 }//end class
Please login to merge, or discard this patch.
Standards/Squiz/Sniffs/ControlStructures/ForEachLoopDeclarationSniff.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -91,7 +91,7 @@  discard block
 block discarded – undo
91 91
                     $this->requiredSpacesAfterOpen,
92 92
                     $spaceAfterOpen,
93 93
                 ];
94
-                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data);
94
+                $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data);
95 95
                 if ($fix === true) {
96 96
                     $padding = str_repeat(' ', $this->requiredSpacesAfterOpen);
97 97
                     if ($spaceAfterOpen === 0) {
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
                     $this->requiredSpacesBeforeClose,
122 122
                     $spaceBeforeClose,
123 123
                 ];
124
-                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose', $data);
124
+                $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose', $data);
125 125
                 if ($fix === true) {
126 126
                     $padding = str_repeat(' ', $this->requiredSpacesBeforeClose);
127 127
                     if ($spaceBeforeClose === 0) {
Please login to merge, or discard this patch.
Indentation   +216 added lines, -216 removed lines patch added patch discarded remove patch
@@ -15,222 +15,222 @@
 block discarded – undo
15 15
 class ForEachLoopDeclarationSniff implements Sniff
16 16
 {
17 17
 
18
-    /**
19
-     * How many spaces should follow the opening bracket.
20
-     *
21
-     * @var integer
22
-     */
23
-    public $requiredSpacesAfterOpen = 0;
24
-
25
-    /**
26
-     * How many spaces should precede the closing bracket.
27
-     *
28
-     * @var integer
29
-     */
30
-    public $requiredSpacesBeforeClose = 0;
31
-
32
-
33
-    /**
34
-     * Returns an array of tokens this test wants to listen for.
35
-     *
36
-     * @return array
37
-     */
38
-    public function register()
39
-    {
40
-        return [T_FOREACH];
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 in the
50
-     *                                               stack passed in $tokens.
51
-     *
52
-     * @return void
53
-     */
54
-    public function process(File $phpcsFile, $stackPtr)
55
-    {
56
-        $this->requiredSpacesAfterOpen   = (int) $this->requiredSpacesAfterOpen;
57
-        $this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
58
-        $tokens = $phpcsFile->getTokens();
59
-
60
-        $openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
61
-        if ($openingBracket === false) {
62
-            $error = 'Possible parse error: FOREACH has no opening parenthesis';
63
-            $phpcsFile->addWarning($error, $stackPtr, 'MissingOpenParenthesis');
64
-            return;
65
-        }
66
-
67
-        if (isset($tokens[$openingBracket]['parenthesis_closer']) === false) {
68
-            $error = 'Possible parse error: FOREACH has no closing parenthesis';
69
-            $phpcsFile->addWarning($error, $stackPtr, 'MissingCloseParenthesis');
70
-            return;
71
-        }
72
-
73
-        $closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
74
-
75
-        if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
76
-            $error = 'Space found after opening bracket of FOREACH loop';
77
-            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen');
78
-            if ($fix === true) {
79
-                $phpcsFile->fixer->replaceToken(($openingBracket + 1), '');
80
-            }
81
-        } else if ($this->requiredSpacesAfterOpen > 0) {
82
-            $spaceAfterOpen = 0;
83
-            if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
84
-                $spaceAfterOpen = $tokens[($openingBracket + 1)]['length'];
85
-            }
86
-
87
-            if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
88
-                $error = 'Expected %s spaces after opening bracket; %s found';
89
-                $data  = [
90
-                    $this->requiredSpacesAfterOpen,
91
-                    $spaceAfterOpen,
92
-                ];
93
-                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data);
94
-                if ($fix === true) {
95
-                    $padding = str_repeat(' ', $this->requiredSpacesAfterOpen);
96
-                    if ($spaceAfterOpen === 0) {
97
-                        $phpcsFile->fixer->addContent($openingBracket, $padding);
98
-                    } else {
99
-                        $phpcsFile->fixer->replaceToken(($openingBracket + 1), $padding);
100
-                    }
101
-                }
102
-            }
103
-        }//end if
104
-
105
-        if ($this->requiredSpacesBeforeClose === 0 && $tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
106
-            $error = 'Space found before closing bracket of FOREACH loop';
107
-            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose');
108
-            if ($fix === true) {
109
-                $phpcsFile->fixer->replaceToken(($closingBracket - 1), '');
110
-            }
111
-        } else if ($this->requiredSpacesBeforeClose > 0) {
112
-            $spaceBeforeClose = 0;
113
-            if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
114
-                $spaceBeforeClose = $tokens[($closingBracket - 1)]['length'];
115
-            }
116
-
117
-            if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
118
-                $error = 'Expected %s spaces before closing bracket; %s found';
119
-                $data  = [
120
-                    $this->requiredSpacesBeforeClose,
121
-                    $spaceBeforeClose,
122
-                ];
123
-                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose', $data);
124
-                if ($fix === true) {
125
-                    $padding = str_repeat(' ', $this->requiredSpacesBeforeClose);
126
-                    if ($spaceBeforeClose === 0) {
127
-                        $phpcsFile->fixer->addContentBefore($closingBracket, $padding);
128
-                    } else {
129
-                        $phpcsFile->fixer->replaceToken(($closingBracket - 1), $padding);
130
-                    }
131
-                }
132
-            }
133
-        }//end if
134
-
135
-        $asToken = $phpcsFile->findNext(T_AS, $openingBracket);
136
-        if ($asToken === false) {
137
-            $error = 'Possible parse error: FOREACH has no AS statement';
138
-            $phpcsFile->addWarning($error, $stackPtr, 'MissingAs');
139
-            return;
140
-        }
141
-
142
-        $content = $tokens[$asToken]['content'];
143
-        if ($content !== strtolower($content)) {
144
-            $expected = strtolower($content);
145
-            $error    = 'AS keyword must be lowercase; expected "%s" but found "%s"';
146
-            $data     = [
147
-                $expected,
148
-                $content,
149
-            ];
150
-
151
-            $fix = $phpcsFile->addFixableError($error, $asToken, 'AsNotLower', $data);
152
-            if ($fix === true) {
153
-                $phpcsFile->fixer->replaceToken($asToken, $expected);
154
-            }
155
-        }
156
-
157
-        $doubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $asToken, $closingBracket);
158
-
159
-        if ($doubleArrow !== false) {
160
-            if ($tokens[($doubleArrow - 1)]['code'] !== T_WHITESPACE) {
161
-                $error = 'Expected 1 space before "=>"; 0 found';
162
-                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeArrow');
163
-                if ($fix === true) {
164
-                    $phpcsFile->fixer->addContentBefore($doubleArrow, ' ');
165
-                }
166
-            } else {
167
-                if ($tokens[($doubleArrow - 1)]['length'] !== 1) {
168
-                    $spaces = $tokens[($doubleArrow - 1)]['length'];
169
-                    $error  = 'Expected 1 space before "=>"; %s found';
170
-                    $data   = [$spaces];
171
-                    $fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeArrow', $data);
172
-                    if ($fix === true) {
173
-                        $phpcsFile->fixer->replaceToken(($doubleArrow - 1), ' ');
174
-                    }
175
-                }
176
-            }
177
-
178
-            if ($tokens[($doubleArrow + 1)]['code'] !== T_WHITESPACE) {
179
-                $error = 'Expected 1 space after "=>"; 0 found';
180
-                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterArrow');
181
-                if ($fix === true) {
182
-                    $phpcsFile->fixer->addContent($doubleArrow, ' ');
183
-                }
184
-            } else {
185
-                if ($tokens[($doubleArrow + 1)]['length'] !== 1) {
186
-                    $spaces = $tokens[($doubleArrow + 1)]['length'];
187
-                    $error  = 'Expected 1 space after "=>"; %s found';
188
-                    $data   = [$spaces];
189
-                    $fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterArrow', $data);
190
-                    if ($fix === true) {
191
-                        $phpcsFile->fixer->replaceToken(($doubleArrow + 1), ' ');
192
-                    }
193
-                }
194
-            }
195
-        }//end if
196
-
197
-        if ($tokens[($asToken - 1)]['code'] !== T_WHITESPACE) {
198
-            $error = 'Expected 1 space before "as"; 0 found';
199
-            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeAs');
200
-            if ($fix === true) {
201
-                $phpcsFile->fixer->addContentBefore($asToken, ' ');
202
-            }
203
-        } else {
204
-            if ($tokens[($asToken - 1)]['length'] !== 1) {
205
-                $spaces = $tokens[($asToken - 1)]['length'];
206
-                $error  = 'Expected 1 space before "as"; %s found';
207
-                $data   = [$spaces];
208
-                $fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeAs', $data);
209
-                if ($fix === true) {
210
-                    $phpcsFile->fixer->replaceToken(($asToken - 1), ' ');
211
-                }
212
-            }
213
-        }
214
-
215
-        if ($tokens[($asToken + 1)]['code'] !== T_WHITESPACE) {
216
-            $error = 'Expected 1 space after "as"; 0 found';
217
-            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterAs');
218
-            if ($fix === true) {
219
-                $phpcsFile->fixer->addContent($asToken, ' ');
220
-            }
221
-        } else {
222
-            if ($tokens[($asToken + 1)]['length'] !== 1) {
223
-                $spaces = $tokens[($asToken + 1)]['length'];
224
-                $error  = 'Expected 1 space after "as"; %s found';
225
-                $data   = [$spaces];
226
-                $fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterAs', $data);
227
-                if ($fix === true) {
228
-                    $phpcsFile->fixer->replaceToken(($asToken + 1), ' ');
229
-                }
230
-            }
231
-        }
232
-
233
-    }//end process()
18
+	/**
19
+	 * How many spaces should follow the opening bracket.
20
+	 *
21
+	 * @var integer
22
+	 */
23
+	public $requiredSpacesAfterOpen = 0;
24
+
25
+	/**
26
+	 * How many spaces should precede the closing bracket.
27
+	 *
28
+	 * @var integer
29
+	 */
30
+	public $requiredSpacesBeforeClose = 0;
31
+
32
+
33
+	/**
34
+	 * Returns an array of tokens this test wants to listen for.
35
+	 *
36
+	 * @return array
37
+	 */
38
+	public function register()
39
+	{
40
+		return [T_FOREACH];
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 in the
50
+	 *                                               stack passed in $tokens.
51
+	 *
52
+	 * @return void
53
+	 */
54
+	public function process(File $phpcsFile, $stackPtr)
55
+	{
56
+		$this->requiredSpacesAfterOpen   = (int) $this->requiredSpacesAfterOpen;
57
+		$this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
58
+		$tokens = $phpcsFile->getTokens();
59
+
60
+		$openingBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr);
61
+		if ($openingBracket === false) {
62
+			$error = 'Possible parse error: FOREACH has no opening parenthesis';
63
+			$phpcsFile->addWarning($error, $stackPtr, 'MissingOpenParenthesis');
64
+			return;
65
+		}
66
+
67
+		if (isset($tokens[$openingBracket]['parenthesis_closer']) === false) {
68
+			$error = 'Possible parse error: FOREACH has no closing parenthesis';
69
+			$phpcsFile->addWarning($error, $stackPtr, 'MissingCloseParenthesis');
70
+			return;
71
+		}
72
+
73
+		$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
74
+
75
+		if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
76
+			$error = 'Space found after opening bracket of FOREACH loop';
77
+			$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceAfterOpen');
78
+			if ($fix === true) {
79
+				$phpcsFile->fixer->replaceToken(($openingBracket + 1), '');
80
+			}
81
+		} else if ($this->requiredSpacesAfterOpen > 0) {
82
+			$spaceAfterOpen = 0;
83
+			if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
84
+				$spaceAfterOpen = $tokens[($openingBracket + 1)]['length'];
85
+			}
86
+
87
+			if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
88
+				$error = 'Expected %s spaces after opening bracket; %s found';
89
+				$data  = [
90
+					$this->requiredSpacesAfterOpen,
91
+					$spaceAfterOpen,
92
+				];
93
+				$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data);
94
+				if ($fix === true) {
95
+					$padding = str_repeat(' ', $this->requiredSpacesAfterOpen);
96
+					if ($spaceAfterOpen === 0) {
97
+						$phpcsFile->fixer->addContent($openingBracket, $padding);
98
+					} else {
99
+						$phpcsFile->fixer->replaceToken(($openingBracket + 1), $padding);
100
+					}
101
+				}
102
+			}
103
+		}//end if
104
+
105
+		if ($this->requiredSpacesBeforeClose === 0 && $tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
106
+			$error = 'Space found before closing bracket of FOREACH loop';
107
+			$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose');
108
+			if ($fix === true) {
109
+				$phpcsFile->fixer->replaceToken(($closingBracket - 1), '');
110
+			}
111
+		} else if ($this->requiredSpacesBeforeClose > 0) {
112
+			$spaceBeforeClose = 0;
113
+			if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
114
+				$spaceBeforeClose = $tokens[($closingBracket - 1)]['length'];
115
+			}
116
+
117
+			if ($spaceBeforeClose !== $this->requiredSpacesBeforeClose) {
118
+				$error = 'Expected %s spaces before closing bracket; %s found';
119
+				$data  = [
120
+					$this->requiredSpacesBeforeClose,
121
+					$spaceBeforeClose,
122
+				];
123
+				$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeClose', $data);
124
+				if ($fix === true) {
125
+					$padding = str_repeat(' ', $this->requiredSpacesBeforeClose);
126
+					if ($spaceBeforeClose === 0) {
127
+						$phpcsFile->fixer->addContentBefore($closingBracket, $padding);
128
+					} else {
129
+						$phpcsFile->fixer->replaceToken(($closingBracket - 1), $padding);
130
+					}
131
+				}
132
+			}
133
+		}//end if
134
+
135
+		$asToken = $phpcsFile->findNext(T_AS, $openingBracket);
136
+		if ($asToken === false) {
137
+			$error = 'Possible parse error: FOREACH has no AS statement';
138
+			$phpcsFile->addWarning($error, $stackPtr, 'MissingAs');
139
+			return;
140
+		}
141
+
142
+		$content = $tokens[$asToken]['content'];
143
+		if ($content !== strtolower($content)) {
144
+			$expected = strtolower($content);
145
+			$error    = 'AS keyword must be lowercase; expected "%s" but found "%s"';
146
+			$data     = [
147
+				$expected,
148
+				$content,
149
+			];
150
+
151
+			$fix = $phpcsFile->addFixableError($error, $asToken, 'AsNotLower', $data);
152
+			if ($fix === true) {
153
+				$phpcsFile->fixer->replaceToken($asToken, $expected);
154
+			}
155
+		}
156
+
157
+		$doubleArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $asToken, $closingBracket);
158
+
159
+		if ($doubleArrow !== false) {
160
+			if ($tokens[($doubleArrow - 1)]['code'] !== T_WHITESPACE) {
161
+				$error = 'Expected 1 space before "=>"; 0 found';
162
+				$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeArrow');
163
+				if ($fix === true) {
164
+					$phpcsFile->fixer->addContentBefore($doubleArrow, ' ');
165
+				}
166
+			} else {
167
+				if ($tokens[($doubleArrow - 1)]['length'] !== 1) {
168
+					$spaces = $tokens[($doubleArrow - 1)]['length'];
169
+					$error  = 'Expected 1 space before "=>"; %s found';
170
+					$data   = [$spaces];
171
+					$fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeArrow', $data);
172
+					if ($fix === true) {
173
+						$phpcsFile->fixer->replaceToken(($doubleArrow - 1), ' ');
174
+					}
175
+				}
176
+			}
177
+
178
+			if ($tokens[($doubleArrow + 1)]['code'] !== T_WHITESPACE) {
179
+				$error = 'Expected 1 space after "=>"; 0 found';
180
+				$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterArrow');
181
+				if ($fix === true) {
182
+					$phpcsFile->fixer->addContent($doubleArrow, ' ');
183
+				}
184
+			} else {
185
+				if ($tokens[($doubleArrow + 1)]['length'] !== 1) {
186
+					$spaces = $tokens[($doubleArrow + 1)]['length'];
187
+					$error  = 'Expected 1 space after "=>"; %s found';
188
+					$data   = [$spaces];
189
+					$fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterArrow', $data);
190
+					if ($fix === true) {
191
+						$phpcsFile->fixer->replaceToken(($doubleArrow + 1), ' ');
192
+					}
193
+				}
194
+			}
195
+		}//end if
196
+
197
+		if ($tokens[($asToken - 1)]['code'] !== T_WHITESPACE) {
198
+			$error = 'Expected 1 space before "as"; 0 found';
199
+			$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBeforeAs');
200
+			if ($fix === true) {
201
+				$phpcsFile->fixer->addContentBefore($asToken, ' ');
202
+			}
203
+		} else {
204
+			if ($tokens[($asToken - 1)]['length'] !== 1) {
205
+				$spaces = $tokens[($asToken - 1)]['length'];
206
+				$error  = 'Expected 1 space before "as"; %s found';
207
+				$data   = [$spaces];
208
+				$fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeAs', $data);
209
+				if ($fix === true) {
210
+					$phpcsFile->fixer->replaceToken(($asToken - 1), ' ');
211
+				}
212
+			}
213
+		}
214
+
215
+		if ($tokens[($asToken + 1)]['code'] !== T_WHITESPACE) {
216
+			$error = 'Expected 1 space after "as"; 0 found';
217
+			$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceAfterAs');
218
+			if ($fix === true) {
219
+				$phpcsFile->fixer->addContent($asToken, ' ');
220
+			}
221
+		} else {
222
+			if ($tokens[($asToken + 1)]['length'] !== 1) {
223
+				$spaces = $tokens[($asToken + 1)]['length'];
224
+				$error  = 'Expected 1 space after "as"; %s found';
225
+				$data   = [$spaces];
226
+				$fix    = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterAs', $data);
227
+				if ($fix === true) {
228
+					$phpcsFile->fixer->replaceToken(($asToken + 1), ' ');
229
+				}
230
+			}
231
+		}
232
+
233
+	}//end process()
234 234
 
235 235
 
236 236
 }//end class
Please login to merge, or discard this patch.
src/Standards/Squiz/Sniffs/ControlStructures/InlineIfDeclarationSniff.php 1 patch
Indentation   +134 added lines, -134 removed lines patch added patch discarded remove patch
@@ -16,140 +16,140 @@
 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_INLINE_THEN];
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 the
36
-     *                                               stack passed in $tokens.
37
-     *
38
-     * @return void
39
-     */
40
-    public function process(File $phpcsFile, $stackPtr)
41
-    {
42
-        $tokens = $phpcsFile->getTokens();
43
-
44
-        $openBracket  = null;
45
-        $closeBracket = null;
46
-        if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
47
-            $parens       = $tokens[$stackPtr]['nested_parenthesis'];
48
-            $openBracket  = array_pop($parens);
49
-            $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
50
-        }
51
-
52
-        // Find the beginning of the statement. If we don't find a
53
-        // semicolon (end of statement) or comma (end of array value)
54
-        // then assume the content before the closing parenthesis is the end.
55
-        $else         = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1));
56
-        $statementEnd = $phpcsFile->findNext([T_SEMICOLON, T_COMMA], ($else + 1), $closeBracket);
57
-        if ($statementEnd === false) {
58
-            $statementEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true);
59
-        }
60
-
61
-        // Make sure it's all on the same line.
62
-        if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) {
63
-            $error = 'Inline shorthand IF statement must be declared on a single line';
64
-            $phpcsFile->addError($error, $stackPtr, 'NotSingleLine');
65
-            return;
66
-        }
67
-
68
-        // Make sure there are spaces around the question mark.
69
-        $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
70
-        $contentAfter  = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
71
-        if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) {
72
-            $error = 'Inline shorthand IF statement requires brackets around comparison';
73
-            $phpcsFile->addError($error, $stackPtr, 'NoBrackets');
74
-        }
75
-
76
-        $spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length']));
77
-        if ($spaceBefore !== 1) {
78
-            $error = 'Inline shorthand IF statement requires 1 space before THEN; %s found';
79
-            $data  = [$spaceBefore];
80
-            $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeThen', $data);
81
-            if ($fix === true) {
82
-                if ($spaceBefore === 0) {
83
-                    $phpcsFile->fixer->addContentBefore($stackPtr, ' ');
84
-                } else {
85
-                    $phpcsFile->fixer->replaceToken(($stackPtr - 1), ' ');
86
-                }
87
-            }
88
-        }
89
-
90
-        // If there is no content between the ? and the : operators, then they are
91
-        // trying to replicate an elvis operator, even though PHP doesn't have one.
92
-        // In this case, we want no spaces between the two operators so ?: looks like
93
-        // an operator itself.
94
-        $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
95
-        if ($tokens[$next]['code'] === T_INLINE_ELSE) {
96
-            $inlineElse = $next;
97
-            if ($inlineElse !== ($stackPtr + 1)) {
98
-                $error = 'Inline shorthand IF statement without THEN statement requires 0 spaces between THEN and ELSE';
99
-                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'ElvisSpacing');
100
-                if ($fix === true) {
101
-                    $phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
102
-                }
103
-            }
104
-        } else {
105
-            $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
106
-            if ($spaceAfter !== 1) {
107
-                $error = 'Inline shorthand IF statement requires 1 space after THEN; %s found';
108
-                $data  = [$spaceAfter];
109
-                $fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterThen', $data);
110
-                if ($fix === true) {
111
-                    if ($spaceAfter === 0) {
112
-                        $phpcsFile->fixer->addContent($stackPtr, ' ');
113
-                    } else {
114
-                        $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
115
-                    }
116
-                }
117
-            }
118
-
119
-            // Make sure the ELSE has the correct spacing.
120
-            $inlineElse    = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false);
121
-            $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true);
122
-            $spaceBefore   = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length']));
123
-            if ($spaceBefore !== 1) {
124
-                $error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
125
-                $data  = [$spaceBefore];
126
-                $fix   = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingBeforeElse', $data);
127
-                if ($fix === true) {
128
-                    if ($spaceBefore === 0) {
129
-                        $phpcsFile->fixer->addContentBefore($inlineElse, ' ');
130
-                    } else {
131
-                        $phpcsFile->fixer->replaceToken(($inlineElse - 1), ' ');
132
-                    }
133
-                }
134
-            }
135
-        }//end if
136
-
137
-        $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true);
138
-        $spaceAfter   = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1));
139
-        if ($spaceAfter !== 1) {
140
-            $error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found';
141
-            $data  = [$spaceAfter];
142
-            $fix   = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingAfterElse', $data);
143
-            if ($fix === true) {
144
-                if ($spaceAfter === 0) {
145
-                    $phpcsFile->fixer->addContent($inlineElse, ' ');
146
-                } else {
147
-                    $phpcsFile->fixer->replaceToken(($inlineElse + 1), ' ');
148
-                }
149
-            }
150
-        }
151
-
152
-    }//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_INLINE_THEN];
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 the
36
+	 *                                               stack passed in $tokens.
37
+	 *
38
+	 * @return void
39
+	 */
40
+	public function process(File $phpcsFile, $stackPtr)
41
+	{
42
+		$tokens = $phpcsFile->getTokens();
43
+
44
+		$openBracket  = null;
45
+		$closeBracket = null;
46
+		if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
47
+			$parens       = $tokens[$stackPtr]['nested_parenthesis'];
48
+			$openBracket  = array_pop($parens);
49
+			$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
50
+		}
51
+
52
+		// Find the beginning of the statement. If we don't find a
53
+		// semicolon (end of statement) or comma (end of array value)
54
+		// then assume the content before the closing parenthesis is the end.
55
+		$else         = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1));
56
+		$statementEnd = $phpcsFile->findNext([T_SEMICOLON, T_COMMA], ($else + 1), $closeBracket);
57
+		if ($statementEnd === false) {
58
+			$statementEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true);
59
+		}
60
+
61
+		// Make sure it's all on the same line.
62
+		if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) {
63
+			$error = 'Inline shorthand IF statement must be declared on a single line';
64
+			$phpcsFile->addError($error, $stackPtr, 'NotSingleLine');
65
+			return;
66
+		}
67
+
68
+		// Make sure there are spaces around the question mark.
69
+		$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
70
+		$contentAfter  = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
71
+		if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) {
72
+			$error = 'Inline shorthand IF statement requires brackets around comparison';
73
+			$phpcsFile->addError($error, $stackPtr, 'NoBrackets');
74
+		}
75
+
76
+		$spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length']));
77
+		if ($spaceBefore !== 1) {
78
+			$error = 'Inline shorthand IF statement requires 1 space before THEN; %s found';
79
+			$data  = [$spaceBefore];
80
+			$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeThen', $data);
81
+			if ($fix === true) {
82
+				if ($spaceBefore === 0) {
83
+					$phpcsFile->fixer->addContentBefore($stackPtr, ' ');
84
+				} else {
85
+					$phpcsFile->fixer->replaceToken(($stackPtr - 1), ' ');
86
+				}
87
+			}
88
+		}
89
+
90
+		// If there is no content between the ? and the : operators, then they are
91
+		// trying to replicate an elvis operator, even though PHP doesn't have one.
92
+		// In this case, we want no spaces between the two operators so ?: looks like
93
+		// an operator itself.
94
+		$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
95
+		if ($tokens[$next]['code'] === T_INLINE_ELSE) {
96
+			$inlineElse = $next;
97
+			if ($inlineElse !== ($stackPtr + 1)) {
98
+				$error = 'Inline shorthand IF statement without THEN statement requires 0 spaces between THEN and ELSE';
99
+				$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'ElvisSpacing');
100
+				if ($fix === true) {
101
+					$phpcsFile->fixer->replaceToken(($stackPtr + 1), '');
102
+				}
103
+			}
104
+		} else {
105
+			$spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1));
106
+			if ($spaceAfter !== 1) {
107
+				$error = 'Inline shorthand IF statement requires 1 space after THEN; %s found';
108
+				$data  = [$spaceAfter];
109
+				$fix   = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterThen', $data);
110
+				if ($fix === true) {
111
+					if ($spaceAfter === 0) {
112
+						$phpcsFile->fixer->addContent($stackPtr, ' ');
113
+					} else {
114
+						$phpcsFile->fixer->replaceToken(($stackPtr + 1), ' ');
115
+					}
116
+				}
117
+			}
118
+
119
+			// Make sure the ELSE has the correct spacing.
120
+			$inlineElse    = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false);
121
+			$contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true);
122
+			$spaceBefore   = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length']));
123
+			if ($spaceBefore !== 1) {
124
+				$error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found';
125
+				$data  = [$spaceBefore];
126
+				$fix   = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingBeforeElse', $data);
127
+				if ($fix === true) {
128
+					if ($spaceBefore === 0) {
129
+						$phpcsFile->fixer->addContentBefore($inlineElse, ' ');
130
+					} else {
131
+						$phpcsFile->fixer->replaceToken(($inlineElse - 1), ' ');
132
+					}
133
+				}
134
+			}
135
+		}//end if
136
+
137
+		$contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true);
138
+		$spaceAfter   = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1));
139
+		if ($spaceAfter !== 1) {
140
+			$error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found';
141
+			$data  = [$spaceAfter];
142
+			$fix   = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingAfterElse', $data);
143
+			if ($fix === true) {
144
+				if ($spaceAfter === 0) {
145
+					$phpcsFile->fixer->addContent($inlineElse, ' ');
146
+				} else {
147
+					$phpcsFile->fixer->replaceToken(($inlineElse + 1), ' ');
148
+				}
149
+			}
150
+		}
151
+
152
+	}//end process()
153 153
 
154 154
 
155 155
 }//end class
Please login to merge, or discard this patch.
src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationSniff.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -15,20 +15,20 @@
 block discarded – undo
15 15
 {
16 16
 
17 17
 
18
-    /**
19
-     * Returns an array of patterns to check are correct.
20
-     *
21
-     * @return array
22
-     */
23
-    protected function getPatterns()
24
-    {
25
-        return [
26
-            'function abc(...);',
27
-            'function abc(...)',
28
-            'abstract function abc(...);',
29
-        ];
30
-
31
-    }//end getPatterns()
18
+	/**
19
+	 * Returns an array of patterns to check are correct.
20
+	 *
21
+	 * @return array
22
+	 */
23
+	protected function getPatterns()
24
+	{
25
+		return [
26
+			'function abc(...);',
27
+			'function abc(...)',
28
+			'abstract function abc(...);',
29
+		];
30
+
31
+	}//end getPatterns()
32 32
 
33 33
 
34 34
 }//end class
Please login to merge, or discard this patch.
src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -140,7 +140,7 @@
 block discarded – undo
140 140
      *
141 141
      * @return void
142 142
      */
143
-    public function processBracket($phpcsFile, $openBracket, $tokens, $type='function')
143
+    public function processBracket($phpcsFile, $openBracket, $tokens, $type = 'function')
144 144
     {
145 145
         $errorPrefix = '';
146 146
         if ($type === 'use') {
Please login to merge, or discard this patch.
Indentation   +238 added lines, -238 removed lines patch added patch discarded remove patch
@@ -15,244 +15,244 @@
 block discarded – undo
15 15
 class MultiLineFunctionDeclarationSniff extends PEARFunctionDeclarationSniff
16 16
 {
17 17
 
18
-    /**
19
-     * A list of tokenizers this sniff supports.
20
-     *
21
-     * @var array
22
-     */
23
-    public $supportedTokenizers = [
24
-        'PHP',
25
-        'JS',
26
-    ];
27
-
28
-
29
-    /**
30
-     * Determine if this is a multi-line function declaration.
31
-     *
32
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
33
-     * @param int                         $stackPtr    The position of the current token
34
-     *                                                 in the stack passed in $tokens.
35
-     * @param int                         $openBracket The position of the opening bracket
36
-     *                                                 in the stack passed in $tokens.
37
-     * @param array                       $tokens      The stack of tokens that make up
38
-     *                                                 the file.
39
-     *
40
-     * @return void
41
-     */
42
-    public function isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens)
43
-    {
44
-        $bracketsToCheck = [$stackPtr => $openBracket];
45
-
46
-        // Closures may use the USE keyword and so be multi-line in this way.
47
-        if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
48
-            $use = $phpcsFile->findNext(T_USE, ($tokens[$openBracket]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
49
-            if ($use !== false) {
50
-                $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
51
-                if ($open !== false) {
52
-                    $bracketsToCheck[$use] = $open;
53
-                }
54
-            }
55
-        }
56
-
57
-        foreach ($bracketsToCheck as $stackPtr => $openBracket) {
58
-            // If the first argument is on a new line, this is a multi-line
59
-            // function declaration, even if there is only one argument.
60
-            $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
61
-            if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
62
-                return true;
63
-            }
64
-
65
-            $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
66
-
67
-            $end = $phpcsFile->findEndOfStatement($openBracket + 1);
68
-            while ($tokens[$end]['code'] === T_COMMA) {
69
-                // If the next bit of code is not on the same line, this is a
70
-                // multi-line function declaration.
71
-                $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), $closeBracket, true);
72
-                if ($next === false) {
73
-                    continue(2);
74
-                }
75
-
76
-                if ($tokens[$next]['line'] !== $tokens[$end]['line']) {
77
-                    return true;
78
-                }
79
-
80
-                $end = $phpcsFile->findEndOfStatement($next);
81
-            }
82
-
83
-            // We've reached the last argument, so see if the next content
84
-            // (should be the close bracket) is also on the same line.
85
-            $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), $closeBracket, true);
86
-            if ($next !== false && $tokens[$next]['line'] !== $tokens[$end]['line']) {
87
-                return true;
88
-            }
89
-        }//end foreach
90
-
91
-        return false;
92
-
93
-    }//end isMultiLineDeclaration()
94
-
95
-
96
-    /**
97
-     * Processes single-line declarations.
98
-     *
99
-     * Just uses the Generic BSD-Allman brace sniff.
100
-     *
101
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
102
-     * @param int                         $stackPtr  The position of the current token
103
-     *                                               in the stack passed in $tokens.
104
-     * @param array                       $tokens    The stack of tokens that make up
105
-     *                                               the file.
106
-     *
107
-     * @return void
108
-     */
109
-    public function processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens)
110
-    {
111
-        // We do everything the parent sniff does, and a bit more because we
112
-        // define multi-line declarations a bit differently.
113
-        parent::processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
114
-
115
-        $openingBracket = $tokens[$stackPtr]['parenthesis_opener'];
116
-        $closingBracket = $tokens[$stackPtr]['parenthesis_closer'];
117
-
118
-        $prevNonWhiteSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingBracket - 1), $openingBracket, true);
119
-        if ($tokens[$prevNonWhiteSpace]['line'] !== $tokens[$closingBracket]['line']) {
120
-            $error = 'There must not be a newline before the closing parenthesis of a single-line function declaration';
121
-
122
-            if (isset(Tokens::$emptyTokens[$tokens[$prevNonWhiteSpace]['code']]) === true) {
123
-                $phpcsFile->addError($error, $closingBracket, 'CloseBracketNewLine');
124
-            } else {
125
-                $fix = $phpcsFile->addFixableError($error, $closingBracket, 'CloseBracketNewLine');
126
-                if ($fix === true) {
127
-                    $phpcsFile->fixer->beginChangeset();
128
-                    for ($i = ($closingBracket - 1); $i > $openingBracket; $i--) {
129
-                        if ($tokens[$i]['code'] !== T_WHITESPACE) {
130
-                            break;
131
-                        }
132
-
133
-                        $phpcsFile->fixer->replaceToken($i, '');
134
-                    }
135
-
136
-                    $phpcsFile->fixer->endChangeset();
137
-                }
138
-            }
139
-        }//end if
140
-
141
-    }//end processSingleLineDeclaration()
142
-
143
-
144
-    /**
145
-     * Processes multi-line declarations.
146
-     *
147
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
148
-     * @param int                         $stackPtr  The position of the current token
149
-     *                                               in the stack passed in $tokens.
150
-     * @param array                       $tokens    The stack of tokens that make up
151
-     *                                               the file.
152
-     *
153
-     * @return void
154
-     */
155
-    public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
156
-    {
157
-        // We do everything the parent sniff does, and a bit more.
158
-        parent::processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
159
-
160
-        $openBracket = $tokens[$stackPtr]['parenthesis_opener'];
161
-        $this->processBracket($phpcsFile, $openBracket, $tokens, 'function');
162
-
163
-        if ($tokens[$stackPtr]['code'] !== T_CLOSURE) {
164
-            return;
165
-        }
166
-
167
-        $use = $phpcsFile->findNext(T_USE, ($tokens[$stackPtr]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
168
-        if ($use === false) {
169
-            return;
170
-        }
171
-
172
-        $openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1), null);
173
-        $this->processBracket($phpcsFile, $openBracket, $tokens, 'use');
174
-
175
-    }//end processMultiLineDeclaration()
176
-
177
-
178
-    /**
179
-     * Processes the contents of a single set of brackets.
180
-     *
181
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
182
-     * @param int                         $openBracket The position of the open bracket
183
-     *                                                 in the stack passed in $tokens.
184
-     * @param array                       $tokens      The stack of tokens that make up
185
-     *                                                 the file.
186
-     * @param string                      $type        The type of the token the brackets
187
-     *                                                 belong to (function or use).
188
-     *
189
-     * @return void
190
-     */
191
-    public function processBracket($phpcsFile, $openBracket, $tokens, $type='function')
192
-    {
193
-        $errorPrefix = '';
194
-        if ($type === 'use') {
195
-            $errorPrefix = 'Use';
196
-        }
197
-
198
-        $closeBracket = $tokens[$openBracket]['parenthesis_closer'];
199
-
200
-        // The open bracket should be the last thing on the line.
201
-        if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) {
202
-            $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
203
-            if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
204
-                $error = 'The first parameter of a multi-line '.$type.' declaration must be on the line after the opening bracket';
205
-                $fix   = $phpcsFile->addFixableError($error, $next, $errorPrefix.'FirstParamSpacing');
206
-                if ($fix === true) {
207
-                    if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
208
-                        $phpcsFile->fixer->addNewline($openBracket);
209
-                    } else {
210
-                        $phpcsFile->fixer->beginChangeset();
211
-                        for ($x = $openBracket; $x < $next; $x++) {
212
-                            if ($tokens[$x]['line'] === $tokens[$openBracket]['line']) {
213
-                                continue;
214
-                            }
215
-
216
-                            if ($tokens[$x]['line'] === $tokens[$next]['line']) {
217
-                                break;
218
-                            }
219
-                        }
220
-
221
-                        $phpcsFile->fixer->endChangeset();
222
-                    }
223
-                }
224
-            }//end if
225
-        }//end if
226
-
227
-        // Each line between the brackets should contain a single parameter.
228
-        $lastComma = null;
229
-        for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
230
-            // Skip brackets, like arrays, as they can contain commas.
231
-            if (isset($tokens[$i]['bracket_opener']) === true) {
232
-                $i = $tokens[$i]['bracket_closer'];
233
-                continue;
234
-            }
235
-
236
-            if (isset($tokens[$i]['parenthesis_opener']) === true) {
237
-                $i = $tokens[$i]['parenthesis_closer'];
238
-                continue;
239
-            }
240
-
241
-            if ($tokens[$i]['code'] !== T_COMMA) {
242
-                continue;
243
-            }
244
-
245
-            $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
246
-            if ($tokens[$next]['line'] === $tokens[$i]['line']) {
247
-                $error = 'Multi-line '.$type.' declarations must define one parameter per line';
248
-                $fix   = $phpcsFile->addFixableError($error, $next, $errorPrefix.'OneParamPerLine');
249
-                if ($fix === true) {
250
-                    $phpcsFile->fixer->addNewline($i);
251
-                }
252
-            }
253
-        }//end for
254
-
255
-    }//end processBracket()
18
+	/**
19
+	 * A list of tokenizers this sniff supports.
20
+	 *
21
+	 * @var array
22
+	 */
23
+	public $supportedTokenizers = [
24
+		'PHP',
25
+		'JS',
26
+	];
27
+
28
+
29
+	/**
30
+	 * Determine if this is a multi-line function declaration.
31
+	 *
32
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
33
+	 * @param int                         $stackPtr    The position of the current token
34
+	 *                                                 in the stack passed in $tokens.
35
+	 * @param int                         $openBracket The position of the opening bracket
36
+	 *                                                 in the stack passed in $tokens.
37
+	 * @param array                       $tokens      The stack of tokens that make up
38
+	 *                                                 the file.
39
+	 *
40
+	 * @return void
41
+	 */
42
+	public function isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens)
43
+	{
44
+		$bracketsToCheck = [$stackPtr => $openBracket];
45
+
46
+		// Closures may use the USE keyword and so be multi-line in this way.
47
+		if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
48
+			$use = $phpcsFile->findNext(T_USE, ($tokens[$openBracket]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
49
+			if ($use !== false) {
50
+				$open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1));
51
+				if ($open !== false) {
52
+					$bracketsToCheck[$use] = $open;
53
+				}
54
+			}
55
+		}
56
+
57
+		foreach ($bracketsToCheck as $stackPtr => $openBracket) {
58
+			// If the first argument is on a new line, this is a multi-line
59
+			// function declaration, even if there is only one argument.
60
+			$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
61
+			if ($tokens[$next]['line'] !== $tokens[$stackPtr]['line']) {
62
+				return true;
63
+			}
64
+
65
+			$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
66
+
67
+			$end = $phpcsFile->findEndOfStatement($openBracket + 1);
68
+			while ($tokens[$end]['code'] === T_COMMA) {
69
+				// If the next bit of code is not on the same line, this is a
70
+				// multi-line function declaration.
71
+				$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), $closeBracket, true);
72
+				if ($next === false) {
73
+					continue(2);
74
+				}
75
+
76
+				if ($tokens[$next]['line'] !== $tokens[$end]['line']) {
77
+					return true;
78
+				}
79
+
80
+				$end = $phpcsFile->findEndOfStatement($next);
81
+			}
82
+
83
+			// We've reached the last argument, so see if the next content
84
+			// (should be the close bracket) is also on the same line.
85
+			$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($end + 1), $closeBracket, true);
86
+			if ($next !== false && $tokens[$next]['line'] !== $tokens[$end]['line']) {
87
+				return true;
88
+			}
89
+		}//end foreach
90
+
91
+		return false;
92
+
93
+	}//end isMultiLineDeclaration()
94
+
95
+
96
+	/**
97
+	 * Processes single-line declarations.
98
+	 *
99
+	 * Just uses the Generic BSD-Allman brace sniff.
100
+	 *
101
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
102
+	 * @param int                         $stackPtr  The position of the current token
103
+	 *                                               in the stack passed in $tokens.
104
+	 * @param array                       $tokens    The stack of tokens that make up
105
+	 *                                               the file.
106
+	 *
107
+	 * @return void
108
+	 */
109
+	public function processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens)
110
+	{
111
+		// We do everything the parent sniff does, and a bit more because we
112
+		// define multi-line declarations a bit differently.
113
+		parent::processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens);
114
+
115
+		$openingBracket = $tokens[$stackPtr]['parenthesis_opener'];
116
+		$closingBracket = $tokens[$stackPtr]['parenthesis_closer'];
117
+
118
+		$prevNonWhiteSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingBracket - 1), $openingBracket, true);
119
+		if ($tokens[$prevNonWhiteSpace]['line'] !== $tokens[$closingBracket]['line']) {
120
+			$error = 'There must not be a newline before the closing parenthesis of a single-line function declaration';
121
+
122
+			if (isset(Tokens::$emptyTokens[$tokens[$prevNonWhiteSpace]['code']]) === true) {
123
+				$phpcsFile->addError($error, $closingBracket, 'CloseBracketNewLine');
124
+			} else {
125
+				$fix = $phpcsFile->addFixableError($error, $closingBracket, 'CloseBracketNewLine');
126
+				if ($fix === true) {
127
+					$phpcsFile->fixer->beginChangeset();
128
+					for ($i = ($closingBracket - 1); $i > $openingBracket; $i--) {
129
+						if ($tokens[$i]['code'] !== T_WHITESPACE) {
130
+							break;
131
+						}
132
+
133
+						$phpcsFile->fixer->replaceToken($i, '');
134
+					}
135
+
136
+					$phpcsFile->fixer->endChangeset();
137
+				}
138
+			}
139
+		}//end if
140
+
141
+	}//end processSingleLineDeclaration()
142
+
143
+
144
+	/**
145
+	 * Processes multi-line declarations.
146
+	 *
147
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
148
+	 * @param int                         $stackPtr  The position of the current token
149
+	 *                                               in the stack passed in $tokens.
150
+	 * @param array                       $tokens    The stack of tokens that make up
151
+	 *                                               the file.
152
+	 *
153
+	 * @return void
154
+	 */
155
+	public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens)
156
+	{
157
+		// We do everything the parent sniff does, and a bit more.
158
+		parent::processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens);
159
+
160
+		$openBracket = $tokens[$stackPtr]['parenthesis_opener'];
161
+		$this->processBracket($phpcsFile, $openBracket, $tokens, 'function');
162
+
163
+		if ($tokens[$stackPtr]['code'] !== T_CLOSURE) {
164
+			return;
165
+		}
166
+
167
+		$use = $phpcsFile->findNext(T_USE, ($tokens[$stackPtr]['parenthesis_closer'] + 1), $tokens[$stackPtr]['scope_opener']);
168
+		if ($use === false) {
169
+			return;
170
+		}
171
+
172
+		$openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1), null);
173
+		$this->processBracket($phpcsFile, $openBracket, $tokens, 'use');
174
+
175
+	}//end processMultiLineDeclaration()
176
+
177
+
178
+	/**
179
+	 * Processes the contents of a single set of brackets.
180
+	 *
181
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile   The file being scanned.
182
+	 * @param int                         $openBracket The position of the open bracket
183
+	 *                                                 in the stack passed in $tokens.
184
+	 * @param array                       $tokens      The stack of tokens that make up
185
+	 *                                                 the file.
186
+	 * @param string                      $type        The type of the token the brackets
187
+	 *                                                 belong to (function or use).
188
+	 *
189
+	 * @return void
190
+	 */
191
+	public function processBracket($phpcsFile, $openBracket, $tokens, $type='function')
192
+	{
193
+		$errorPrefix = '';
194
+		if ($type === 'use') {
195
+			$errorPrefix = 'Use';
196
+		}
197
+
198
+		$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
199
+
200
+		// The open bracket should be the last thing on the line.
201
+		if ($tokens[$openBracket]['line'] !== $tokens[$closeBracket]['line']) {
202
+			$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
203
+			if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
204
+				$error = 'The first parameter of a multi-line '.$type.' declaration must be on the line after the opening bracket';
205
+				$fix   = $phpcsFile->addFixableError($error, $next, $errorPrefix.'FirstParamSpacing');
206
+				if ($fix === true) {
207
+					if ($tokens[$next]['line'] === $tokens[$openBracket]['line']) {
208
+						$phpcsFile->fixer->addNewline($openBracket);
209
+					} else {
210
+						$phpcsFile->fixer->beginChangeset();
211
+						for ($x = $openBracket; $x < $next; $x++) {
212
+							if ($tokens[$x]['line'] === $tokens[$openBracket]['line']) {
213
+								continue;
214
+							}
215
+
216
+							if ($tokens[$x]['line'] === $tokens[$next]['line']) {
217
+								break;
218
+							}
219
+						}
220
+
221
+						$phpcsFile->fixer->endChangeset();
222
+					}
223
+				}
224
+			}//end if
225
+		}//end if
226
+
227
+		// Each line between the brackets should contain a single parameter.
228
+		$lastComma = null;
229
+		for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
230
+			// Skip brackets, like arrays, as they can contain commas.
231
+			if (isset($tokens[$i]['bracket_opener']) === true) {
232
+				$i = $tokens[$i]['bracket_closer'];
233
+				continue;
234
+			}
235
+
236
+			if (isset($tokens[$i]['parenthesis_opener']) === true) {
237
+				$i = $tokens[$i]['parenthesis_closer'];
238
+				continue;
239
+			}
240
+
241
+			if ($tokens[$i]['code'] !== T_COMMA) {
242
+				continue;
243
+			}
244
+
245
+			$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
246
+			if ($tokens[$next]['line'] === $tokens[$i]['line']) {
247
+				$error = 'Multi-line '.$type.' declarations must define one parameter per line';
248
+				$fix   = $phpcsFile->addFixableError($error, $next, $errorPrefix.'OneParamPerLine');
249
+				if ($fix === true) {
250
+					$phpcsFile->fixer->addNewline($i);
251
+				}
252
+			}
253
+		}//end for
254
+
255
+	}//end processBracket()
256 256
 
257 257
 
258 258
 }//end class
Please login to merge, or discard this patch.
src/Standards/Squiz/Sniffs/Functions/GlobalFunctionSniff.php 1 patch
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -16,46 +16,46 @@
 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_FUNCTION];
27
-
28
-    }//end register()
29
-
30
-
31
-    /**
32
-     * Processes this test, 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 the
36
-     *                                               stack passed in $tokens.
37
-     *
38
-     * @return void
39
-     */
40
-    public function process(File $phpcsFile, $stackPtr)
41
-    {
42
-        $tokens = $phpcsFile->getTokens();
43
-
44
-        if (empty($tokens[$stackPtr]['conditions']) === true) {
45
-            $functionName = $phpcsFile->getDeclarationName($stackPtr);
46
-            if ($functionName === null) {
47
-                return;
48
-            }
49
-
50
-            // Special exception for __autoload as it needs to be global.
51
-            if ($functionName !== '__autoload') {
52
-                $error = 'Consider putting global function "%s" in a static class';
53
-                $data  = [$functionName];
54
-                $phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
55
-            }
56
-        }
57
-
58
-    }//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_FUNCTION];
27
+
28
+	}//end register()
29
+
30
+
31
+	/**
32
+	 * Processes this test, 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 the
36
+	 *                                               stack passed in $tokens.
37
+	 *
38
+	 * @return void
39
+	 */
40
+	public function process(File $phpcsFile, $stackPtr)
41
+	{
42
+		$tokens = $phpcsFile->getTokens();
43
+
44
+		if (empty($tokens[$stackPtr]['conditions']) === true) {
45
+			$functionName = $phpcsFile->getDeclarationName($stackPtr);
46
+			if ($functionName === null) {
47
+				return;
48
+			}
49
+
50
+			// Special exception for __autoload as it needs to be global.
51
+			if ($functionName !== '__autoload') {
52
+				$error = 'Consider putting global function "%s" in a static class';
53
+				$data  = [$functionName];
54
+				$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
55
+			}
56
+		}
57
+
58
+	}//end process()
59 59
 
60 60
 
61 61
 }//end class
Please login to merge, or discard this patch.
src/Standards/Squiz/Sniffs/Objects/DisallowObjectStringIndexSniff.php 1 patch
Indentation   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -15,71 +15,71 @@
 block discarded – undo
15 15
 class DisallowObjectStringIndexSniff implements Sniff
16 16
 {
17 17
 
18
-    /**
19
-     * A list of tokenizers this sniff supports.
20
-     *
21
-     * @var array
22
-     */
23
-    public $supportedTokenizers = ['JS'];
24
-
25
-
26
-    /**
27
-     * Returns an array of tokens this test wants to listen for.
28
-     *
29
-     * @return array
30
-     */
31
-    public function register()
32
-    {
33
-        return [T_OPEN_SQUARE_BRACKET];
34
-
35
-    }//end register()
36
-
37
-
38
-    /**
39
-     * Processes this test, when one of its tokens is encountered.
40
-     *
41
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
42
-     * @param int                         $stackPtr  The position of the current token
43
-     *                                               in the stack passed in $tokens.
44
-     *
45
-     * @return void
46
-     */
47
-    public function process(File $phpcsFile, $stackPtr)
48
-    {
49
-        $tokens = $phpcsFile->getTokens();
50
-
51
-        // Check if the next non whitespace token is a string.
52
-        $index = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
53
-        if ($tokens[$index]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
54
-            return;
55
-        }
56
-
57
-        // Make sure it is the only thing in the square brackets.
58
-        $next = $phpcsFile->findNext(T_WHITESPACE, ($index + 1), null, true);
59
-        if ($tokens[$next]['code'] !== T_CLOSE_SQUARE_BRACKET) {
60
-            return;
61
-        }
62
-
63
-        // Allow indexes that have dots in them because we can't write
64
-        // them in dot notation.
65
-        $content = trim($tokens[$index]['content'], '"\' ');
66
-        if (strpos($content, '.') !== false) {
67
-            return;
68
-        }
69
-
70
-        // Also ignore reserved words.
71
-        if ($content === 'super') {
72
-            return;
73
-        }
74
-
75
-        // Token before the opening square bracket cannot be a var name.
76
-        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
77
-        if ($tokens[$prev]['code'] === T_STRING) {
78
-            $error = 'Object indexes must be written in dot notation';
79
-            $phpcsFile->addError($error, $prev, 'Found');
80
-        }
81
-
82
-    }//end process()
18
+	/**
19
+	 * A list of tokenizers this sniff supports.
20
+	 *
21
+	 * @var array
22
+	 */
23
+	public $supportedTokenizers = ['JS'];
24
+
25
+
26
+	/**
27
+	 * Returns an array of tokens this test wants to listen for.
28
+	 *
29
+	 * @return array
30
+	 */
31
+	public function register()
32
+	{
33
+		return [T_OPEN_SQUARE_BRACKET];
34
+
35
+	}//end register()
36
+
37
+
38
+	/**
39
+	 * Processes this test, when one of its tokens is encountered.
40
+	 *
41
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
42
+	 * @param int                         $stackPtr  The position of the current token
43
+	 *                                               in the stack passed in $tokens.
44
+	 *
45
+	 * @return void
46
+	 */
47
+	public function process(File $phpcsFile, $stackPtr)
48
+	{
49
+		$tokens = $phpcsFile->getTokens();
50
+
51
+		// Check if the next non whitespace token is a string.
52
+		$index = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
53
+		if ($tokens[$index]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
54
+			return;
55
+		}
56
+
57
+		// Make sure it is the only thing in the square brackets.
58
+		$next = $phpcsFile->findNext(T_WHITESPACE, ($index + 1), null, true);
59
+		if ($tokens[$next]['code'] !== T_CLOSE_SQUARE_BRACKET) {
60
+			return;
61
+		}
62
+
63
+		// Allow indexes that have dots in them because we can't write
64
+		// them in dot notation.
65
+		$content = trim($tokens[$index]['content'], '"\' ');
66
+		if (strpos($content, '.') !== false) {
67
+			return;
68
+		}
69
+
70
+		// Also ignore reserved words.
71
+		if ($content === 'super') {
72
+			return;
73
+		}
74
+
75
+		// Token before the opening square bracket cannot be a var name.
76
+		$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
77
+		if ($tokens[$prev]['code'] === T_STRING) {
78
+			$error = 'Object indexes must be written in dot notation';
79
+			$phpcsFile->addError($error, $prev, 'Found');
80
+		}
81
+
82
+	}//end process()
83 83
 
84 84
 
85 85
 }//end class
Please login to merge, or discard this patch.
php_codesniffer/src/Standards/Squiz/Sniffs/CSS/ForbiddenStylesSniff.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -136,7 +136,7 @@
 block discarded – undo
136 136
      *
137 137
      * @return void
138 138
      */
139
-    protected function addError($phpcsFile, $stackPtr, $style, $pattern=null)
139
+    protected function addError($phpcsFile, $stackPtr, $style, $pattern = null)
140 140
     {
141 141
         $data  = [$style];
142 142
         $error = 'The use of style %s is ';
Please login to merge, or discard this patch.
Indentation   +157 added lines, -157 removed lines patch added patch discarded remove patch
@@ -15,163 +15,163 @@
 block discarded – undo
15 15
 class ForbiddenStylesSniff implements Sniff
16 16
 {
17 17
 
18
-    /**
19
-     * A list of tokenizers this sniff supports.
20
-     *
21
-     * @var array
22
-     */
23
-    public $supportedTokenizers = ['CSS'];
24
-
25
-    /**
26
-     * A list of forbidden styles with their alternatives.
27
-     *
28
-     * The value is NULL if no alternative exists. i.e., the
29
-     * style should just not be used.
30
-     *
31
-     * @var array<string, string|null>
32
-     */
33
-    protected $forbiddenStyles = [
34
-        '-moz-border-radius'             => 'border-radius',
35
-        '-webkit-border-radius'          => 'border-radius',
36
-        '-moz-border-radius-topleft'     => 'border-top-left-radius',
37
-        '-moz-border-radius-topright'    => 'border-top-right-radius',
38
-        '-moz-border-radius-bottomright' => 'border-bottom-right-radius',
39
-        '-moz-border-radius-bottomleft'  => 'border-bottom-left-radius',
40
-        '-moz-box-shadow'                => 'box-shadow',
41
-        '-webkit-box-shadow'             => 'box-shadow',
42
-    ];
43
-
44
-    /**
45
-     * A cache of forbidden style names, for faster lookups.
46
-     *
47
-     * @var string[]
48
-     */
49
-    protected $forbiddenStyleNames = [];
50
-
51
-    /**
52
-     * If true, forbidden styles will be considered regular expressions.
53
-     *
54
-     * @var boolean
55
-     */
56
-    protected $patternMatch = false;
57
-
58
-    /**
59
-     * If true, an error will be thrown; otherwise a warning.
60
-     *
61
-     * @var boolean
62
-     */
63
-    public $error = true;
64
-
65
-
66
-    /**
67
-     * Returns an array of tokens this test wants to listen for.
68
-     *
69
-     * @return array
70
-     */
71
-    public function register()
72
-    {
73
-        $this->forbiddenStyleNames = array_keys($this->forbiddenStyles);
74
-
75
-        if ($this->patternMatch === true) {
76
-            foreach ($this->forbiddenStyleNames as $i => $name) {
77
-                $this->forbiddenStyleNames[$i] = '/'.$name.'/i';
78
-            }
79
-        }
80
-
81
-        return [T_STYLE];
82
-
83
-    }//end register()
84
-
85
-
86
-    /**
87
-     * Processes this test, when one of its tokens is encountered.
88
-     *
89
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
90
-     * @param int                         $stackPtr  The position of the current token in
91
-     *                                               the stack passed in $tokens.
92
-     *
93
-     * @return void
94
-     */
95
-    public function process(File $phpcsFile, $stackPtr)
96
-    {
97
-        $tokens  = $phpcsFile->getTokens();
98
-        $style   = strtolower($tokens[$stackPtr]['content']);
99
-        $pattern = null;
100
-
101
-        if ($this->patternMatch === true) {
102
-            $count   = 0;
103
-            $pattern = preg_replace(
104
-                $this->forbiddenStyleNames,
105
-                $this->forbiddenStyleNames,
106
-                $style,
107
-                1,
108
-                $count
109
-            );
110
-
111
-            if ($count === 0) {
112
-                return;
113
-            }
114
-
115
-            // Remove the pattern delimiters and modifier.
116
-            $pattern = substr($pattern, 1, -2);
117
-        } else {
118
-            if (in_array($style, $this->forbiddenStyleNames, true) === false) {
119
-                return;
120
-            }
121
-        }//end if
122
-
123
-        $this->addError($phpcsFile, $stackPtr, $style, $pattern);
124
-
125
-    }//end process()
126
-
127
-
128
-    /**
129
-     * Generates the error or warning for this sniff.
130
-     *
131
-     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
132
-     * @param int                         $stackPtr  The position of the forbidden style
133
-     *                                               in the token array.
134
-     * @param string                      $style     The name of the forbidden style.
135
-     * @param string                      $pattern   The pattern used for the match.
136
-     *
137
-     * @return void
138
-     */
139
-    protected function addError($phpcsFile, $stackPtr, $style, $pattern=null)
140
-    {
141
-        $data  = [$style];
142
-        $error = 'The use of style %s is ';
143
-        if ($this->error === true) {
144
-            $type   = 'Found';
145
-            $error .= 'forbidden';
146
-        } else {
147
-            $type   = 'Discouraged';
148
-            $error .= 'discouraged';
149
-        }
150
-
151
-        if ($pattern === null) {
152
-            $pattern = $style;
153
-        }
154
-
155
-        if ($this->forbiddenStyles[$pattern] !== null) {
156
-            $data[] = $this->forbiddenStyles[$pattern];
157
-            if ($this->error === true) {
158
-                $fix = $phpcsFile->addFixableError($error.'; use %s instead', $stackPtr, $type.'WithAlternative', $data);
159
-            } else {
160
-                $fix = $phpcsFile->addFixableWarning($error.'; use %s instead', $stackPtr, $type.'WithAlternative', $data);
161
-            }
162
-
163
-            if ($fix === true) {
164
-                $phpcsFile->fixer->replaceToken($stackPtr, $this->forbiddenStyles[$pattern]);
165
-            }
166
-        } else {
167
-            if ($this->error === true) {
168
-                $phpcsFile->addError($error, $stackPtr, $type, $data);
169
-            } else {
170
-                $phpcsFile->addWarning($error, $stackPtr, $type, $data);
171
-            }
172
-        }
173
-
174
-    }//end addError()
18
+	/**
19
+	 * A list of tokenizers this sniff supports.
20
+	 *
21
+	 * @var array
22
+	 */
23
+	public $supportedTokenizers = ['CSS'];
24
+
25
+	/**
26
+	 * A list of forbidden styles with their alternatives.
27
+	 *
28
+	 * The value is NULL if no alternative exists. i.e., the
29
+	 * style should just not be used.
30
+	 *
31
+	 * @var array<string, string|null>
32
+	 */
33
+	protected $forbiddenStyles = [
34
+		'-moz-border-radius'             => 'border-radius',
35
+		'-webkit-border-radius'          => 'border-radius',
36
+		'-moz-border-radius-topleft'     => 'border-top-left-radius',
37
+		'-moz-border-radius-topright'    => 'border-top-right-radius',
38
+		'-moz-border-radius-bottomright' => 'border-bottom-right-radius',
39
+		'-moz-border-radius-bottomleft'  => 'border-bottom-left-radius',
40
+		'-moz-box-shadow'                => 'box-shadow',
41
+		'-webkit-box-shadow'             => 'box-shadow',
42
+	];
43
+
44
+	/**
45
+	 * A cache of forbidden style names, for faster lookups.
46
+	 *
47
+	 * @var string[]
48
+	 */
49
+	protected $forbiddenStyleNames = [];
50
+
51
+	/**
52
+	 * If true, forbidden styles will be considered regular expressions.
53
+	 *
54
+	 * @var boolean
55
+	 */
56
+	protected $patternMatch = false;
57
+
58
+	/**
59
+	 * If true, an error will be thrown; otherwise a warning.
60
+	 *
61
+	 * @var boolean
62
+	 */
63
+	public $error = true;
64
+
65
+
66
+	/**
67
+	 * Returns an array of tokens this test wants to listen for.
68
+	 *
69
+	 * @return array
70
+	 */
71
+	public function register()
72
+	{
73
+		$this->forbiddenStyleNames = array_keys($this->forbiddenStyles);
74
+
75
+		if ($this->patternMatch === true) {
76
+			foreach ($this->forbiddenStyleNames as $i => $name) {
77
+				$this->forbiddenStyleNames[$i] = '/'.$name.'/i';
78
+			}
79
+		}
80
+
81
+		return [T_STYLE];
82
+
83
+	}//end register()
84
+
85
+
86
+	/**
87
+	 * Processes this test, when one of its tokens is encountered.
88
+	 *
89
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
90
+	 * @param int                         $stackPtr  The position of the current token in
91
+	 *                                               the stack passed in $tokens.
92
+	 *
93
+	 * @return void
94
+	 */
95
+	public function process(File $phpcsFile, $stackPtr)
96
+	{
97
+		$tokens  = $phpcsFile->getTokens();
98
+		$style   = strtolower($tokens[$stackPtr]['content']);
99
+		$pattern = null;
100
+
101
+		if ($this->patternMatch === true) {
102
+			$count   = 0;
103
+			$pattern = preg_replace(
104
+				$this->forbiddenStyleNames,
105
+				$this->forbiddenStyleNames,
106
+				$style,
107
+				1,
108
+				$count
109
+			);
110
+
111
+			if ($count === 0) {
112
+				return;
113
+			}
114
+
115
+			// Remove the pattern delimiters and modifier.
116
+			$pattern = substr($pattern, 1, -2);
117
+		} else {
118
+			if (in_array($style, $this->forbiddenStyleNames, true) === false) {
119
+				return;
120
+			}
121
+		}//end if
122
+
123
+		$this->addError($phpcsFile, $stackPtr, $style, $pattern);
124
+
125
+	}//end process()
126
+
127
+
128
+	/**
129
+	 * Generates the error or warning for this sniff.
130
+	 *
131
+	 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
132
+	 * @param int                         $stackPtr  The position of the forbidden style
133
+	 *                                               in the token array.
134
+	 * @param string                      $style     The name of the forbidden style.
135
+	 * @param string                      $pattern   The pattern used for the match.
136
+	 *
137
+	 * @return void
138
+	 */
139
+	protected function addError($phpcsFile, $stackPtr, $style, $pattern=null)
140
+	{
141
+		$data  = [$style];
142
+		$error = 'The use of style %s is ';
143
+		if ($this->error === true) {
144
+			$type   = 'Found';
145
+			$error .= 'forbidden';
146
+		} else {
147
+			$type   = 'Discouraged';
148
+			$error .= 'discouraged';
149
+		}
150
+
151
+		if ($pattern === null) {
152
+			$pattern = $style;
153
+		}
154
+
155
+		if ($this->forbiddenStyles[$pattern] !== null) {
156
+			$data[] = $this->forbiddenStyles[$pattern];
157
+			if ($this->error === true) {
158
+				$fix = $phpcsFile->addFixableError($error.'; use %s instead', $stackPtr, $type.'WithAlternative', $data);
159
+			} else {
160
+				$fix = $phpcsFile->addFixableWarning($error.'; use %s instead', $stackPtr, $type.'WithAlternative', $data);
161
+			}
162
+
163
+			if ($fix === true) {
164
+				$phpcsFile->fixer->replaceToken($stackPtr, $this->forbiddenStyles[$pattern]);
165
+			}
166
+		} else {
167
+			if ($this->error === true) {
168
+				$phpcsFile->addError($error, $stackPtr, $type, $data);
169
+			} else {
170
+				$phpcsFile->addWarning($error, $stackPtr, $type, $data);
171
+			}
172
+		}
173
+
174
+	}//end addError()
175 175
 
176 176
 
177 177
 }//end class
Please login to merge, or discard this patch.