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 |
||
| 26 | class KunstmaanGenerator extends Generator |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var Filesystem |
||
| 30 | */ |
||
| 31 | protected $filesystem; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var RegistryInterface |
||
| 35 | */ |
||
| 36 | protected $registry; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $skeletonDir; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var CommandAssistant |
||
| 45 | */ |
||
| 46 | protected $assistant; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var ContainerInterface |
||
| 50 | */ |
||
| 51 | protected $container; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param Filesystem $filesystem The filesystem |
||
| 55 | * @param RegistryInterface $registry The registry |
||
| 56 | * @param string $skeletonDir The directory of the skeleton |
||
| 57 | * @param CommandAssistant $assistant The command assistant |
||
| 58 | * @param ContainerInterface $container The container |
||
|
|
|||
| 59 | */ |
||
| 60 | 1 | public function __construct( |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Check that the keyword is a reserved word for the database system. |
||
| 78 | * |
||
| 79 | * @param string $keyword |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public function isReservedKeyword($keyword) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Generate the entity PHP code. |
||
| 90 | * |
||
| 91 | * @param BundleInterface $bundle |
||
| 92 | * @param string $name |
||
| 93 | * @param array $fields |
||
| 94 | * @param string $namePrefix |
||
| 95 | * @param string $dbPrefix |
||
| 96 | * @param string|null $extendClass |
||
| 97 | * @param bool $withRepository |
||
| 98 | * |
||
| 99 | * @return array |
||
| 100 | * |
||
| 101 | * @throws \RuntimeException |
||
| 102 | */ |
||
| 103 | protected function generateEntity( |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get a Doctrine EntityGenerator instance. |
||
| 166 | * |
||
| 167 | * @param string|null $classToExtend |
||
| 168 | * |
||
| 169 | * @return EntityGenerator |
||
| 170 | */ |
||
| 171 | protected function getEntityGenerator($classToExtend = null) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Generate the entity admin type. |
||
| 189 | * |
||
| 190 | * @param $bundle |
||
| 191 | * @param $entityName |
||
| 192 | * @param $entityPrefix |
||
| 193 | * @param array $fields |
||
| 194 | * @param string $extendClass |
||
| 195 | */ |
||
| 196 | protected function generateEntityAdminType( |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Install the default page templates. |
||
| 225 | * |
||
| 226 | * @param BundleInterface $bundle |
||
| 227 | */ |
||
| 228 | protected function installDefaultPageTemplates($bundle) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Install the default pagepart configuration. |
||
| 288 | * |
||
| 289 | * @param BundleInterface $bundle |
||
| 290 | */ |
||
| 291 | protected function installDefaultPagePartConfiguration($bundle) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Render all files in the source directory and copy them to the target directory. |
||
| 313 | * |
||
| 314 | * @param string $sourceDir The source directory where we need to look in |
||
| 315 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 316 | * @param array $parameters The parameters that will be passed to the templates |
||
| 317 | * @param bool $override Whether to override an existing file or not |
||
| 318 | * @param bool $recursive Whether to render all files recursively or not |
||
| 319 | */ |
||
| 320 | 1 | public function renderFiles($sourceDir, $targetDir, array $parameters, $override = false, $recursive = true) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Render all files in the source directory and copy them to the target directory. |
||
| 352 | * |
||
| 353 | * @param string $sourceDir The source directory where we need to look in |
||
| 354 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 355 | * @param string $filename The name of the file that needs to be rendered |
||
| 356 | * @param array $parameters The parameters that will be passed to the templates |
||
| 357 | * @param bool $override Whether to override an existing file or not |
||
| 358 | * @param string|null $targetFilename The name of the target file (if null, then use $filename) |
||
| 359 | */ |
||
| 360 | 1 | public function renderSingleFile($sourceDir, $targetDir, $filename, array $parameters, $override = false, $targetFilename = null) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Render a file and make it executable. |
||
| 386 | * |
||
| 387 | * @param string $sourceDir The source directory where we need to look in |
||
| 388 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 389 | * @param string $filename The name of the file that needs to be rendered |
||
| 390 | * @param array $parameters The parameters that will be passed to the templates |
||
| 391 | * @param bool $override Whether to override an existing file or not |
||
| 392 | * @param int $mode The mode |
||
| 393 | */ |
||
| 394 | public function renderExecutableFile($sourceDir, $targetDir, $filename, array $parameters, $override = false, $mode = 0774) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Copy all files in the source directory to the target directory. |
||
| 405 | * |
||
| 406 | * @param string $sourceDir The source directory where we need to look in |
||
| 407 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 408 | * @param bool $override Whether to override an existing file or not |
||
| 409 | */ |
||
| 410 | public function copyFiles($sourceDir, $targetDir, $override = false) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Remove a directory from the filesystem. |
||
| 421 | * |
||
| 422 | * @param string $targetDir |
||
| 423 | */ |
||
| 424 | public function removeDirectory($targetDir) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Remove a file from the filesystem. |
||
| 434 | * |
||
| 435 | * @param string $file |
||
| 436 | */ |
||
| 437 | public function removeFile($file) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Render a twig file with custom twig tags. |
||
| 444 | * |
||
| 445 | * @param string $template |
||
| 446 | * @param array $parameters |
||
| 447 | * @param string $sourceDir |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | 1 | public function renderTwig($template, array $parameters, $sourceDir) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Render a twig file, and save it to disk. |
||
| 478 | * |
||
| 479 | * @param string $template |
||
| 480 | * @param string $target |
||
| 481 | * @param array $parameters |
||
| 482 | * @param string $sourceDir |
||
| 483 | * |
||
| 484 | * @return int |
||
| 485 | */ |
||
| 486 | 1 | public function renderTwigFile($template, $target, array $parameters, $sourceDir) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator |
||
| 497 | */ |
||
| 498 | protected function getRepositoryGenerator() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @return \Kunstmaan\GeneratorBundle\Generator\Symfony4EntityRepositoryGenerator |
||
| 505 | */ |
||
| 506 | protected function getSymfony4RepositoryGenerator() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @internal |
||
| 513 | */ |
||
| 514 | 1 | View Code Duplication | protected function getTemplateDir(BundleInterface $bundle) |
| 522 | |||
| 523 | /** |
||
| 524 | * @internal |
||
| 525 | */ |
||
| 526 | View Code Duplication | protected function getAssetsDir(BundleInterface $bundle) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * @internal |
||
| 537 | */ |
||
| 538 | 1 | protected function isSymfony4() |
|
| 542 | } |
||
| 543 |
This check looks for
@paramannotations 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.