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 |
||
36 | class ParamFetcher implements ParamFetcherInterface |
||
37 | { |
||
38 | use ResolverTrait; |
||
39 | |||
40 | private $container; |
||
41 | private $parameterBag; |
||
42 | private $requestStack; |
||
43 | private $validator; |
||
44 | private $violationFormatter; |
||
45 | /** |
||
46 | * @var callable |
||
47 | */ |
||
48 | private $controller; |
||
49 | |||
50 | /** |
||
51 | * Initializes fetcher. |
||
52 | * |
||
53 | * @param ContainerInterface $container |
||
54 | * @param ParamReaderInterface $paramReader |
||
55 | * @param RequestStack $requestStack |
||
56 | * @param ValidatorInterface $validator |
||
57 | * @param ViolationFormatterInterface $violationFormatter |
||
58 | */ |
||
59 | 22 | public function __construct(ContainerInterface $container, ParamReaderInterface $paramReader, RequestStack $requestStack, ViolationFormatterInterface $violationFormatter, ValidatorInterface $validator = null) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 11 | public function setController($controller) |
|
76 | |||
77 | /** |
||
78 | * Add additional params to the ParamFetcher during runtime. |
||
79 | * |
||
80 | * Note that adding a param that has the same name as an existing param will override that param. |
||
81 | * |
||
82 | * @param ParamInterface $param |
||
83 | */ |
||
84 | 1 | public function addParam(ParamInterface $param) |
|
88 | |||
89 | /** |
||
90 | * @return ParamInterface[] |
||
91 | */ |
||
92 | 11 | public function getParams() |
|
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 8 | public function get($name, $strict = null) |
|
117 | |||
118 | /** |
||
119 | * @param ParamInterface $param |
||
120 | * @param mixed $paramValue |
||
121 | * @param bool $strict |
||
122 | * |
||
123 | * @throws BadRequestHttpException |
||
124 | * @throws \RuntimeException |
||
125 | * |
||
126 | * @return mixed |
||
127 | * |
||
128 | * @internal |
||
129 | */ |
||
130 | 11 | protected function cleanParamWithRequirements(ParamInterface $param, $paramValue, $strict) |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | 10 | public function all($strict = null) |
|
194 | |||
195 | /** |
||
196 | * Check if current param is not in conflict with other parameters |
||
197 | * according to the "incompatibles" field. |
||
198 | * |
||
199 | * @param ParamInterface $param the configuration for the param fetcher |
||
200 | * |
||
201 | * @throws InvalidArgumentException |
||
202 | * @throws BadRequestHttpException |
||
203 | * |
||
204 | * @internal |
||
205 | */ |
||
206 | 7 | protected function checkNotIncompatibleParams(ParamInterface $param) |
|
226 | |||
227 | /** |
||
228 | * @param Constraint[] $constraints |
||
229 | */ |
||
230 | 10 | private function resolveConstraints(array $constraints) |
|
238 | |||
239 | /** |
||
240 | * @throws \RuntimeException |
||
241 | * |
||
242 | * @return Request |
||
243 | */ |
||
244 | 14 | View Code Duplication | private function getRequest() |
253 | } |
||
254 |
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.