Complex classes like GeneratorVisitor 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 GeneratorVisitor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class GeneratorVisitor implements GeneratorVisitorInterface { |
||
39 | |||
40 | protected $writer; |
||
41 | |||
42 | protected $scalarTypeHints; |
||
43 | protected $returnTypeHints; |
||
44 | |||
45 | protected $config; |
||
46 | |||
47 | protected static $noTypeHints = [ |
||
48 | 'string', |
||
49 | 'int', |
||
50 | 'integer', |
||
51 | 'bool', |
||
52 | 'boolean', |
||
53 | 'float', |
||
54 | 'double', |
||
55 | 'object', |
||
56 | 'mixed', |
||
57 | 'resource' |
||
58 | ]; |
||
59 | |||
60 | 22 | public function __construct(CodeGeneratorConfig $config = null) { |
|
65 | |||
66 | 18 | public function reset() { |
|
69 | |||
70 | 5 | private function ensureBlankLine() { |
|
75 | |||
76 | 13 | protected function visitNamespace(NamespaceInterface $model) { |
|
81 | |||
82 | 13 | protected function visitRequiredFiles(AbstractPhpStruct $struct) { |
|
90 | |||
91 | 13 | protected function visitUseStatements(AbstractPhpStruct $struct) { |
|
117 | |||
118 | 22 | protected function visitDocblock(Docblock $docblock) { |
|
123 | |||
124 | 3 | protected function writeDocblock(Docblock $docblock) { |
|
131 | |||
132 | 12 | protected function visitTraits(TraitsInterface $struct) { |
|
138 | |||
139 | 11 | public function startVisitingClass(PhpClass $class) { |
|
171 | |||
172 | 1 | public function startVisitingInterface(PhpInterface $interface) { |
|
190 | |||
191 | 1 | public function startVisitingTrait(PhpTrait $trait) { |
|
206 | |||
207 | 6 | public function startVisitingStructConstants() { |
|
209 | |||
210 | 6 | public function visitStructConstant(PhpConstant $constant) { |
|
211 | 6 | $this->visitDocblock($constant->getDocblock()); |
|
212 | 6 | $this->writer->write('const ' . $constant->getName() . ' = '); |
|
213 | |||
214 | 6 | if ($constant->isExpression()) { |
|
215 | $this->writer->write($constant->getExpression()); |
||
216 | } else { |
||
217 | 6 | $this->writer->write($this->getPhpExport($constant->getValue())); |
|
218 | } |
||
219 | |||
220 | 6 | $this->writer->writeln(';'); |
|
221 | 6 | } |
|
222 | |||
223 | 6 | public function endVisitingStructConstants() { |
|
226 | |||
227 | 6 | public function startVisitingProperties() { |
|
229 | |||
230 | 7 | public function visitProperty(PhpProperty $property) { |
|
231 | 7 | $this->visitDocblock($property->getDocblock()); |
|
232 | |||
233 | 7 | $this->writer->write($property->getVisibility() . ' ' . ($property->isStatic() ? 'static ' : '') . '$' . $property->getName()); |
|
234 | |||
235 | 7 | if ($property->hasValue()) { |
|
236 | 3 | if ($property->isExpression()) { |
|
237 | 1 | $this->writer->write(' = ' . $property->getExpression()); |
|
238 | 1 | } else { |
|
239 | 2 | $this->writer->write(' = ' . $this->getPhpExport($property->getValue())); |
|
240 | } |
||
241 | 3 | } |
|
242 | |||
243 | 7 | $this->writer->writeln(';'); |
|
244 | 7 | } |
|
245 | |||
246 | 7 | protected function getPhpExport($value) { |
|
254 | |||
255 | 6 | public function endVisitingProperties() { |
|
258 | |||
259 | 8 | public function startVisitingMethods() { |
|
261 | |||
262 | 10 | public function visitMethod(PhpMethod $method) { |
|
263 | 10 | $this->visitDocblock($method->getDocblock()); |
|
264 | |||
265 | 10 | if ($method->isAbstract()) { |
|
266 | $this->writer->write('abstract '); |
||
267 | } |
||
268 | |||
269 | 10 | $this->writer->write($method->getVisibility() . ' '); |
|
270 | |||
271 | 10 | if ($method->isStatic()) { |
|
272 | $this->writer->write('static '); |
||
273 | } |
||
274 | |||
275 | 10 | $this->writer->write('function '); |
|
276 | |||
277 | 10 | if ($method->isReferenceReturned()) { |
|
278 | 1 | $this->writer->write('& '); |
|
279 | 1 | } |
|
280 | |||
281 | 10 | $this->writer->write($method->getName() . '('); |
|
282 | |||
283 | 10 | $this->writeParameters($method->getParameters()); |
|
284 | 10 | $this->writer->write(')'); |
|
285 | 10 | $this->writeFunctionReturnType($method->getType()); |
|
286 | |||
287 | 10 | if ($method->isAbstract() || $method->getParent() instanceof PhpInterface) { |
|
288 | $this->writer->write(";\n\n"); |
||
289 | |||
290 | return; |
||
291 | } |
||
292 | |||
293 | 10 | $this->writer->writeln(' {')->indent()->writeln(trim($method->getBody()))->outdent()->rtrim()->write("}\n\n"); |
|
294 | 10 | } |
|
295 | |||
296 | 8 | public function endVisitingMethods() { |
|
298 | |||
299 | 13 | protected function endVisitingStruct(AbstractPhpStruct $struct) { |
|
302 | |||
303 | 11 | public function endVisitingClass(PhpClass $class) { |
|
306 | |||
307 | 1 | public function endVisitingInterface(PhpInterface $interface) { |
|
310 | |||
311 | 1 | public function endVisitingTrait(PhpTrait $trait) { |
|
314 | |||
315 | 6 | public function visitFunction(PhpFunction $function) { |
|
316 | 6 | if ($namespace = $function->getNamespace()) { |
|
317 | $this->writer->write("namespace $namespace;\n\n"); |
||
318 | } |
||
319 | |||
320 | 6 | $this->visitDocblock($function->getDocblock()); |
|
321 | |||
322 | 6 | $this->writer->write("function {$function->getName()}("); |
|
323 | 6 | $this->writeParameters($function->getParameters()); |
|
324 | 6 | $this->writer->write(')'); |
|
325 | 6 | $this->writeFunctionReturnType($function->getType()); |
|
326 | 6 | $this->writer->write(" {\n")->indent()->writeln(trim($function->getBody()))->outdent()->rtrim()->write('}'); |
|
327 | 6 | } |
|
328 | |||
329 | 22 | public function getContent() { |
|
332 | |||
333 | 16 | protected function writeParameters(array $parameters) { |
|
334 | 16 | $first = true; |
|
335 | 16 | foreach ($parameters as $parameter) { |
|
336 | 11 | if (!$first) { |
|
372 | |||
373 | /** |
||
374 | * @param string $type |
||
375 | */ |
||
376 | 16 | protected function writeFunctionReturnType($type) { |
|
381 | } |
||
382 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.