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 RegenerateUrlAliasesCommand 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 RegenerateUrlAliasesCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class RegenerateUrlAliasesCommand extends ContainerAwareCommand |
||
29 | { |
||
30 | const MIGRATION_TABLE = '__migration_ezurlalias_ml'; |
||
31 | const CUSTOM_ALIAS_BACKUP_TABLE = '__migration_backup_custom_alias'; |
||
32 | |||
33 | /** |
||
34 | * @var \eZ\Publish\API\Repository\ContentService |
||
35 | */ |
||
36 | protected $contentService; |
||
37 | |||
38 | /** |
||
39 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
40 | */ |
||
41 | protected $nameSchemaResolver; |
||
42 | |||
43 | /** |
||
44 | * @var \eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler |
||
45 | */ |
||
46 | protected $urlAliasHandler; |
||
47 | |||
48 | /** |
||
49 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway |
||
50 | */ |
||
51 | protected $urlAliasGateway; |
||
52 | |||
53 | /** |
||
54 | * @var \Doctrine\DBAL\Connection $connection |
||
55 | */ |
||
56 | protected $connection; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $bulkCount; |
||
62 | |||
63 | /** |
||
64 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
65 | */ |
||
66 | protected $output; |
||
67 | |||
68 | protected $actions = [ |
||
69 | 'full', |
||
70 | 'autogenerate', |
||
71 | 'backup-custom', |
||
72 | 'restore-custom', |
||
73 | ]; |
||
74 | |||
75 | protected function configure() |
||
103 | |||
104 | protected function execute(InputInterface $input, OutputInterface $output) |
||
131 | |||
132 | /** |
||
133 | * Checks that configured storage engine is Legacy Storage Engine. |
||
134 | */ |
||
135 | protected function checkStorage() |
||
145 | |||
146 | /** |
||
147 | * Prepares dependencies used by the command. |
||
148 | * |
||
149 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
150 | */ |
||
151 | protected function prepareDependencies(OutputInterface $output) |
||
174 | |||
175 | /** |
||
176 | * Sets storage gateway to the default table. |
||
177 | * |
||
178 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway::TABLE |
||
179 | */ |
||
180 | protected function setDefaultTable() |
||
184 | |||
185 | /** |
||
186 | * Sets storage gateway to the migration table. |
||
187 | * |
||
188 | * @see \eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\RegenerateUrlAliasesCommand::MIGRATION_TABLE |
||
189 | */ |
||
190 | protected function setMigrationTable() |
||
194 | |||
195 | /** |
||
196 | * Backups custom URL aliases the custom URL alias backup table. |
||
197 | */ |
||
198 | protected function backupCustomLocationAliases() |
||
213 | |||
214 | /** |
||
215 | * Internal method for backing up custom URL aliases. |
||
216 | * |
||
217 | * @see \eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\RegenerateUrlAliasesCommand::backupCustomLocationAliases() |
||
218 | */ |
||
219 | protected function doBackupCustomLocationAliases() |
||
265 | |||
266 | /** |
||
267 | * Restores custom URL aliases from the backup table. |
||
268 | */ |
||
269 | protected function restoreCustomLocationAliases() |
||
280 | |||
281 | /** |
||
282 | * Restores custom URL aliases from the backup table. |
||
283 | * |
||
284 | * @see \eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\RegenerateUrlAliasesCommand::restoreCustomLocationAliases() |
||
285 | */ |
||
286 | protected function doRestoreCustomLocationAliases() |
||
339 | |||
340 | /** |
||
341 | * Loads Location data for the given $pass. |
||
342 | * |
||
343 | * @param \Doctrine\DBAL\Query\QueryBuilder $queryBuilder |
||
344 | * @param int $pass |
||
345 | * |
||
346 | * @return array |
||
347 | */ |
||
348 | View Code Duplication | protected function loadCustomUrlAliasData(QueryBuilder $queryBuilder, $pass) |
|
359 | |||
360 | /** |
||
361 | * Stores given custom $aliases to the custom alias backup table. |
||
362 | * |
||
363 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $aliases |
||
364 | * |
||
365 | * @return int |
||
366 | */ |
||
367 | protected function storeCustomAliases(array $aliases) |
||
388 | |||
389 | /** |
||
390 | * Stores custom URL alias data for $path to the backup table. |
||
391 | * |
||
392 | * @param int $locationId |
||
393 | * @param string $path |
||
394 | * @param string $languageCode |
||
395 | * @param boolean $alwaysAvailable |
||
396 | * @param boolean $forwarding |
||
397 | */ |
||
398 | protected function storeCustomAliasPath($locationId, $path, $languageCode, $alwaysAvailable, $forwarding) |
||
422 | |||
423 | /** |
||
424 | * Combines path data to an array of URL alias paths. |
||
425 | * |
||
426 | * Explanation: |
||
427 | * |
||
428 | * Custom URL aliases can generate NOP entries, which can be taken over by the autogenerated |
||
429 | * aliases. When multiple languages exists for the Location that took over, multiple entries |
||
430 | * with the same link will exist on the same level. In that case it will not be possible to |
||
431 | * reliably reconstruct what was the path for the original custom alias. For that reason we |
||
432 | * combine path data to get all possible path combinations. |
||
433 | * |
||
434 | * Note: it could happen that original NOP entry was historized after being taken over by the |
||
435 | * autogenerated alias. So to be complete this would have to take into account history entries |
||
436 | * as well, but at the moment we lack API to do that. |
||
437 | * |
||
438 | * Proper solution of this problem would be introducing separate database table to store custom |
||
439 | * URL alias data. |
||
440 | * |
||
441 | * @see https://jira.ez.no/browse/EZP-20777 |
||
442 | * |
||
443 | * @param array $pathData |
||
444 | * |
||
445 | * @return string[] |
||
446 | */ |
||
447 | protected function combinePaths(array $pathData) |
||
467 | |||
468 | /** |
||
469 | * Returns all path element strings found for the given path $levelData. |
||
470 | * |
||
471 | * @param array $levelData |
||
472 | * |
||
473 | * @return string[] |
||
474 | */ |
||
475 | protected function extractPathElements(array $levelData) |
||
489 | |||
490 | /** |
||
491 | * Generates URL aliases from the Location and Content data to the migration table. |
||
492 | */ |
||
493 | protected function generateLocationAliases() |
||
507 | |||
508 | /** |
||
509 | * Internal method for generating URL aliases. |
||
510 | * |
||
511 | * @see \eZ\Bundle\EzPublishMigrationBundle\Command\LegacyStorage\RegenerateUrlAliasesCommand::generateLocationAliases() |
||
512 | */ |
||
513 | protected function doGenerateLocationAliases() |
||
556 | |||
557 | /** |
||
558 | * Publishes URL aliases in all languages for the given parameters. |
||
559 | * |
||
560 | * @throws \Exception |
||
561 | * |
||
562 | * @param int|string $locationId |
||
563 | * @param int|string $parentLocationId |
||
564 | * @param int|string $contentId |
||
565 | * |
||
566 | * @return int |
||
567 | */ |
||
568 | protected function publishAliases($locationId, $parentLocationId, $contentId) |
||
593 | |||
594 | /** |
||
595 | * Loads Location data for the given $pass. |
||
596 | * |
||
597 | * @param \Doctrine\DBAL\Query\QueryBuilder $queryBuilder |
||
598 | * @param int $pass |
||
599 | * |
||
600 | * @return array |
||
601 | */ |
||
602 | View Code Duplication | protected function loadLocationData(QueryBuilder $queryBuilder, $pass) |
|
613 | |||
614 | /** |
||
615 | * Returns total number of Locations in the database. |
||
616 | * |
||
617 | * The number excludes absolute root Location, which does not have an URL alias. |
||
618 | */ |
||
619 | View Code Duplication | protected function getTotalLocationCount() |
|
636 | |||
637 | /** |
||
638 | * Returns total number of Content objects having a Location in the database. |
||
639 | * |
||
640 | * The number excludes absolute root Location, which does not have an URL alias. |
||
641 | */ |
||
642 | View Code Duplication | protected function getTotalLocationContentCount() |
|
659 | |||
660 | /** |
||
661 | * Returns total number of Content objects having a Location in the database. |
||
662 | * |
||
663 | * The number excludes absolute root Location, which does not have an URL alias. |
||
664 | */ |
||
665 | View Code Duplication | protected function getTotalCustomUrlAliasBackupCount() |
|
676 | |||
677 | /** |
||
678 | * Creates database table for custom URL alias backup. |
||
679 | */ |
||
680 | protected function createCustomAliasBackupTable() |
||
700 | |||
701 | /** |
||
702 | * Checks if database table $name exists. |
||
703 | * |
||
704 | * @param string $name |
||
705 | * |
||
706 | * @return bool |
||
707 | */ |
||
708 | protected function tableExists($name) |
||
712 | |||
713 | /** |
||
714 | * Checks if database table $name is empty. |
||
715 | * |
||
716 | * @param string $name |
||
717 | * |
||
718 | * @return bool |
||
719 | */ |
||
720 | View Code Duplication | protected function isTableEmpty($name) |
|
731 | |||
732 | /** |
||
733 | * Returns configured progress bar helper. |
||
734 | * |
||
735 | * @param int $maxSteps |
||
736 | * |
||
737 | * @return \Symfony\Component\Console\Helper\ProgressBar |
||
738 | */ |
||
739 | protected function getProgressBar($maxSteps) |
||
748 | } |
||
749 |