Complex classes like InlineCommentSniff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InlineCommentSniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | * @author Greg Sherwood <[email protected]> |
||
30 | * @author Marc McIntyre <[email protected]> |
||
31 | * @author Alexander Obuhovich <[email protected]> |
||
32 | * @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
||
33 | * @link https://github.com/aik099/CodingStandard |
||
34 | */ |
||
35 | class InlineCommentSniff implements Sniff |
||
36 | { |
||
37 | |||
38 | /** |
||
39 | * A list of tokenizers this sniff supports. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | public $supportedTokenizers = array( |
||
44 | 'PHP', |
||
45 | 'JS', |
||
46 | ); |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Returns an array of tokens this test wants to listen for. |
||
51 | * |
||
52 | * @return integer[] |
||
53 | */ |
||
54 | public function register() |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Processes this test, when one of its tokens is encountered. |
||
65 | * |
||
66 | * @param File $phpcsFile The file being scanned. |
||
67 | * @param int $stackPtr The position of the current token in the |
||
68 | * stack passed in $tokens. |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | public function process(File $phpcsFile, $stackPtr) |
||
73 | { |
||
74 | $tokens = $phpcsFile->getTokens(); |
||
75 | |||
76 | // If this is a function/class/interface doc block comment, skip it. |
||
77 | // We are only interested in inline doc block comments, which are |
||
78 | // not allowed. |
||
79 | if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) { |
||
80 | $nextToken = $phpcsFile->findNext( |
||
81 | Tokens::$emptyTokens, |
||
82 | ($stackPtr + 1), |
||
83 | null, |
||
84 | true |
||
85 | ); |
||
86 | |||
87 | $ignore = array( |
||
88 | T_CLASS, |
||
89 | T_INTERFACE, |
||
90 | T_TRAIT, |
||
91 | T_FUNCTION, |
||
92 | T_CLOSURE, |
||
93 | T_PUBLIC, |
||
94 | T_PRIVATE, |
||
95 | T_PROTECTED, |
||
96 | T_FINAL, |
||
97 | T_STATIC, |
||
98 | T_ABSTRACT, |
||
99 | T_CONST, |
||
100 | T_PROPERTY, |
||
101 | ); |
||
102 | |||
103 | if (in_array($tokens[$nextToken]['code'], $ignore) === true) { |
||
104 | return; |
||
105 | } |
||
106 | |||
107 | if ($phpcsFile->tokenizerType === 'JS') { |
||
108 | // We allow block comments if a function or object |
||
109 | // is being assigned to a variable. |
||
110 | $ignore = Tokens::$emptyTokens; |
||
111 | $ignore[] = T_EQUAL; |
||
112 | $ignore[] = T_STRING; |
||
113 | $ignore[] = T_OBJECT_OPERATOR; |
||
114 | $nextToken = $phpcsFile->findNext($ignore, ($nextToken + 1), null, true); |
||
115 | if ($tokens[$nextToken]['code'] === T_FUNCTION |
||
116 | || $tokens[$nextToken]['code'] === T_CLOSURE |
||
117 | || $tokens[$nextToken]['code'] === T_OBJECT |
||
118 | || $tokens[$nextToken]['code'] === T_PROTOTYPE |
||
119 | ) { |
||
120 | return; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | $prevToken = $phpcsFile->findPrevious( |
||
125 | Tokens::$emptyTokens, |
||
126 | ($stackPtr - 1), |
||
127 | null, |
||
128 | true |
||
129 | ); |
||
130 | |||
131 | if ($tokens[$prevToken]['code'] === T_OPEN_TAG) { |
||
132 | return; |
||
133 | } |
||
134 | |||
135 | // Only error once per comment. |
||
136 | if ($tokens[$stackPtr]['content'] === '/**') { |
||
137 | $commentEnd = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr + 1)); |
||
138 | $commentText = $phpcsFile->getTokensAsString($stackPtr, (($commentEnd - $stackPtr) + 1)); |
||
139 | |||
140 | if (strpos($commentText, '@var') === false && strpos($commentText, '@type') === false) { |
||
141 | $error = 'Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead'; |
||
142 | $phpcsFile->addError($error, $stackPtr, 'DocBlock'); |
||
143 | } |
||
144 | } |
||
145 | }//end if |
||
146 | |||
147 | if ($tokens[$stackPtr]['content']{0} === '#') { |
||
148 | $error = 'Perl-style comments are not allowed; use "// Comment" instead'; |
||
149 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'WrongStyle'); |
||
150 | if ($fix === true) { |
||
151 | $comment = ltrim($tokens[$stackPtr]['content'], "# \t"); |
||
152 | $phpcsFile->fixer->replaceToken($stackPtr, "// $comment"); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | // We don't want: |
||
157 | // - end of block comments, if the last comment is a closing curly brace |
||
158 | // - closing comment of previous control structure (see "WhiteSpace.ControlStructureSpacing") |
||
159 | $previousContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
||
160 | if ($tokens[$previousContent]['line'] === $tokens[$stackPtr]['line'] |
||
161 | || $tokens[$previousContent]['line'] === ($tokens[$stackPtr]['line'] - 1) |
||
162 | ) { |
||
163 | if ($tokens[$previousContent]['code'] === T_CLOSE_CURLY_BRACKET) { |
||
164 | return; |
||
165 | } |
||
166 | |||
167 | // Special case for JS files. |
||
168 | if ($tokens[$previousContent]['code'] === T_COMMA |
||
169 | || $tokens[$previousContent]['code'] === T_SEMICOLON |
||
170 | ) { |
||
171 | $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($previousContent - 1), null, true); |
||
172 | if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) { |
||
173 | return; |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | $comment = rtrim($tokens[$stackPtr]['content']); |
||
179 | |||
180 | // Only want inline comments. |
||
181 | if (substr($comment, 0, 2) !== '//') { |
||
182 | return; |
||
183 | } |
||
184 | |||
185 | if (trim(substr($comment, 2)) !== '') { |
||
186 | $spaceCount = 0; |
||
187 | $tabFound = false; |
||
188 | |||
189 | $commentLength = strlen($comment); |
||
190 | for ($i = 2; $i < $commentLength; $i++) { |
||
191 | if ($comment[$i] === "\t") { |
||
192 | $tabFound = true; |
||
193 | break; |
||
194 | } |
||
195 | |||
196 | if ($comment[$i] !== ' ') { |
||
197 | break; |
||
198 | } |
||
199 | |||
200 | $spaceCount++; |
||
201 | } |
||
202 | |||
203 | $fix = false; |
||
204 | if ($tabFound === true) { |
||
205 | $error = 'Tab found before comment text; expected "// %s" but found "%s"'; |
||
206 | $data = array( |
||
207 | ltrim(substr($comment, 2)), |
||
208 | $comment, |
||
209 | ); |
||
210 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'TabBefore', $data); |
||
211 | } elseif ($spaceCount === 0) { |
||
212 | $error = 'No space found before comment text; expected "// %s" but found "%s"'; |
||
213 | $data = array( |
||
214 | substr($comment, 2), |
||
215 | $comment, |
||
216 | ); |
||
217 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSpaceBefore', $data); |
||
218 | } elseif ($spaceCount > 1) { |
||
219 | $error = 'Expected 1 space before comment text but found %s; use block comment if you need indentation'; |
||
220 | $data = array( |
||
221 | $spaceCount, |
||
222 | substr($comment, (2 + $spaceCount)), |
||
223 | $comment, |
||
224 | ); |
||
225 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBefore', $data); |
||
226 | }//end if |
||
227 | |||
228 | if ($fix === true) { |
||
229 | $newComment = '// '.ltrim($tokens[$stackPtr]['content'], "/\t "); |
||
230 | $phpcsFile->fixer->replaceToken($stackPtr, $newComment); |
||
231 | } |
||
232 | }//end if |
||
233 | |||
234 | // The below section determines if a comment block is correctly capitalised, |
||
235 | // and ends in a full-stop. It will find the last comment in a block, and |
||
236 | // work its way up. |
||
237 | $nextComment = $phpcsFile->findNext(array(T_COMMENT), ($stackPtr + 1), null, false); |
||
238 | |||
239 | if (($nextComment !== false) && (($tokens[$nextComment]['line']) === ($tokens[$stackPtr]['line'] + 1))) { |
||
240 | return; |
||
241 | } |
||
242 | |||
243 | $lastComment = $stackPtr; |
||
244 | while (($topComment = $phpcsFile->findPrevious(array(T_COMMENT), ($lastComment - 1), null, false)) !== false) { |
||
245 | if ($tokens[$topComment]['line'] !== ($tokens[$lastComment]['line'] - 1)) { |
||
246 | break; |
||
247 | } |
||
248 | |||
249 | $lastComment = $topComment; |
||
250 | } |
||
251 | |||
252 | $topComment = $lastComment; |
||
253 | $commentText = ''; |
||
254 | |||
255 | for ($i = $topComment; $i <= $stackPtr; $i++) { |
||
256 | if ($tokens[$i]['code'] === T_COMMENT) { |
||
257 | $commentText .= trim(substr($tokens[$i]['content'], 2)); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | if ($commentText === '') { |
||
262 | $error = 'Blank comments are not allowed'; |
||
263 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); |
||
264 | if ($fix === true) { |
||
265 | $phpcsFile->fixer->replaceToken($stackPtr, ''); |
||
266 | } |
||
267 | |||
268 | return; |
||
269 | } |
||
270 | |||
271 | // If the first character is now uppercase and is not |
||
272 | // a non-letter character, throw an error. |
||
273 | // \p{Lu} : an uppercase letter that has a lowercase variant. |
||
274 | // \P{L} : a non-letter character. |
||
275 | if (preg_match('/\p{Lu}|\P{L}/u', $commentText[0]) === 0) { |
||
276 | $error = 'Inline comments must start with a capital letter'; |
||
277 | $phpcsFile->addError($error, $topComment, 'NotCapital'); |
||
278 | } |
||
279 | |||
280 | // Only check the end of comment character if the start of the comment |
||
281 | // is a letter, indicating that the comment is just standard text. |
||
282 | if (preg_match('/\P{L}/u', $commentText[0]) === 0) { |
||
283 | $commentCloser = $commentText[(strlen($commentText) - 1)]; |
||
284 | $acceptedClosers = array( |
||
285 | 'full-stops' => '.', |
||
286 | 'exclamation marks' => '!', |
||
287 | 'or question marks' => '?', |
||
288 | ); |
||
289 | |||
290 | if (in_array($commentCloser, $acceptedClosers) === false) { |
||
291 | $error = 'Inline comments must end in %s'; |
||
292 | $ender = ''; |
||
293 | foreach ($acceptedClosers as $closerName => $symbol) { |
||
294 | $ender .= ' '.$closerName.','; |
||
295 | } |
||
296 | |||
297 | $ender = trim($ender, ' ,'); |
||
298 | $data = array($ender); |
||
299 | $phpcsFile->addError($error, $stackPtr, 'InvalidEndChar', $data); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | // Finally, the line below the last comment cannot be empty if this inline |
||
304 | // comment is on a line by itself. |
||
305 | if ($tokens[$previousContent]['line'] < $tokens[$stackPtr]['line']) { |
||
306 | for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) { |
||
307 | if ($tokens[$i]['line'] === ($tokens[$stackPtr]['line'] + 1)) { |
||
308 | if ($tokens[$i]['code'] !== T_WHITESPACE) { |
||
309 | return; |
||
310 | } |
||
311 | } elseif ($tokens[$i]['line'] > ($tokens[$stackPtr]['line'] + 1)) { |
||
312 | break; |
||
313 | } |
||
314 | } |
||
315 | |||
316 | $error = 'There must be no blank line following an inline comment'; |
||
317 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfter'); |
||
318 | if ($fix === true) { |
||
319 | $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
||
320 | $phpcsFile->fixer->beginChangeset(); |
||
321 | for ($i = ($stackPtr + 1); $i < $next; $i++) { |
||
322 | if ($tokens[$i]['line'] === $tokens[$next]['line']) { |
||
323 | break; |
||
324 | } |
||
325 | |||
326 | $phpcsFile->fixer->replaceToken($i, ''); |
||
327 | } |
||
328 | |||
329 | $phpcsFile->fixer->endChangeset(); |
||
330 | } |
||
331 | }//end if |
||
332 | }//end process() |
||
334 |