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 |
||
25 | final class BaseParametersResolver implements ParametersResolverInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var ConfigRepository |
||
29 | */ |
||
30 | private $config; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $defaultValue; |
||
36 | |||
37 | /** |
||
38 | * @var ExpressionLanguage|null |
||
39 | */ |
||
40 | private $expressionLanguage; |
||
41 | |||
42 | /** |
||
43 | * @var array|null |
||
44 | 6 | */ |
|
45 | private $parameters; |
||
46 | 6 | ||
47 | 6 | /** |
|
48 | 6 | * @var array |
|
49 | */ |
||
50 | private $resolved = []; |
||
51 | |||
52 | public function __construct(ConfigRepository $config) |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | * |
||
61 | 6 | * @param array $parameters |
|
62 | * |
||
63 | 6 | * @return array |
|
64 | 6 | * |
|
65 | 6 | * @throws ParameterCircularReferenceException |
|
66 | 2 | * @throws ParameterNotFoundException |
|
67 | 1 | * @throws Exception |
|
68 | */ |
||
69 | 2 | public function resolve(array $parameters) |
|
79 | |||
80 | 6 | /** |
|
81 | * @param mixed $value |
||
82 | 6 | * @param array $resolving |
|
83 | 2 | * |
|
84 | * @return mixed |
||
85 | * @throws ParameterCircularReferenceException |
||
86 | 6 | * @throws ParameterNotFoundException |
|
87 | 2 | */ |
|
88 | private function resolveValue($value, $resolving = []) |
||
108 | |||
109 | View Code Duplication | private function resolveArray(array $arrayValue, array $resolving) |
|
118 | 2 | ||
119 | 2 | /** |
|
120 | * @param $value |
||
121 | * @param $resolving |
||
122 | * |
||
123 | * @return array|mixed |
||
124 | 6 | * @throws ParameterCircularReferenceException |
|
125 | * @throws ParameterNotFoundException |
||
126 | */ |
||
127 | 6 | private function resolveString($value, array $resolving) |
|
149 | 2 | ||
150 | /** |
||
151 | * @param string $key |
||
152 | 4 | * @param array $resolving |
|
153 | 2 | * |
|
154 | 1 | * @return array|mixed |
|
155 | 2 | * @throws ParameterCircularReferenceException |
|
156 | 1 | * @throws ParameterNotFoundException |
|
157 | 2 | */ |
|
158 | 1 | private function resolveParameter($key, array $resolving) |
|
178 | 2 | ||
179 | /** |
||
180 | * @param string $key |
||
181 | 2 | * |
|
182 | * @return string|int|bool|null |
||
183 | * @throws ParameterNotFoundException |
||
184 | */ |
||
185 | View Code Duplication | private function resolveEnvironmentValue($key) |
|
195 | |||
196 | /** |
||
197 | * @return ExpressionLanguage |
||
198 | * @throws RuntimeException |
||
199 | */ |
||
200 | private function getExpressionLanguage() |
||
211 | } |
||
212 |
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.