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 |
||
15 | class ImportCommandHandler extends AbstractCommandHandler |
||
16 | { |
||
17 | /** |
||
18 | * TranslationFileExplorer |
||
19 | * |
||
20 | * @var TranslationFileExplorer |
||
21 | */ |
||
22 | private $translationFileExplorer; |
||
23 | |||
24 | /** |
||
25 | * Importer |
||
26 | * |
||
27 | * @var Importer |
||
28 | */ |
||
29 | private $importer; |
||
30 | |||
31 | /** |
||
32 | * Execute an import command |
||
33 | * |
||
34 | * @param ImportCommand $importCommand |
||
35 | * |
||
36 | * @return int total number of files imported |
||
|
|||
37 | */ |
||
38 | 1 | public function executeImportCommand(ImportCommand $importCommand) |
|
39 | { |
||
40 | 1 | if ($importCommand->getGlobals() || $importCommand->getDefaultBundle() === false || $importCommand->getDefaultBundle() === null) { |
|
41 | 1 | return $this->importGlobalTranslationFiles($importCommand); |
|
42 | } |
||
43 | |||
44 | return $this->importBundleTranslationFiles($importCommand); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Import all translation files from app resources |
||
49 | * |
||
50 | * @param ImportCommand $importCommand |
||
51 | * |
||
52 | * @return int total number of files imported |
||
53 | */ |
||
54 | 1 | private function importGlobalTranslationFiles(ImportCommand $importCommand) |
|
55 | { |
||
56 | 1 | $baseDir = Kernel::VERSION_ID >= 40000 ? $this->kernel->getProjectDir() : $this->kernel->getRootDir(); |
|
57 | 1 | $translationsDir = Kernel::VERSION_ID >= 40000 ? 'translations' : null; |
|
58 | 1 | $locales = $this->determineLocalesToImport($importCommand); |
|
59 | 1 | $finder = $this->translationFileExplorer->find($baseDir, $locales, $translationsDir); |
|
60 | |||
61 | 1 | return $this->importTranslationFiles($finder, $importCommand->getForce()); |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Import all translation files from a specific bundle, bundle name will be lowercased so cases don't matter |
||
66 | * |
||
67 | * @param ImportCommand $importCommand |
||
68 | * |
||
69 | * @return int total number of files imported |
||
70 | */ |
||
71 | 1 | public function importBundleTranslationFiles(ImportCommand $importCommand) |
|
72 | { |
||
73 | 1 | $importBundle = strtolower($importCommand->getDefaultBundle()); |
|
74 | |||
75 | 1 | if ($importBundle == 'all') { |
|
76 | $importCount = $this->importAllBundlesTranslationFiles($importCommand); |
||
77 | |||
78 | if (Kernel::VERSION_ID >= 40000) { |
||
79 | $importCount += $this->importSf4TranslationFiles($importCommand); |
||
80 | } |
||
81 | |||
82 | return $importCount; |
||
83 | 1 | } elseif ($importBundle == 'custom') { |
|
84 | return $this->importCustomBundlesTranslationFiles($importCommand); |
||
85 | } else { |
||
86 | 1 | if (Kernel::VERSION_ID >= 40000) { |
|
87 | 1 | return $this->importSf4TranslationFiles($importCommand); |
|
88 | } |
||
89 | |||
90 | return $this->importOwnBundlesTranslationFiles($importCommand); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Import all translation files from all registered bundles (in AppKernel) |
||
96 | * |
||
97 | * @param ImportCommand $importCommand |
||
98 | * |
||
99 | * @return int total number of files imported |
||
100 | */ |
||
101 | View Code Duplication | private function importAllBundlesTranslationFiles(ImportCommand $importCommand) |
|
118 | |||
119 | /** |
||
120 | * Import all translation files from your own registered bundles (in src/ directory) |
||
121 | * |
||
122 | * @param ImportCommand $importCommand |
||
123 | * |
||
124 | * @return int The total number of imported files |
||
125 | */ |
||
126 | private function importOwnBundlesTranslationFiles(ImportCommand $importCommand) |
||
145 | |||
146 | /** |
||
147 | * Import the translation files from the defined bundles. |
||
148 | * |
||
149 | * @param ImportCommand $importCommand |
||
150 | * |
||
151 | * @return int The total number of imported files |
||
152 | */ |
||
153 | View Code Duplication | private function importCustomBundlesTranslationFiles(ImportCommand $importCommand) |
|
169 | |||
170 | /** |
||
171 | * Import all translation files from a single bundle |
||
172 | * |
||
173 | * @param ImportCommand $importCommand |
||
174 | * |
||
175 | * @return int total number of files imported |
||
176 | */ |
||
177 | private function importSingleBundleTranslationFiles(ImportCommand $importCommand) |
||
189 | |||
190 | /** |
||
191 | * Import translation files from a specific Finder object |
||
192 | * The finder object shoud already have the files to look for defined; |
||
193 | * Forcing the import will override all existing translations in the stasher |
||
194 | * |
||
195 | * @param Finder $finder |
||
196 | * @param bool $force override identical translations in the stasher (domain/locale and keyword combination) |
||
197 | * |
||
198 | * @return int total number of files imported |
||
199 | */ |
||
200 | 2 | private function importTranslationFiles(Finder $finder, $force = flase) |
|
201 | { |
||
202 | 2 | if (!$finder instanceof Finder) { |
|
203 | return false; |
||
204 | } |
||
205 | |||
206 | 2 | $imported = 0; |
|
207 | |||
208 | 2 | foreach ($finder as $file) { |
|
209 | 2 | $imported += $this->importer->import($file, $force); |
|
210 | } |
||
211 | |||
212 | 2 | return $imported; |
|
213 | } |
||
214 | |||
215 | /** |
||
216 | * Validates that a bundle is registered in the AppKernel |
||
217 | * |
||
218 | * @param string $bundle |
||
219 | * |
||
220 | * @return bool bundle is valid or not |
||
221 | * |
||
222 | * @throws \Exception If the bundlename isn't valid |
||
223 | */ |
||
224 | public function validateBundleName($bundle) |
||
235 | |||
236 | /** |
||
237 | * Gives an array with all languages that needs to be imported (from the given ImportCommand) |
||
238 | * If non is given, all managed locales will be used (defined in config) |
||
239 | * |
||
240 | * @param ImportCommand $importCommand |
||
241 | * |
||
242 | * @return array all locales to import by the given ImportCommand |
||
243 | */ |
||
244 | 3 | public function determineLocalesToImport(ImportCommand $importCommand) |
|
252 | |||
253 | 6 | public function setTranslationFileExplorer($translationFileExplorer) |
|
257 | |||
258 | 6 | public function setImporter($importer) |
|
262 | |||
263 | 1 | private function importSf4TranslationFiles($importCommand) |
|
264 | { |
||
265 | 1 | $finder = $this->translationFileExplorer->find($this->kernel->getProjectDir(), $this->determineLocalesToImport($importCommand), 'translations'); |
|
266 | |||
273 | } |
||
274 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.