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 |
||
| 23 | class KunstmaanGenerator extends Generator |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var Filesystem |
||
| 27 | */ |
||
| 28 | protected $filesystem; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var RegistryInterface |
||
| 32 | */ |
||
| 33 | protected $registry; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $skeletonDir; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var CommandAssistant |
||
| 42 | */ |
||
| 43 | protected $assistant; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var ContainerInterface |
||
| 47 | */ |
||
| 48 | protected $container; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param Filesystem $filesystem The filesystem |
||
| 52 | * @param RegistryInterface $registry The registry |
||
| 53 | * @param string $skeletonDir The directory of the skeleton |
||
| 54 | * @param CommandAssistant $assistant The command assistant |
||
| 55 | * @param ContainerInterface $container The container |
||
|
|
|||
| 56 | */ |
||
| 57 | 1 | public function __construct( |
|
| 58 | Filesystem $filesystem, |
||
| 59 | RegistryInterface $registry, |
||
| 60 | $skeletonDir, |
||
| 61 | CommandAssistant $assistant, |
||
| 62 | ContainerInterface $container = null |
||
| 63 | ) { |
||
| 64 | 1 | $this->filesystem = $filesystem; |
|
| 65 | 1 | $this->registry = $registry; |
|
| 66 | 1 | $this->skeletonDir = GeneratorUtils::getFullSkeletonPath($skeletonDir); |
|
| 67 | 1 | $this->assistant = $assistant; |
|
| 68 | 1 | $this->container = $container; |
|
| 69 | |||
| 70 | 1 | $this->setSkeletonDirs(array($this->skeletonDir, GeneratorUtils::getFullSkeletonPath('/common'))); |
|
| 71 | 1 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Check that the keyword is a reserved word for the database system. |
||
| 75 | * |
||
| 76 | * @param string $keyword |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | public function isReservedKeyword($keyword) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Generate the entity PHP code. |
||
| 87 | * |
||
| 88 | * @param BundleInterface $bundle |
||
| 89 | * @param string $name |
||
| 90 | * @param array $fields |
||
| 91 | * @param string $namePrefix |
||
| 92 | * @param string $dbPrefix |
||
| 93 | * @param string|null $extendClass |
||
| 94 | * @param bool $withRepository |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | * |
||
| 98 | * @throws \RuntimeException |
||
| 99 | */ |
||
| 100 | protected function generateEntity( |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get a Doctrine EntityGenerator instance. |
||
| 163 | * |
||
| 164 | * @param string|null $classToExtend |
||
| 165 | * |
||
| 166 | * @return EntityGenerator |
||
| 167 | */ |
||
| 168 | protected function getEntityGenerator($classToExtend = null) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Generate the entity admin type. |
||
| 186 | * |
||
| 187 | * @param $bundle |
||
| 188 | * @param $entityName |
||
| 189 | * @param $entityPrefix |
||
| 190 | * @param array $fields |
||
| 191 | * @param string $extendClass |
||
| 192 | */ |
||
| 193 | protected function generateEntityAdminType( |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Install the default page templates. |
||
| 222 | * |
||
| 223 | * @param BundleInterface $bundle |
||
| 224 | */ |
||
| 225 | protected function installDefaultPageTemplates($bundle) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Install the default pagepart configuration. |
||
| 285 | * |
||
| 286 | * @param BundleInterface $bundle |
||
| 287 | */ |
||
| 288 | protected function installDefaultPagePartConfiguration($bundle) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Render all files in the source directory and copy them to the target directory. |
||
| 310 | * |
||
| 311 | * @param string $sourceDir The source directory where we need to look in |
||
| 312 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 313 | * @param array $parameters The parameters that will be passed to the templates |
||
| 314 | * @param bool $override Whether to override an existing file or not |
||
| 315 | * @param bool $recursive Whether to render all files recursively or not |
||
| 316 | */ |
||
| 317 | 1 | public function renderFiles($sourceDir, $targetDir, array $parameters, $override = false, $recursive = true) |
|
| 318 | { |
||
| 319 | // Make sure the source -and target dir contain a trailing slash |
||
| 320 | 1 | $sourceDir = rtrim($sourceDir, '/') . '/'; |
|
| 321 | 1 | $targetDir = rtrim($targetDir, '/') . '/'; |
|
| 322 | |||
| 323 | 1 | $this->setSkeletonDirs(array($sourceDir)); |
|
| 324 | |||
| 325 | 1 | $finder = new Finder(); |
|
| 326 | 1 | $finder->files()->in($sourceDir); |
|
| 327 | 1 | if (!$recursive) { |
|
| 328 | $finder->depth('== 0'); |
||
| 329 | } |
||
| 330 | |||
| 331 | // Get all files in the source directory |
||
| 332 | 1 | foreach ($finder as $file) { |
|
| 333 | 1 | $name = $file->getRelativePathname(); |
|
| 334 | |||
| 335 | // Check that we are allowed to overwrite the file if it already exists |
||
| 336 | 1 | View Code Duplication | if (!is_file($targetDir . $name) || $override === true) { |
| 337 | 1 | $fileParts = explode('.', $name); |
|
| 338 | 1 | if (end($fileParts) === 'twig') { |
|
| 339 | 1 | $this->renderTwigFile($name, $targetDir . $name, $parameters, $sourceDir); |
|
| 340 | } else { |
||
| 341 | $this->renderFile($name, $targetDir . $name, $parameters); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | } |
||
| 345 | 1 | } |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Render all files in the source directory and copy them to the target directory. |
||
| 349 | * |
||
| 350 | * @param string $sourceDir The source directory where we need to look in |
||
| 351 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 352 | * @param string $filename The name of the file that needs to be rendered |
||
| 353 | * @param array $parameters The parameters that will be passed to the templates |
||
| 354 | * @param bool $override Whether to override an existing file or not |
||
| 355 | * @param string|null $targetFilename The name of the target file (if null, then use $filename) |
||
| 356 | */ |
||
| 357 | 1 | public function renderSingleFile($sourceDir, $targetDir, $filename, array $parameters, $override = false, $targetFilename = null) |
|
| 358 | { |
||
| 359 | // Make sure the source -and target dir contain a trailing slash |
||
| 360 | 1 | $sourceDir = rtrim($sourceDir, '/') . '/'; |
|
| 361 | 1 | $targetDir = rtrim($targetDir, '/') . '/'; |
|
| 362 | 1 | if (is_null($targetFilename)) { |
|
| 363 | 1 | $targetFilename = $filename; |
|
| 364 | } |
||
| 365 | |||
| 366 | 1 | $this->setSkeletonDirs(array($sourceDir)); |
|
| 367 | |||
| 368 | 1 | View Code Duplication | if (is_file($sourceDir . $filename)) { |
| 369 | // Check that we are allowed the overwrite the file if it already exists |
||
| 370 | 1 | if (!is_file($targetDir . $targetFilename) || $override === true) { |
|
| 371 | 1 | $fileParts = explode('.', $filename); |
|
| 372 | 1 | if (end($fileParts) === 'twig') { |
|
| 373 | 1 | $this->renderTwigFile($filename, $targetDir . $targetFilename, $parameters, $sourceDir); |
|
| 374 | } else { |
||
| 375 | 1 | $this->renderFile($filename, $targetDir . $targetFilename, $parameters); |
|
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | 1 | } |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Render a file and make it executable. |
||
| 383 | * |
||
| 384 | * @param string $sourceDir The source directory where we need to look in |
||
| 385 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 386 | * @param string $filename The name of the file that needs to be rendered |
||
| 387 | * @param array $parameters The parameters that will be passed to the templates |
||
| 388 | * @param bool $override Whether to override an existing file or not |
||
| 389 | * @param int $mode The mode |
||
| 390 | */ |
||
| 391 | public function renderExecutableFile($sourceDir, $targetDir, $filename, array $parameters, $override = false, $mode = 0774) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Copy all files in the source directory to the target directory. |
||
| 402 | * |
||
| 403 | * @param string $sourceDir The source directory where we need to look in |
||
| 404 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 405 | * @param bool $override Whether to override an existing file or not |
||
| 406 | */ |
||
| 407 | public function copyFiles($sourceDir, $targetDir, $override = false) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Remove a directory from the filesystem. |
||
| 418 | * |
||
| 419 | * @param string $targetDir |
||
| 420 | */ |
||
| 421 | public function removeDirectory($targetDir) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Remove a file from the filesystem. |
||
| 431 | * |
||
| 432 | * @param string $file |
||
| 433 | */ |
||
| 434 | public function removeFile($file) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Render a twig file with custom twig tags. |
||
| 441 | * |
||
| 442 | * @param string $template |
||
| 443 | * @param array $parameters |
||
| 444 | * @param string $sourceDir |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | 1 | public function renderTwig($template, array $parameters, $sourceDir) |
|
| 449 | { |
||
| 450 | 1 | $twig = new \Twig_Environment( |
|
| 451 | 1 | new \Twig_Loader_Filesystem(array($sourceDir)), array( |
|
| 452 | 1 | 'debug' => true, |
|
| 453 | 'cache' => false, |
||
| 454 | 'strict_variables' => true, |
||
| 455 | 'autoescape' => false, |
||
| 456 | ) |
||
| 457 | ); |
||
| 458 | |||
| 459 | // Ruby erb template syntax |
||
| 460 | 1 | $lexer = new \Twig_Lexer( |
|
| 461 | 1 | $twig, array( |
|
| 462 | 1 | 'tag_comment' => array('<%#', '%>'), |
|
| 463 | 'tag_block' => array('<%', '%>'), |
||
| 464 | 'tag_variable' => array('<%=', '%>'), |
||
| 465 | ) |
||
| 466 | ); |
||
| 467 | |||
| 468 | 1 | $twig->setLexer($lexer); |
|
| 469 | |||
| 470 | 1 | return $twig->render($template, $parameters); |
|
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Render a twig file, and save it to disk. |
||
| 475 | * |
||
| 476 | * @param string $template |
||
| 477 | * @param string $target |
||
| 478 | * @param array $parameters |
||
| 479 | * @param string $sourceDir |
||
| 480 | * |
||
| 481 | * @return int |
||
| 482 | */ |
||
| 483 | 1 | public function renderTwigFile($template, $target, array $parameters, $sourceDir) |
|
| 484 | { |
||
| 485 | 1 | if (!is_dir(dirname($target))) { |
|
| 486 | 1 | mkdir(dirname($target), 0777, true); |
|
| 487 | } |
||
| 488 | |||
| 489 | 1 | return file_put_contents($target, $this->renderTwig($template, $parameters, $sourceDir)); |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator |
||
| 494 | */ |
||
| 495 | protected function getRepositoryGenerator() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @return \Kunstmaan\GeneratorBundle\Generator\Symfony4EntityRepositoryGenerator |
||
| 502 | */ |
||
| 503 | protected function getSymfony4RepositoryGenerator() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @internal |
||
| 510 | */ |
||
| 511 | 1 | View Code Duplication | protected function getTemplateDir(BundleInterface $bundle) |
| 512 | { |
||
| 513 | 1 | if ($this->isSymfony4()) { |
|
| 514 | 1 | return $this->container->getParameter('kernel.project_dir') . '/templates'; |
|
| 515 | } |
||
| 516 | |||
| 517 | return $bundle->getPath() . '/Resources/views'; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @internal |
||
| 522 | */ |
||
| 523 | View Code Duplication | protected function getAssetsDir(BundleInterface $bundle) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * @internal |
||
| 534 | */ |
||
| 535 | 1 | protected function isSymfony4() |
|
| 536 | { |
||
| 537 | 1 | return Kernel::VERSION_ID >= 40000; |
|
| 539 | } |
||
| 540 |
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.