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 |
||
11 | class PhpArray extends Extractor implements ExtractorInterface |
||
12 | { |
||
13 | /** |
||
14 | * Extract the translations from a file. |
||
15 | * |
||
16 | * @param array|string $file A path of a file or files |
||
17 | * @param null|Translations $translations The translations instance to append the new translations. |
||
18 | * |
||
19 | * @return Translations |
||
20 | */ |
||
21 | View Code Duplication | public static function fromFile($file, Translations $translations = null) |
|
|
|||
22 | { |
||
23 | if ($translations === null) { |
||
24 | $translations = new Translations(); |
||
25 | } |
||
26 | |||
27 | foreach (static::getFiles($file) as $file) { |
||
28 | static::extract(include($file), $translations); |
||
29 | } |
||
30 | |||
31 | return $translations; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public static function fromString($string, Translations $translations = null, $file = '') |
||
41 | |||
42 | /** |
||
43 | * Handle an array of translations and append to the Translations instance. |
||
44 | * |
||
45 | * @param array $content |
||
46 | * @param Translations $translations |
||
47 | */ |
||
48 | public static function extract(array $content, Translations $translations) |
||
70 | } |
||
71 |
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.