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( |
||
| 100 | BundleInterface $bundle, |
||
| 101 | $name, |
||
| 102 | $fields, |
||
| 103 | $namePrefix, |
||
| 104 | $dbPrefix, |
||
| 105 | $extendClass = null, |
||
| 106 | $withRepository = false |
||
| 107 | ) { |
||
| 108 | // configure the bundle (needed if the bundle does not contain any Entities yet) |
||
| 109 | $config = $this->registry->getManager(null)->getConfiguration(); |
||
| 110 | $config->setEntityNamespaces( |
||
| 111 | array_merge( |
||
| 112 | array($bundle->getName() => $bundle->getNamespace() . '\\Entity' . ($namePrefix ? '\\' . $namePrefix : '')), |
||
| 113 | $config->getEntityNamespaces() |
||
| 114 | ) |
||
| 115 | ); |
||
| 116 | |||
| 117 | $entityClass = $this->registry->getAliasNamespace($bundle->getName()) . ($namePrefix ? '\\' . $namePrefix : '') . '\\' . $name; |
||
| 118 | $entityPath = $bundle->getPath() . '/Entity/' . ($namePrefix ? $namePrefix . '/' : '') . str_replace('\\', '/', $name) . '.php'; |
||
| 119 | if (file_exists($entityPath)) { |
||
| 120 | throw new \RuntimeException(sprintf('Entity "%s" already exists.', $entityClass)); |
||
| 121 | } |
||
| 122 | |||
| 123 | $class = new ClassMetadataInfo($entityClass, new UnderscoreNamingStrategy()); |
||
| 124 | if ($withRepository) { |
||
| 125 | $entityClass = preg_replace('/\\\\Entity\\\\/', '\\Repository\\', $entityClass, 1); |
||
| 126 | $class->customRepositoryClassName = $entityClass.'Repository'; |
||
| 127 | $path = $this->isSymfony4() ? $bundle->getPath() : $bundle->getPath().str_repeat('/..', substr_count(get_class($bundle), '\\')); |
||
| 128 | $this->writeEntityRepositoryClass($class->customRepositoryClassName, $path); |
||
| 129 | } |
||
| 130 | |||
| 131 | foreach ($fields as $fieldSet) { |
||
| 132 | foreach ($fieldSet as $fieldArray) { |
||
| 133 | foreach ($fieldArray as $field) { |
||
| 134 | if (array_key_exists('joinColumn', $field)) { |
||
| 135 | $class->mapManyToOne($field); |
||
| 136 | } elseif (array_key_exists('joinTable', $field)) { |
||
| 137 | $class->mapManyToMany($field); |
||
| 138 | } else { |
||
| 139 | $class->mapField($field); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | $class->setPrimaryTable( |
||
| 145 | array( |
||
| 146 | 'name' => strtolower($dbPrefix . strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $name))) . 's', |
||
| 147 | ) |
||
| 148 | ); |
||
| 149 | $entityCode = $this->getEntityGenerator($extendClass)->generateEntityClass($class); |
||
| 150 | |||
| 151 | return array($entityCode, $entityPath); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get a Doctrine EntityGenerator instance. |
||
| 156 | * |
||
| 157 | * @param string|null $classToExtend |
||
| 158 | * |
||
| 159 | * @return EntityGenerator |
||
| 160 | */ |
||
| 161 | protected function getEntityGenerator($classToExtend = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Generate the entity admin type. |
||
| 179 | * |
||
| 180 | * @param $bundle |
||
| 181 | * @param $entityName |
||
| 182 | * @param $entityPrefix |
||
| 183 | * @param array $fields |
||
| 184 | * @param string $extendClass |
||
| 185 | */ |
||
| 186 | protected function generateEntityAdminType( |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Install the default page templates. |
||
| 215 | * |
||
| 216 | * @param BundleInterface $bundle |
||
| 217 | */ |
||
| 218 | protected function installDefaultPageTemplates($bundle) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Install the default pagepart configuration. |
||
| 266 | * |
||
| 267 | * @param BundleInterface $bundle |
||
| 268 | */ |
||
| 269 | protected function installDefaultPagePartConfiguration($bundle) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Render all files in the source directory and copy them to the target directory. |
||
| 286 | * |
||
| 287 | * @param string $sourceDir The source directory where we need to look in |
||
| 288 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 289 | * @param array $parameters The parameters that will be passed to the templates |
||
| 290 | * @param bool $override Whether to override an existing file or not |
||
| 291 | * @param bool $recursive Whether to render all files recursively or not |
||
| 292 | */ |
||
| 293 | public function renderFiles($sourceDir, $targetDir, array $parameters, $override = false, $recursive = true) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Render all files in the source directory and copy them to the target directory. |
||
| 325 | * |
||
| 326 | * @param string $sourceDir The source directory where we need to look in |
||
| 327 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 328 | * @param string $filename The name of the file that needs to be rendered |
||
| 329 | * @param array $parameters The parameters that will be passed to the templates |
||
| 330 | * @param bool $override Whether to override an existing file or not |
||
| 331 | * @param string|null $targetFilename The name of the target file (if null, then use $filename) |
||
| 332 | */ |
||
| 333 | public function renderSingleFile($sourceDir, $targetDir, $filename, array $parameters, $override = false, $targetFilename = null) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Render a file and make it executable. |
||
| 359 | * |
||
| 360 | * @param string $sourceDir The source directory where we need to look in |
||
| 361 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 362 | * @param string $filename The name of the file that needs to be rendered |
||
| 363 | * @param array $parameters The parameters that will be passed to the templates |
||
| 364 | * @param bool $override Whether to override an existing file or not |
||
| 365 | * @param int $mode The mode |
||
| 366 | */ |
||
| 367 | public function renderExecutableFile($sourceDir, $targetDir, $filename, array $parameters, $override = false, $mode = 0774) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Copy all files in the source directory to the target directory. |
||
| 378 | * |
||
| 379 | * @param string $sourceDir The source directory where we need to look in |
||
| 380 | * @param string $targetDir The target directory where we need to copy the files too |
||
| 381 | * @param bool $override Whether to override an existing file or not |
||
| 382 | */ |
||
| 383 | public function copyFiles($sourceDir, $targetDir, $override = false) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Remove a directory from the filesystem. |
||
| 394 | * |
||
| 395 | * @param string $targetDir |
||
| 396 | */ |
||
| 397 | public function removeDirectory($targetDir) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Remove a file from the filesystem. |
||
| 407 | * |
||
| 408 | * @param string $file |
||
| 409 | */ |
||
| 410 | public function removeFile($file) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Render a twig file with custom twig tags. |
||
| 417 | * |
||
| 418 | * @param string $template |
||
| 419 | * @param array $parameters |
||
| 420 | * @param string $sourceDir |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function renderTwig($template, array $parameters, $sourceDir) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Render a twig file, and save it to disk. |
||
| 451 | * |
||
| 452 | * @param string $template |
||
| 453 | * @param string $target |
||
| 454 | * @param array $parameters |
||
| 455 | * @param string $sourceDir |
||
| 456 | * |
||
| 457 | * @return int |
||
| 458 | */ |
||
| 459 | public function renderTwigFile($template, $target, array $parameters, $sourceDir) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator |
||
| 470 | */ |
||
| 471 | protected function getRepositoryGenerator() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @internal |
||
| 478 | */ |
||
| 479 | View Code Duplication | protected function getTemplateDir(BundleInterface $bundle) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @internal |
||
| 490 | */ |
||
| 491 | View Code Duplication | protected function getAssetsDir(BundleInterface $bundle) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @internal |
||
| 502 | */ |
||
| 503 | protected function isSymfony4() |
||
| 507 | |||
| 508 | private function writeEntityRepositoryClass($fullClassName, $outputDirectory) |
||
| 531 | } |
||
| 532 |
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.