Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ForbiddenCallTimePassByReferenceSniff 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 ForbiddenCallTimePassByReferenceSniff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | * @author Gary Rogers <[email protected]> |
||
28 | * @author Florian Grandel <[email protected]> |
||
29 | * @copyright 2009 Florian Grandel |
||
30 | */ |
||
31 | class ForbiddenCallTimePassByReferenceSniff extends Sniff |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * Returns an array of tokens this test wants to listen for. |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | public function register() |
||
44 | |||
45 | /** |
||
46 | * Processes this test, when one of its tokens is encountered. |
||
47 | * |
||
48 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
49 | * @param int $stackPtr The position of the current token |
||
50 | * in the stack passed in $tokens. |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
124 | |||
125 | |||
126 | /** |
||
127 | * Determine whether a parameter is passed by reference. |
||
128 | * |
||
129 | * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
||
130 | * @param array $parameter Information on the current parameter |
||
131 | * to be examined. |
||
132 | * @param int $nestingLevel Target nesting level. |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | protected function isCallTimePassByReferenceParam(\PHP_CodeSniffer_File $phpcsFile, $parameter, $nestingLevel) |
||
137 | { |
||
138 | $tokens = $phpcsFile->getTokens(); |
||
139 | |||
140 | $searchStartToken = $parameter['start'] - 1; |
||
141 | $searchEndToken = $parameter['end'] + 1; |
||
142 | $nextVariable = $searchStartToken; |
||
143 | do { |
||
144 | $nextVariable = $phpcsFile->findNext(T_VARIABLE, ($nextVariable + 1), $searchEndToken); |
||
145 | if ($nextVariable === false) { |
||
146 | return false; |
||
147 | } |
||
148 | |||
149 | // Make sure the variable belongs directly to this function call |
||
150 | // and is not inside a nested function call or array. |
||
151 | if (isset($tokens[$nextVariable]['nested_parenthesis']) === false |
||
152 | || (count($tokens[$nextVariable]['nested_parenthesis']) !== $nestingLevel) |
||
153 | ) { |
||
154 | continue; |
||
155 | } |
||
156 | |||
157 | |||
158 | // Checking this: $value = my_function(...[*]$arg...). |
||
159 | $tokenBefore = $phpcsFile->findPrevious( |
||
160 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
161 | ($nextVariable - 1), |
||
162 | $searchStartToken, |
||
163 | true |
||
164 | ); |
||
165 | |||
166 | if ($tokenBefore === false || $tokens[$tokenBefore]['code'] !== T_BITWISE_AND) { |
||
167 | // Nothing before the token or no &. |
||
168 | continue; |
||
169 | } |
||
170 | |||
171 | // Checking this: $value = my_function(...[*]&$arg...). |
||
172 | $tokenBefore = $phpcsFile->findPrevious( |
||
173 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
174 | ($tokenBefore - 1), |
||
175 | $searchStartToken, |
||
176 | true |
||
177 | ); |
||
178 | |||
179 | // We have to exclude all uses of T_BITWISE_AND that are not |
||
180 | // references. We use a blacklist approach as we prefer false |
||
181 | // positives to not identifying a pass-by-reference call at all. |
||
182 | // The blacklist may not yet be complete. |
||
183 | switch ($tokens[$tokenBefore]['code']) { |
||
184 | // In these cases T_BITWISE_AND represents |
||
185 | // the bitwise and operator. |
||
186 | case T_LNUMBER: |
||
187 | case T_VARIABLE: |
||
188 | case T_CLOSE_SQUARE_BRACKET: |
||
189 | case T_CLOSE_PARENTHESIS: |
||
190 | break; |
||
191 | |||
192 | // Prevent false positive on assign by reference and compare with reference |
||
193 | // with function call parameters. |
||
194 | case T_EQUAL: |
||
195 | case T_AND_EQUAL: |
||
196 | case T_OR_EQUAL: |
||
197 | case T_CONCAT_EQUAL: |
||
198 | case T_DIV_EQUAL: |
||
199 | case T_MINUS_EQUAL: |
||
200 | case T_MOD_EQUAL: |
||
201 | case T_MUL_EQUAL: |
||
202 | case T_PLUS_EQUAL: |
||
203 | case T_XOR_EQUAL: |
||
204 | case T_SL_EQUAL: |
||
205 | case T_SR_EQUAL: |
||
206 | case T_IS_EQUAL: |
||
207 | case T_IS_IDENTICAL: |
||
208 | break; |
||
209 | |||
210 | // Unfortunately the tokenizer fails to recognize global constants, |
||
211 | // class-constants and -attributes. Any of these are returned is |
||
212 | // treated as T_STRING. |
||
213 | // So we step back another token and check if it is a class |
||
214 | // operator (-> or ::), which means we have a false positive. |
||
215 | // Global constants still remain uncovered. |
||
216 | case T_STRING: |
||
217 | $tokenBeforePlus = $phpcsFile->findPrevious( |
||
218 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
219 | ($tokenBefore - 1), |
||
220 | $searchStartToken, |
||
221 | true |
||
222 | ); |
||
223 | if ($tokens[$tokenBeforePlus]['code'] === T_DOUBLE_COLON |
||
224 | || $tokens[$tokenBeforePlus]['code'] === T_OBJECT_OPERATOR |
||
225 | ) { |
||
226 | break; |
||
227 | } |
||
228 | // If not a class constant: fall through. |
||
229 | |||
230 | default: |
||
231 | // Deal with T_POW_EQUAL which doesn't exist in PHPCS 1.x. |
||
232 | if (defined('T_POW_EQUAL') && $tokens[$tokenBefore]['type'] === 'T_POW_EQUAL') { |
||
233 | break; |
||
234 | } |
||
235 | |||
236 | // The found T_BITWISE_AND represents a pass-by-reference. |
||
237 | return true; |
||
238 | } |
||
239 | } while ($nextVariable < $searchEndToken); |
||
240 | |||
241 | // This code should never be reached, but here in case of weird bugs ;-) |
||
242 | return false; |
||
246 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.