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 |
||
20 | final class AutoloadPrefixer |
||
21 | { |
||
22 | /** |
||
23 | * @param array $content Decoded JSON |
||
24 | * @param string $prefix |
||
25 | * |
||
26 | * @return array Prefixed decoded JSON |
||
27 | */ |
||
28 | 7 | public static function prefixPackageAutoloads(array $content, string $prefix): array |
|
40 | |||
41 | 7 | private static function prefixAutoloads(array $autoload, string $prefix): array |
|
42 | { |
||
43 | 7 | $autoload['psr-4'] = isset($autoload['psr-4']) ? $autoload['psr-4'] : []; |
|
44 | |||
45 | 7 | if (isset($autoload['psr-0'])) { |
|
46 | 5 | $autoload['psr-4'] = self::mergePSR0And4($autoload['psr-0'], $autoload['psr-4']); |
|
47 | } |
||
48 | 7 | unset($autoload['psr-0']); |
|
49 | |||
50 | 7 | if (isset($autoload['psr-4'])) { |
|
51 | 7 | $autoload['psr-4'] = self::prefixAutoload($autoload['psr-4'], $prefix); |
|
52 | } |
||
53 | |||
54 | 7 | return $autoload; |
|
55 | } |
||
56 | |||
57 | 7 | private static function prefixAutoload(array $autoload, string $prefix): array |
|
67 | |||
68 | 5 | private static function mergePSR0And4(array $psr0, array $psr4): array |
|
88 | |||
89 | 5 | private static function updatePSR0Path($path, $namespace) |
|
113 | |||
114 | /** |
||
115 | * Deals with the 4 possible scenarios: |
||
116 | * PSR0 | PSR4 |
||
117 | * array | |
||
118 | * string | |
||
119 | * or simply the namepace not existing as a psr-4 entry. |
||
120 | * |
||
121 | * @param string $psr0Namespace |
||
122 | * @param string|array $psr0Path |
||
123 | * @param string|array $psr4 |
||
124 | * |
||
125 | * @return string|array |
||
126 | */ |
||
127 | 3 | private static function mergeNamespaces(string $psr0Namespace, $psr0Path, $psr4) |
|
153 | } |
||
154 |
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.