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 UndefinedVariable 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 UndefinedVariable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class UndefinedVariable extends AbstractLocalVariable implements FunctionAware, MethodAware |
||
41 | { |
||
42 | /** |
||
43 | * @var array Self reference class names. |
||
44 | */ |
||
45 | protected $selfReferences = array('self', 'static'); |
||
46 | |||
47 | /** |
||
48 | * Found variable images within a single method or function. |
||
49 | * |
||
50 | * @var array(string) |
||
51 | */ |
||
52 | private $images = array(); |
||
53 | |||
54 | /** |
||
55 | * This method checks that all local variables within the given function or |
||
56 | * method are used at least one time. |
||
57 | * |
||
58 | * @param \PHPMD\AbstractNode $node |
||
59 | * @return void |
||
60 | */ |
||
61 | public function apply(AbstractNode $node) |
||
82 | |||
83 | /** |
||
84 | * Collect variables defined inside a PHPMD entry node (such as MethodNode). |
||
85 | * |
||
86 | * @param AbstractNode $node |
||
87 | */ |
||
88 | private function collect(AbstractNode $node) |
||
99 | |||
100 | /** |
||
101 | * Stores the given literal node in an global of found variables. |
||
102 | * |
||
103 | * @param \PHPMD\Node\AbstractNode $node |
||
104 | * @return void |
||
105 | */ |
||
106 | private function collectGlobalStatements(AbstractNode $node) |
||
116 | |||
117 | /** |
||
118 | * Stores the given literal node in an catch of found variables. |
||
119 | * |
||
120 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
121 | * @return void |
||
122 | */ |
||
123 | View Code Duplication | private function collectExceptionCatches(AbstractCallableNode $node) |
|
135 | |||
136 | /** |
||
137 | * Stores the given literal node in an internal list of found variables. |
||
138 | * |
||
139 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
140 | * @return void |
||
141 | */ |
||
142 | View Code Duplication | private function collectListExpressions(AbstractCallableNode $node) |
|
152 | |||
153 | /** |
||
154 | * Stores the given literal node in an internal foreach of found variables. |
||
155 | * |
||
156 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
157 | * @return void |
||
158 | */ |
||
159 | private function collectForeachStatements(AbstractCallableNode $node) |
||
177 | |||
178 | /** |
||
179 | * Stores the given literal node in an internal closure of found variables. |
||
180 | * |
||
181 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
182 | * @return void |
||
183 | */ |
||
184 | private function collectClosureParameters(AbstractCallableNode $node) |
||
192 | |||
193 | /** |
||
194 | * Check if the given variable was defined in the current context before usage. |
||
195 | * |
||
196 | * @param \PHPMD\Node\ASTNode $variable |
||
197 | * @param \PHPMD\Node\AbstractCallableNode $parentNode |
||
198 | * @return bool |
||
199 | */ |
||
200 | private function checkVariableDefined(ASTNode $variable, AbstractCallableNode $parentNode) |
||
206 | |||
207 | /** |
||
208 | * Collect parameter names of method/function. |
||
209 | * |
||
210 | * @param \PHPMD\Node\AbstractNode $node |
||
211 | * @return void |
||
212 | */ |
||
213 | private function collectParameters(AbstractNode $node) |
||
225 | |||
226 | /** |
||
227 | * Collect assignments of variables. |
||
228 | * |
||
229 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
230 | * @return void |
||
231 | */ |
||
232 | private function collectAssignments(AbstractCallableNode $node) |
||
248 | |||
249 | /** |
||
250 | * Collect postfix property. |
||
251 | * |
||
252 | * @param \PHPMD\Node\AbstractNode $node |
||
253 | * @return void |
||
254 | */ |
||
255 | View Code Duplication | private function collectPropertyPostfix(AbstractNode $node) |
|
267 | |||
268 | /** |
||
269 | * Add the variable to images. |
||
270 | * |
||
271 | * @param ASTVariable|ASTPropertyPostfix|ASTVariableDeclarator $variable |
||
272 | * @return void |
||
273 | */ |
||
274 | private function addVariableDefinition($variable) |
||
282 | |||
283 | /** |
||
284 | * Return the PDepend node of ASTNode PHPMD node. |
||
285 | * |
||
286 | * Or return the input as is if it's not an ASTNode PHPMD node. |
||
287 | * |
||
288 | * @param mixed $node |
||
289 | * |
||
290 | * @return \PDepend\Source\AST\ASTArtifact|\PDepend\Source\AST\ASTNode |
||
291 | */ |
||
292 | private function getNode($node) |
||
300 | |||
301 | /** |
||
302 | * Get the image of the given variable node. |
||
303 | * |
||
304 | * Prefix self:: and static:: properties with "::". |
||
305 | * |
||
306 | * @param ASTVariable|ASTPropertyPostfix|ASTVariableDeclarator $variable |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | private function getVariableImage($variable) |
||
338 | |||
339 | /** |
||
340 | * Checks if a short name is acceptable in the current context. |
||
341 | * |
||
342 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
343 | * @param \PHPMD\Node\ASTNode $variable |
||
344 | * |
||
345 | * @return boolean |
||
346 | */ |
||
347 | private function isNameAllowedInContext(AbstractCallableNode $node, ASTNode $variable) |
||
355 | } |
||
356 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: