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:
1 | <?php |
||
32 | class ExtractCommand extends Command |
||
33 | { |
||
34 | use BundleTrait; |
||
35 | |||
36 | protected static $defaultName = 'translation:extract'; |
||
37 | |||
38 | /** |
||
39 | * @var CatalogueFetcher |
||
40 | */ |
||
41 | private $catalogueFetcher; |
||
42 | |||
43 | /** |
||
44 | * @var CatalogueWriter |
||
45 | */ |
||
46 | private $catalogueWriter; |
||
47 | |||
48 | /** |
||
49 | * @var CatalogueCounter |
||
50 | */ |
||
51 | private $catalogueCounter; |
||
52 | |||
53 | /** |
||
54 | * @var Importer |
||
55 | */ |
||
56 | private $importer; |
||
57 | |||
58 | /** |
||
59 | * @var ConfigurationManager |
||
60 | */ |
||
61 | private $configurationManager; |
||
62 | |||
63 | public function __construct( |
||
78 | |||
79 | View Code Duplication | protected function configure(): void |
|
90 | |||
91 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
92 | { |
||
93 | $configName = $input->getArgument('configuration'); |
||
94 | $config = $this->configurationManager->getConfiguration($configName); |
||
95 | |||
96 | $locales = []; |
||
97 | if ($inputLocale = $input->getArgument('locale')) { |
||
98 | $locales = [$inputLocale]; |
||
99 | } |
||
100 | |||
101 | $catalogues = $this->catalogueFetcher->getCatalogues($config, $locales); |
||
102 | $this->configureBundleDirs($input, $config); |
||
103 | $finder = $this->getConfiguredFinder($config); |
||
104 | |||
105 | $result = $this->importer->extractToCatalogues($finder, $catalogues, [ |
||
106 | 'blacklist_domains' => $config->getBlacklistDomains(), |
||
107 | 'whitelist_domains' => $config->getWhitelistDomains(), |
||
108 | 'project_root' => $config->getProjectRoot(), |
||
109 | ]); |
||
110 | $errors = $result->getErrors(); |
||
111 | |||
112 | $this->catalogueWriter->writeCatalogues($config, $result->getMessageCatalogues()); |
||
113 | |||
114 | $definedBefore = $this->catalogueCounter->getNumberOfDefinedMessages($catalogues[0]); |
||
115 | $definedAfter = $this->catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]); |
||
116 | |||
117 | /* |
||
118 | * Print results |
||
119 | */ |
||
120 | $io = new SymfonyStyle($input, $output); |
||
121 | $io->table(['Type', 'Count'], [ |
||
122 | ['Total defined messages', $definedAfter], |
||
123 | ['New messages', $definedAfter - $definedBefore], |
||
124 | ['Errors', \count($errors)], |
||
125 | ]); |
||
126 | |||
127 | if (!$input->getOption('hide-errors')) { |
||
128 | /** @var Error $error */ |
||
129 | foreach ($errors as $error) { |
||
130 | $io->error( |
||
131 | \sprintf("%s\nLine: %s\nMessage: %s", $error->getPath(), $error->getLine(), $error->getMessage()) |
||
132 | ); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return 0; |
||
137 | } |
||
138 | |||
139 | private function getConfiguredFinder(Configuration $config): Finder |
||
154 | } |
||
155 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.