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 |
||
21 | final class BuiltParameterResolver implements ParameterResolverInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $defaultValue; |
||
27 | |||
28 | /** |
||
29 | * @var ConfigRepository |
||
30 | */ |
||
31 | private $config; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $parameters; |
||
37 | |||
38 | public function __construct(array $parameters, ConfigRepository $config) |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc}array |
||
47 | */ |
||
48 | public function resolve($parameter) |
||
52 | |||
53 | /** |
||
54 | * @param mixed $value |
||
55 | * @param array $resolving |
||
56 | * |
||
57 | * @return mixed |
||
58 | * |
||
59 | * @throws ParameterCircularReferenceException |
||
60 | * @throws ParameterNotFoundException |
||
61 | */ |
||
62 | View Code Duplication | private function resolveValue($value, $resolving = []) |
|
78 | |||
79 | View Code Duplication | private function resolveArray(array $arrayValue, array $resolving) |
|
88 | |||
89 | /** |
||
90 | * @param $value |
||
91 | * @param $resolving |
||
92 | * |
||
93 | * @return array|mixed |
||
94 | * @throws ParameterCircularReferenceException |
||
95 | * @throws ParameterNotFoundException |
||
96 | */ |
||
97 | private function resolveString($value, array $resolving) |
||
121 | |||
122 | /** |
||
123 | * @param string $key |
||
124 | * |
||
125 | * @return string|int|bool|null |
||
126 | * @throws ParameterNotFoundException |
||
127 | */ |
||
128 | View Code Duplication | private function resolveEnvironmentValue($key) |
|
138 | } |
||
139 |
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.