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 KunstmaanGenerator 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 KunstmaanGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class KunstmaanGenerator extends Generator |
||
23 | { |
||
24 | /** |
||
25 | * @var Filesystem |
||
26 | */ |
||
27 | protected $filesystem; |
||
28 | |||
29 | /** |
||
30 | * @var RegistryInterface |
||
31 | */ |
||
32 | protected $registry; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $skeletonDir; |
||
38 | |||
39 | /** |
||
40 | * @var CommandAssistant |
||
41 | */ |
||
42 | protected $assistant; |
||
43 | |||
44 | /** |
||
45 | * @var ContainerInterface |
||
46 | */ |
||
47 | protected $container; |
||
48 | |||
49 | /** |
||
50 | * @param Filesystem $filesystem The filesystem |
||
51 | * @param RegistryInterface $registry The registry |
||
52 | * @param string $skeletonDir The directory of the skeleton |
||
53 | * @param CommandAssistant $assistant The command assistant |
||
54 | * @param ContainerInterface $container The container |
||
|
|||
55 | */ |
||
56 | public function __construct( |
||
71 | |||
72 | /** |
||
73 | * Check that the keyword is a reserved word for the database system. |
||
74 | * |
||
75 | * @param string $keyword |
||
76 | * |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function isReservedKeyword($keyword) |
||
83 | |||
84 | /** |
||
85 | * Generate the entity PHP code. |
||
86 | * |
||
87 | * @param BundleInterface $bundle |
||
88 | * @param string $name |
||
89 | * @param array $fields |
||
90 | * @param string $namePrefix |
||
91 | * @param string $dbPrefix |
||
92 | * @param string|null $extendClass |
||
93 | * @param bool $withRepository |
||
94 | * |
||
95 | * @return array |
||
96 | * |
||
97 | * @throws \RuntimeException |
||
98 | */ |
||
99 | protected function generateEntity( |
||
159 | |||
160 | /** |
||
161 | * Get a Doctrine EntityGenerator instance. |
||
162 | * |
||
163 | * @param string|null $classToExtend |
||
164 | * |
||
165 | * @return EntityGenerator |
||
166 | */ |
||
167 | protected function getEntityGenerator($classToExtend = null) |
||
182 | |||
183 | /** |
||
184 | * Generate the entity admin type. |
||
185 | * |
||
186 | * @param $bundle |
||
187 | * @param $entityName |
||
188 | * @param $entityPrefix |
||
189 | * @param array $fields |
||
190 | * @param string $extendClass |
||
191 | */ |
||
192 | protected function generateEntityAdminType( |
||
218 | |||
219 | /** |
||
220 | * Install the default page templates. |
||
221 | * |
||
222 | * @param BundleInterface $bundle |
||
223 | */ |
||
224 | protected function installDefaultPageTemplates($bundle) |
||
281 | |||
282 | /** |
||
283 | * Install the default pagepart configuration. |
||
284 | * |
||
285 | * @param BundleInterface $bundle |
||
286 | */ |
||
287 | protected function installDefaultPagePartConfiguration($bundle) |
||
306 | |||
307 | /** |
||
308 | * Render all files in the source directory and copy them to the target directory. |
||
309 | * |
||
310 | * @param string $sourceDir The source directory where we need to look in |
||
311 | * @param string $targetDir The target directory where we need to copy the files too |
||
312 | * @param array $parameters The parameters that will be passed to the templates |
||
313 | * @param bool $override Whether to override an existing file or not |
||
314 | * @param bool $recursive Whether to render all files recursively or not |
||
315 | */ |
||
316 | public function renderFiles($sourceDir, $targetDir, array $parameters, $override = false, $recursive = true) |
||
345 | |||
346 | /** |
||
347 | * Render all files in the source directory and copy them to the target directory. |
||
348 | * |
||
349 | * @param string $sourceDir The source directory where we need to look in |
||
350 | * @param string $targetDir The target directory where we need to copy the files too |
||
351 | * @param string $filename The name of the file that needs to be rendered |
||
352 | * @param array $parameters The parameters that will be passed to the templates |
||
353 | * @param bool $override Whether to override an existing file or not |
||
354 | * @param string|null $targetFilename The name of the target file (if null, then use $filename) |
||
355 | */ |
||
356 | public function renderSingleFile($sourceDir, $targetDir, $filename, array $parameters, $override = false, $targetFilename = null) |
||
379 | |||
380 | /** |
||
381 | * Render a file and make it executable. |
||
382 | * |
||
383 | * @param string $sourceDir The source directory where we need to look in |
||
384 | * @param string $targetDir The target directory where we need to copy the files too |
||
385 | * @param string $filename The name of the file that needs to be rendered |
||
386 | * @param array $parameters The parameters that will be passed to the templates |
||
387 | * @param bool $override Whether to override an existing file or not |
||
388 | * @param int $mode The mode |
||
389 | */ |
||
390 | public function renderExecutableFile($sourceDir, $targetDir, $filename, array $parameters, $override = false, $mode = 0774) |
||
398 | |||
399 | /** |
||
400 | * Copy all files in the source directory to the target directory. |
||
401 | * |
||
402 | * @param string $sourceDir The source directory where we need to look in |
||
403 | * @param string $targetDir The target directory where we need to copy the files too |
||
404 | * @param bool $override Whether to override an existing file or not |
||
405 | */ |
||
406 | public function copyFiles($sourceDir, $targetDir, $override = false) |
||
414 | |||
415 | /** |
||
416 | * Remove a directory from the filesystem. |
||
417 | * |
||
418 | * @param string $targetDir |
||
419 | */ |
||
420 | public function removeDirectory($targetDir) |
||
427 | |||
428 | /** |
||
429 | * Remove a file from the filesystem. |
||
430 | * |
||
431 | * @param string $file |
||
432 | */ |
||
433 | public function removeFile($file) |
||
437 | |||
438 | /** |
||
439 | * Render a twig file with custom twig tags. |
||
440 | * |
||
441 | * @param string $template |
||
442 | * @param array $parameters |
||
443 | * @param string $sourceDir |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | public function renderTwig($template, array $parameters, $sourceDir) |
||
471 | |||
472 | /** |
||
473 | * Render a twig file, and save it to disk. |
||
474 | * |
||
475 | * @param string $template |
||
476 | * @param string $target |
||
477 | * @param array $parameters |
||
478 | * @param string $sourceDir |
||
479 | * |
||
480 | * @return int |
||
481 | */ |
||
482 | public function renderTwigFile($template, $target, array $parameters, $sourceDir) |
||
490 | |||
491 | /** |
||
492 | * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator |
||
493 | */ |
||
494 | protected function getRepositoryGenerator() |
||
498 | |||
499 | /** |
||
500 | * @return \Kunstmaan\GeneratorBundle\Generator\Symfony4EntityRepositoryGenerator |
||
501 | */ |
||
502 | protected function getSymfony4RepositoryGenerator() |
||
506 | |||
507 | /** |
||
508 | * @internal |
||
509 | */ |
||
510 | View Code Duplication | protected function getTemplateDir(BundleInterface $bundle) |
|
518 | |||
519 | /** |
||
520 | * @internal |
||
521 | */ |
||
522 | View Code Duplication | protected function getAssetsDir(BundleInterface $bundle) |
|
530 | |||
531 | /** |
||
532 | * @internal |
||
533 | */ |
||
534 | protected function isSymfony4() |
||
538 | } |
||
539 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.