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:
1 | <?php |
||
28 | class UnusedLocalVariable extends AbstractLocalVariable implements FunctionAware, MethodAware |
||
29 | { |
||
30 | /** |
||
31 | * Found variable images within a single method or function. |
||
32 | * |
||
33 | * @var array(string) |
||
34 | */ |
||
35 | private $images = array(); |
||
36 | |||
37 | /** |
||
38 | * This method checks that all local variables within the given function or |
||
39 | * method are used at least one time. |
||
40 | * |
||
41 | * @param \PHPMD\AbstractNode $node |
||
42 | * @return void |
||
43 | */ |
||
44 | 44 | public function apply(AbstractNode $node) |
|
58 | |||
59 | /** |
||
60 | * This method removes all variables from the <b>$_images</b> property that |
||
61 | * are also found in the formal parameters of the given method or/and |
||
62 | * function node. |
||
63 | * |
||
64 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
65 | * @return void |
||
66 | */ |
||
67 | 44 | private function removeParameters(AbstractCallableNode $node) |
|
79 | |||
80 | /** |
||
81 | * This method collects all local variable instances from the given |
||
82 | * method/function node and stores their image in the <b>$_images</b> |
||
83 | * property. |
||
84 | * |
||
85 | * |
||
86 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
87 | * @return void |
||
88 | */ |
||
89 | 44 | private function collectVariables(AbstractCallableNode $node) |
|
114 | |||
115 | /** |
||
116 | * Stores the given compound variable node in an internal list of found variables. |
||
117 | * |
||
118 | * @param \PHPMD\Node\ASTNode $node |
||
119 | * @return void |
||
120 | */ |
||
121 | 2 | private function collectCompoundVariableInString(ASTNode $node) |
|
137 | |||
138 | /** |
||
139 | * Stores the given variable node in an internal list of found variables. |
||
140 | * |
||
141 | * @param \PHPMD\Node\ASTNode $node |
||
142 | * @return void |
||
143 | */ |
||
144 | 28 | private function collectVariable(ASTNode $node) |
|
149 | |||
150 | /** |
||
151 | * Safely add node to $this->images. |
||
152 | * |
||
153 | * @param string $imageName the name to store the node as |
||
154 | * @param \PHPMD\Node\ASTNode $node the node being stored |
||
155 | * @return void |
||
156 | */ |
||
157 | 28 | private function storeImage($imageName, ASTNode $node) |
|
164 | |||
165 | /** |
||
166 | * Stores the given literal node in an internal list of found variables. |
||
167 | * |
||
168 | * @param \PHPMD\Node\ASTNode $node |
||
169 | * @return void |
||
170 | */ |
||
171 | 4 | private function collectLiteral(ASTNode $node) |
|
179 | |||
180 | /** |
||
181 | * Template method that performs the real node image check. |
||
182 | * |
||
183 | * @param ASTNode $node |
||
184 | * @return void |
||
185 | */ |
||
186 | 15 | protected function doCheckNodeImage(ASTNode $node) |
|
200 | |||
201 | /** |
||
202 | * Checks if a short name is acceptable in the current context. For the |
||
203 | * moment these contexts are the init section of a for-loop and short |
||
204 | * variable names in catch-statements. |
||
205 | * |
||
206 | * @param \PHPMD\AbstractNode $node |
||
207 | * @return boolean |
||
208 | */ |
||
209 | 15 | private function isNameAllowedInContext(AbstractNode $node) |
|
213 | |||
214 | /** |
||
215 | * Checks if an unused foreach variable (key or variable) is allowed. |
||
216 | * |
||
217 | * If it's not a foreach variable, it returns always false. |
||
218 | * |
||
219 | * @param \PHPMD\Node\ASTNode $variable The variable to check. |
||
220 | * @return bool True if allowed, else false. |
||
221 | */ |
||
222 | 14 | private function isUnusedForeachVariableAllowed(ASTNode $variable) |
|
231 | |||
232 | /** |
||
233 | * Checks if the given node is a direct or indirect child of a node with |
||
234 | * the given type. |
||
235 | * |
||
236 | * @param \PHPMD\AbstractNode $node |
||
237 | * @param string $type |
||
238 | * @return boolean |
||
239 | */ |
||
240 | 15 | private function isChildOf(AbstractNode $node, $type) |
|
246 | |||
247 | /** |
||
248 | * Gets array of exceptions from property |
||
249 | * |
||
250 | * @return array |
||
251 | */ |
||
252 | 12 | View Code Duplication | private function getExceptionsList() |
262 | } |
||
263 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.