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 |
||
38 | class UndefinedVariable extends AbstractLocalVariable implements FunctionAware, MethodAware |
||
39 | { |
||
40 | /** |
||
41 | * @var array Self reference class names. |
||
42 | */ |
||
43 | protected $selfReferences = array('self', 'static'); |
||
44 | |||
45 | /** |
||
46 | * Found variable images within a single method or function. |
||
47 | * |
||
48 | * @var array(string) |
||
49 | */ |
||
50 | private $images = array(); |
||
51 | |||
52 | /** |
||
53 | * This method checks that all local variables within the given function or |
||
54 | * method are used at least one time. |
||
55 | * |
||
56 | * @param \PHPMD\AbstractNode $node |
||
57 | * @return void |
||
58 | */ |
||
59 | public function apply(AbstractNode $node) |
||
80 | |||
81 | /** |
||
82 | * Collect variables defined inside a PHPMD entry node (such as MethodNode). |
||
83 | * |
||
84 | * @param AbstractNode $node |
||
85 | */ |
||
86 | private function collect(AbstractNode $node) |
||
97 | |||
98 | /** |
||
99 | * Stores the given literal node in an global of found variables. |
||
100 | * |
||
101 | * @param \PHPMD\Node\AbstractNode $node |
||
102 | * @return void |
||
103 | */ |
||
104 | private function collectGlobalStatements(AbstractNode $node) |
||
114 | |||
115 | /** |
||
116 | * Stores the given literal node in an catch of found variables. |
||
117 | * |
||
118 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
119 | * @return void |
||
120 | */ |
||
121 | View Code Duplication | private function collectExceptionCatches(AbstractCallableNode $node) |
|
133 | |||
134 | /** |
||
135 | * Stores the given literal node in an internal list of found variables. |
||
136 | * |
||
137 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
138 | * @return void |
||
139 | */ |
||
140 | View Code Duplication | private function collectListExpressions(AbstractCallableNode $node) |
|
150 | |||
151 | /** |
||
152 | * Stores the given literal node in an internal foreach of found variables. |
||
153 | * |
||
154 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
155 | * @return void |
||
156 | */ |
||
157 | private function collectForeachStatements(AbstractCallableNode $node) |
||
175 | |||
176 | /** |
||
177 | * Stores the given literal node in an internal closure of found variables. |
||
178 | * |
||
179 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
180 | * @return void |
||
181 | */ |
||
182 | private function collectClosureParameters(AbstractCallableNode $node) |
||
190 | |||
191 | /** |
||
192 | * Check if the given variable was defined in the current context before usage. |
||
193 | * |
||
194 | * @param \PHPMD\Node\ASTNode $variable |
||
195 | * @param \PHPMD\Node\AbstractCallableNode $parentNode |
||
196 | * @return bool |
||
197 | */ |
||
198 | private function checkVariableDefined(ASTNode $variable, AbstractCallableNode $parentNode) |
||
204 | |||
205 | /** |
||
206 | * Collect parameter names of method/function. |
||
207 | * |
||
208 | * @param \PHPMD\Node\AbstractNode $node |
||
209 | * @return void |
||
210 | */ |
||
211 | private function collectParameters(AbstractNode $node) |
||
223 | |||
224 | /** |
||
225 | * Collect assignments of variables. |
||
226 | * |
||
227 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
228 | * @return void |
||
229 | */ |
||
230 | private function collectAssignments(AbstractCallableNode $node) |
||
246 | |||
247 | /** |
||
248 | * Collect postfix property. |
||
249 | * |
||
250 | * @param \PHPMD\Node\AbstractNode $node |
||
251 | * @return void |
||
252 | */ |
||
253 | View Code Duplication | private function collectPropertyPostfix(AbstractNode $node) |
|
265 | |||
266 | /** |
||
267 | * Add the variable to images. |
||
268 | * |
||
269 | * @param ASTVariable|ASTPropertyPostfix|ASTVariableDeclarator $variable |
||
270 | * @return void |
||
271 | */ |
||
272 | private function addVariableDefinition($variable) |
||
280 | |||
281 | /** |
||
282 | * Checks if a short name is acceptable in the current context. |
||
283 | * |
||
284 | * @param \PHPMD\Node\AbstractCallableNode $node |
||
285 | * @param \PHPMD\Node\ASTNode $variable |
||
286 | * |
||
287 | * @return boolean |
||
288 | */ |
||
289 | private function isNameAllowedInContext(AbstractCallableNode $node, ASTNode $variable) |
||
297 | } |
||
298 |
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: