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 ClassGenerator 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 ClassGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class ClassGenerator |
||
18 | { |
||
19 | |||
20 | 8 | private function handleBody(Generator\ClassGenerator $class, PHPClass $type) |
|
39 | |||
40 | 8 | private function isNativeType(PHPClass $class) |
|
53 | |||
54 | 8 | private function getPhpType(PHPClass $class) |
|
64 | |||
65 | 2 | private function handleValueMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class, $all = true) |
|
66 | { |
||
67 | 2 | $type = $prop->getType(); |
|
68 | |||
69 | 2 | $docblock = new DocBlockGenerator('Construct'); |
|
70 | 2 | $paramTag = new ParamTag("value", "mixed"); |
|
71 | 2 | $paramTag->setTypes(($type ? $this->getPhpType($type) : "mixed")); |
|
72 | |||
73 | 2 | $docblock->setTag($paramTag); |
|
74 | |||
75 | 2 | $param = new ParameterGenerator("value"); |
|
76 | 2 | if ($type && !$this->isNativeType($type)) { |
|
77 | $param->setType($this->getPhpType($type)); |
||
78 | } |
||
79 | 2 | $method = new MethodGenerator("__construct", [ |
|
80 | $param |
||
81 | 2 | ]); |
|
82 | 2 | $method->setDocBlock($docblock); |
|
83 | 2 | $method->setBody("\$this->value(\$value);"); |
|
84 | |||
85 | 2 | $generator->addMethodFromGenerator($method); |
|
86 | |||
87 | 2 | $docblock = new DocBlockGenerator('Gets or sets the inner value'); |
|
88 | 2 | $paramTag = new ParamTag("value", "mixed"); |
|
89 | 2 | View Code Duplication | if ($type && $type instanceof PHPClassOf) { |
90 | $paramTag->setTypes($this->getPhpType($type->getArg() |
||
91 | ->getType()) . "[]"); |
||
92 | 2 | } elseif ($type) { |
|
93 | 2 | $paramTag->setTypes($this->getPhpType($prop->getType())); |
|
94 | 2 | } |
|
95 | 2 | $docblock->setTag($paramTag); |
|
96 | |||
97 | 2 | $returnTag = new ReturnTag("mixed"); |
|
98 | |||
99 | 2 | View Code Duplication | if ($type && $type instanceof PHPClassOf) { |
100 | $returnTag->setTypes($this->getPhpType($type->getArg() |
||
101 | ->getType()) . "[]"); |
||
102 | 2 | } elseif ($type) { |
|
103 | 2 | $returnTag->setTypes($this->getPhpType($type)); |
|
104 | 2 | } |
|
105 | 2 | $docblock->setTag($returnTag); |
|
106 | |||
107 | 2 | $param = new ParameterGenerator("value"); |
|
108 | 2 | $param->setDefaultValue(null); |
|
109 | |||
110 | 2 | if ($type && !$this->isNativeType($type)) { |
|
111 | $param->setType($this->getPhpType($type)); |
||
112 | } |
||
113 | 2 | $method = new MethodGenerator("value", []); |
|
114 | 2 | $method->setDocBlock($docblock); |
|
115 | |||
116 | 2 | $methodBody = "if (\$args = func_get_args()) {" . PHP_EOL; |
|
117 | 2 | $methodBody .= " \$this->" . $prop->getName() . " = \$args[0];" . PHP_EOL; |
|
118 | 2 | $methodBody .= "}" . PHP_EOL; |
|
119 | 2 | $methodBody .= "return \$this->" . $prop->getName() . ";" . PHP_EOL; |
|
120 | 2 | $method->setBody($methodBody); |
|
121 | |||
122 | 2 | $generator->addMethodFromGenerator($method); |
|
123 | |||
124 | 2 | $docblock = new DocBlockGenerator('Gets a string value'); |
|
125 | 2 | $docblock->setTag(new ReturnTag("string")); |
|
126 | 2 | $method = new MethodGenerator("__toString"); |
|
127 | 2 | $method->setDocBlock($docblock); |
|
128 | 2 | $method->setBody("return strval(\$this->" . $prop->getName() . ");"); |
|
129 | 2 | $generator->addMethodFromGenerator($method); |
|
130 | 2 | } |
|
131 | |||
132 | 7 | private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) |
|
133 | { |
||
134 | 7 | $methodBody = ''; |
|
135 | 7 | $docblock = new DocBlockGenerator(); |
|
136 | |||
137 | 7 | $docblock->setShortDescription("Sets a new " . $prop->getName()); |
|
138 | |||
139 | 7 | if ($prop->getDoc()) { |
|
140 | $docblock->setLongDescription($prop->getDoc()); |
||
141 | } |
||
142 | |||
143 | 7 | $patramTag = new ParamTag($prop->getName()); |
|
144 | 7 | $docblock->setTag($patramTag); |
|
145 | |||
146 | 7 | $return = new ReturnTag("self"); |
|
147 | 7 | $docblock->setTag($return); |
|
148 | |||
149 | 7 | $type = $prop->getType(); |
|
150 | |||
151 | 7 | $method = new MethodGenerator("set" . Inflector::classify($prop->getName())); |
|
152 | |||
153 | 7 | $parameter = new ParameterGenerator($prop->getName(), "mixed"); |
|
154 | |||
155 | 7 | if ($type && $type instanceof PHPClassOf) { |
|
156 | 4 | $patramTag->setTypes($this->getPhpType($type->getArg() |
|
157 | 4 | ->getType()) . "[]"); |
|
158 | 4 | $parameter->setType("array"); |
|
159 | |||
160 | 4 | if ($p = $this->isOneType($type->getArg() |
|
161 | 4 | ->getType()) |
|
162 | 4 | ) { |
|
163 | if (($t = $p->getType())) { |
||
164 | $patramTag->setTypes($this->getPhpType($t)); |
||
165 | } |
||
166 | } |
||
167 | 7 | } elseif ($type) { |
|
168 | 3 | if ($this->isNativeType($type)) { |
|
169 | 3 | $patramTag->setTypes($this->getPhpType($type)); |
|
170 | 3 | } elseif ($p = $this->isOneType($type)) { |
|
171 | if (($t = $p->getType()) && !$this->isNativeType($t)) { |
||
172 | $patramTag->setTypes($this->getPhpType($t)); |
||
173 | $parameter->setType($this->getPhpType($t)); |
||
174 | } elseif ($t && !$this->isNativeType($t)) { |
||
175 | $patramTag->setTypes($this->getPhpType($t)); |
||
176 | $parameter->setType($this->getPhpType($t)); |
||
177 | } elseif ($t) { |
||
178 | $patramTag->setTypes($this->getPhpType($t)); |
||
179 | } |
||
180 | } else { |
||
181 | $patramTag->setTypes($this->getPhpType($type)); |
||
182 | $parameter->setType($this->getPhpType($type)); |
||
183 | } |
||
184 | 3 | } |
|
185 | |||
186 | 7 | $methodBody .= "\$this->" . $prop->getName() . " = \$" . $prop->getName() . ";" . PHP_EOL; |
|
187 | 7 | $methodBody .= "return \$this;"; |
|
188 | 7 | $method->setBody($methodBody); |
|
189 | 7 | $method->setDocBlock($docblock); |
|
190 | 7 | $method->setParameter($parameter); |
|
191 | |||
192 | 7 | $generator->addMethodFromGenerator($method); |
|
193 | 7 | } |
|
194 | |||
195 | 7 | private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) |
|
196 | { |
||
197 | |||
198 | 7 | if ($prop->getType() instanceof PHPClassOf) { |
|
199 | 4 | $docblock = new DocBlockGenerator(); |
|
200 | 4 | $docblock->setShortDescription("isset " . $prop->getName()); |
|
201 | 4 | if ($prop->getDoc()) { |
|
202 | $docblock->setLongDescription($prop->getDoc()); |
||
203 | } |
||
204 | |||
205 | 4 | $patramTag = new ParamTag("index", "scalar"); |
|
206 | 4 | $docblock->setTag($patramTag); |
|
207 | |||
208 | 4 | $docblock->setTag(new ReturnTag("boolean")); |
|
209 | |||
210 | 4 | $paramIndex = new ParameterGenerator("index", "mixed"); |
|
211 | |||
212 | 4 | $method = new MethodGenerator("isset" . Inflector::classify($prop->getName()), [$paramIndex]); |
|
213 | 4 | $method->setDocBlock($docblock); |
|
214 | 4 | $method->setBody("return isset(\$this->" . $prop->getName() . "[\$index]);"); |
|
215 | 4 | $generator->addMethodFromGenerator($method); |
|
216 | |||
217 | 4 | $docblock = new DocBlockGenerator(); |
|
218 | 4 | $docblock->setShortDescription("unset " . $prop->getName()); |
|
219 | 4 | if ($prop->getDoc()) { |
|
220 | $docblock->setLongDescription($prop->getDoc()); |
||
221 | } |
||
222 | |||
223 | 4 | $patramTag = new ParamTag("index", "scalar"); |
|
224 | 4 | $docblock->setTag($patramTag); |
|
225 | 4 | $paramIndex = new ParameterGenerator("index", "mixed"); |
|
226 | |||
227 | 4 | $docblock->setTag(new ReturnTag("void")); |
|
228 | |||
229 | |||
230 | 4 | $method = new MethodGenerator("unset" . Inflector::classify($prop->getName()), [$paramIndex]); |
|
231 | 4 | $method->setDocBlock($docblock); |
|
232 | 4 | $method->setBody("unset(\$this->" . $prop->getName() . "[\$index]);"); |
|
233 | 4 | $generator->addMethodFromGenerator($method); |
|
234 | 4 | } |
|
235 | // //// |
||
236 | |||
237 | 7 | $docblock = new DocBlockGenerator(); |
|
238 | |||
239 | 7 | $docblock->setShortDescription("Gets as " . $prop->getName()); |
|
240 | |||
241 | 7 | if ($prop->getDoc()) { |
|
242 | $docblock->setLongDescription($prop->getDoc()); |
||
243 | } |
||
244 | |||
245 | 7 | $tag = new ReturnTag("mixed"); |
|
246 | 7 | $type = $prop->getType(); |
|
247 | 7 | if ($type && $type instanceof PHPClassOf) { |
|
248 | 4 | $tt = $type->getArg()->getType(); |
|
249 | 4 | $tag->setTypes($this->getPhpType($tt) . "[]"); |
|
250 | 4 | View Code Duplication | if ($p = $this->isOneType($tt)) { |
251 | if (($t = $p->getType())) { |
||
252 | $tag->setTypes($this->getPhpType($t) . "[]"); |
||
253 | } |
||
254 | } |
||
255 | 7 | } elseif ($type) { |
|
256 | |||
257 | 3 | if ($p = $this->isOneType($type)) { |
|
258 | if ($t = $p->getType()) { |
||
259 | $tag->setTypes($this->getPhpType($t)); |
||
260 | } |
||
261 | } else { |
||
262 | 3 | $tag->setTypes($this->getPhpType($type)); |
|
263 | } |
||
264 | 3 | } |
|
265 | |||
266 | 7 | $docblock->setTag($tag); |
|
267 | |||
268 | 7 | $method = new MethodGenerator("get" . Inflector::classify($prop->getName())); |
|
269 | 7 | $method->setDocBlock($docblock); |
|
270 | 7 | $method->setBody("return \$this->" . $prop->getName() . ";"); |
|
271 | |||
272 | 7 | $generator->addMethodFromGenerator($method); |
|
273 | 7 | } |
|
274 | |||
275 | 8 | private function isOneType(PHPClass $type, $onlyParent = false) |
|
290 | |||
291 | 4 | private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) |
|
292 | { |
||
293 | 4 | $type = $prop->getType(); |
|
294 | 4 | $propName = $type->getArg()->getName(); |
|
295 | |||
296 | 4 | $docblock = new DocBlockGenerator(); |
|
297 | 4 | $docblock->setShortDescription("Adds as $propName"); |
|
298 | |||
299 | 4 | if ($prop->getDoc()) { |
|
300 | $docblock->setLongDescription($prop->getDoc()); |
||
301 | } |
||
302 | |||
303 | 4 | $return = new ReturnTag(); |
|
304 | 4 | $return->setTypes("self"); |
|
305 | 4 | $docblock->setTag($return); |
|
306 | |||
307 | 4 | $patramTag = new ParamTag($propName, $this->getPhpType($type->getArg() |
|
308 | 4 | ->getType())); |
|
309 | 4 | $docblock->setTag($patramTag); |
|
310 | |||
311 | 4 | $method = new MethodGenerator("addTo" . Inflector::classify($prop->getName())); |
|
312 | |||
313 | 4 | $parameter = new ParameterGenerator($propName); |
|
314 | 4 | $tt = $type->getArg()->getType(); |
|
315 | |||
316 | 4 | if (!$this->isNativeType($tt)) { |
|
317 | |||
318 | 1 | if ($p = $this->isOneType($tt)) { |
|
319 | if (($t = $p->getType())) { |
||
320 | $patramTag->setTypes($this->getPhpType($t)); |
||
321 | |||
322 | if (!$this->isNativeType($t)) { |
||
323 | $parameter->setType($this->getPhpType($t)); |
||
324 | } |
||
325 | } |
||
326 | 1 | } elseif (!$this->isNativeType($tt)) { |
|
327 | 1 | $parameter->setType($this->getPhpType($tt)); |
|
328 | 1 | } |
|
329 | 1 | } |
|
330 | |||
331 | 4 | $methodBody = "\$this->" . $prop->getName() . "[] = \$" . $propName . ";" . PHP_EOL; |
|
332 | 4 | $methodBody .= "return \$this;"; |
|
333 | 4 | $method->setBody($methodBody); |
|
334 | 4 | $method->setDocBlock($docblock); |
|
335 | 4 | $method->setParameter($parameter); |
|
336 | |||
337 | 4 | $generator->addMethodFromGenerator($method); |
|
338 | 4 | } |
|
339 | |||
340 | 7 | private function handleMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class) |
|
349 | |||
350 | 8 | private function handleProperty(Generator\ClassGenerator $class, PHPProperty $prop) |
|
351 | { |
||
352 | 8 | $generatedProp = new PropertyGenerator($prop->getName()); |
|
353 | 8 | $generatedProp->setVisibility(PropertyGenerator::VISIBILITY_PRIVATE); |
|
354 | |||
355 | 8 | $class->addPropertyFromGenerator($generatedProp); |
|
356 | |||
357 | 8 | if ($prop->getType() && (!$prop->getType()->getNamespace() && $prop->getType()->getName() == "array")) { |
|
358 | // $generatedProp->setDefaultValue(array(), PropertyValueGenerator::TYPE_AUTO, PropertyValueGenerator::OUTPUT_SINGLE_LINE); |
||
359 | 4 | } |
|
360 | |||
361 | 8 | $docBlock = new DocBlockGenerator(); |
|
362 | 8 | $generatedProp->setDocBlock($docBlock); |
|
363 | |||
364 | 8 | if ($prop->getDoc()) { |
|
365 | $docBlock->setLongDescription($prop->getDoc()); |
||
366 | } |
||
367 | 8 | $tag = new PropertyTag($prop->getName(), 'mixed'); |
|
368 | |||
369 | 8 | $type = $prop->getType(); |
|
370 | |||
371 | 8 | if ($type && $type instanceof PHPClassOf) { |
|
372 | 4 | $tt = $type->getArg()->getType(); |
|
373 | 4 | $tag->setTypes($this->getPhpType($tt) . "[]"); |
|
374 | 4 | View Code Duplication | if ($p = $this->isOneType($tt)) { |
375 | if (($t = $p->getType())) { |
||
376 | $tag->setTypes($this->getPhpType($t) . "[]"); |
||
377 | } |
||
378 | } |
||
379 | 8 | } elseif ($type) { |
|
380 | |||
381 | 4 | if ($this->isNativeType($type)) { |
|
382 | 4 | $tag->setTypes($this->getPhpType($type)); |
|
383 | 4 | View Code Duplication | } elseif (($p = $this->isOneType($type)) && ($t = $p->getType())) { |
384 | $tag->setTypes($this->getPhpType($t)); |
||
385 | } else { |
||
386 | $tag->setTypes($this->getPhpType($prop->getType())); |
||
387 | } |
||
388 | 4 | } |
|
389 | 8 | $docBlock->setTag($tag); |
|
390 | 8 | } |
|
391 | |||
392 | 8 | public function generate(Generator\ClassGenerator $class, PHPClass $type) |
|
427 | } |
||
428 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.