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 |
||
17 | class TranslationConverterCommand extends ContainerAwareCommand |
||
18 | { |
||
19 | /** |
||
20 | * @var Finder |
||
21 | */ |
||
22 | protected $finder; |
||
23 | |||
24 | /** |
||
25 | * @var Filesystem |
||
26 | */ |
||
27 | protected $filesystem; |
||
28 | |||
29 | /** |
||
30 | * @param Finder $finder |
||
31 | * @param Filesystem $filesystem |
||
32 | * @param null|string $name |
||
33 | */ |
||
34 | 3 | public function __construct(Finder $finder, Filesystem $filesystem, $name = null) |
|
41 | |||
42 | /** |
||
43 | * {@inheritDoc} |
||
44 | */ |
||
45 | 3 | protected function configure() |
|
59 | |||
60 | /** |
||
61 | * {@inheritDoc} |
||
62 | */ |
||
63 | 3 | protected function execute(InputInterface $input, OutputInterface $output) |
|
64 | { |
||
65 | 3 | $path = $input->getOption('path'); |
|
66 | 3 | $inputFormat = $input->getOption('input'); |
|
67 | 3 | $outputFormat = $input->getOption('output') ?: 'xliff'; |
|
68 | |||
69 | 3 | $catalogs = []; |
|
70 | |||
71 | 3 | if (!$inputFormat) { |
|
72 | throw new \InvalidArgumentException('You must specify a --input format option.'); |
||
73 | } |
||
74 | |||
75 | 3 | if (!$path || !$this->filesystem->exists($path)) { |
|
76 | throw new \InvalidArgumentException('You must specify a valid --path option.'); |
||
77 | } |
||
78 | |||
79 | 3 | $dumper = $this->getDumper($outputFormat); |
|
80 | 3 | $this->getTranslationWriter()->addDumper($outputFormat, $dumper); |
|
81 | |||
82 | 3 | $files = $this->finder->files()->name('/[a-z]+\.[a-z]{2}\.'.$inputFormat.'/')->in($path); |
|
83 | |||
84 | 3 | foreach ($files as $file) { |
|
85 | 3 | list($domain, $language) = explode('.', $file->getFilename()); |
|
86 | 3 | if ($input->getOption('domain') && $domain !== $input->getOption('domain')) { |
|
87 | 1 | continue; |
|
88 | } |
||
89 | 3 | $output->writeln(sprintf('Starts importing file %s', $file->getRealPath())); |
|
90 | try { |
||
91 | 3 | $msgCatalog = $this->getLoader($inputFormat)->load($file->getRealPath(), $language, $domain); |
|
92 | |||
93 | 3 | $messages = $msgCatalog->all(); |
|
94 | |||
95 | 3 | if (!$messages) { |
|
96 | $output->writeln('No translations found in this file.'); |
||
97 | |||
98 | continue; |
||
99 | } |
||
100 | |||
101 | 3 | if (isset($catalogs[$language])) { |
|
102 | $catalogs[$language]->addCatalogue($msgCatalog); |
||
103 | } else { |
||
104 | 3 | $catalogs[$language] = $msgCatalog; |
|
105 | } |
||
106 | |||
107 | 3 | $output->writeln('Translation file saved.'); |
|
108 | 3 | } catch (\Exception $e) { |
|
109 | $output->writeln(sprintf('An error has occured while trying to write translations: %s', $e->getMessage())); |
||
110 | } |
||
111 | 3 | } |
|
112 | |||
113 | /** @var MessageCatalogue $catalog */ |
||
114 | 3 | foreach ($catalogs as $catalog) { |
|
115 | 3 | $this->getTranslationWriter()->writeTranslations($catalog, $outputFormat, array( |
|
116 | 3 | 'path' => $this->getTranslationPath($input->getOption('output-path'))) |
|
117 | 3 | ); |
|
118 | 3 | } |
|
119 | |||
120 | |||
121 | 3 | } |
|
122 | |||
123 | /** |
||
124 | * Returns Symfony translation writer service |
||
125 | * |
||
126 | * @return TranslationWriter |
||
127 | */ |
||
128 | 3 | protected function getTranslationWriter() |
|
132 | |||
133 | /** |
||
134 | * Returns Symfony requested format loader |
||
135 | * |
||
136 | * @param string $format |
||
137 | * |
||
138 | * @return \Symfony\Component\Translation\Loader\LoaderInterface |
||
139 | * |
||
140 | * @throws \InvalidArgumentException |
||
141 | */ |
||
142 | 3 | View Code Duplication | protected function getLoader($format) |
152 | |||
153 | /** |
||
154 | * Returns Symfony requested format dumper |
||
155 | * |
||
156 | * @param string $format |
||
157 | * |
||
158 | * @return \Symfony\Component\Translation\Dumper\DumperInterface |
||
159 | * |
||
160 | * @throws \InvalidArgumentException |
||
161 | */ |
||
162 | 3 | View Code Duplication | protected function getDumper($format) |
172 | |||
173 | /** |
||
174 | * Returns translation path |
||
175 | * |
||
176 | * @param string $outputPath |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | 3 | protected function getTranslationPath($outputPath = null) |
|
188 | } |
||
189 |
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.