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 ConverterProvider |
||
26 | { |
||
27 | /** |
||
28 | * A cache of [@see ResponseBodyConverter]'s |
||
29 | * |
||
30 | * @var ResponseBodyConverter[] |
||
31 | */ |
||
32 | private $responseBodyConverters = []; |
||
33 | |||
34 | /** |
||
35 | * A cache of [@see RequestBodyConverter]'s |
||
36 | * |
||
37 | * @var RequestBodyConverter[] |
||
38 | */ |
||
39 | private $requestBodyConverters = []; |
||
40 | |||
41 | /** |
||
42 | * A cache of [@see StringConverter]'s |
||
43 | * |
||
44 | * @var StringConverter[] |
||
45 | */ |
||
46 | private $stringConverters = []; |
||
47 | |||
48 | /** |
||
49 | * An array of [@see ConverterFactory]'s |
||
50 | * |
||
51 | * @var ConverterFactory[] |
||
52 | */ |
||
53 | private $converterFactories; |
||
54 | |||
55 | /** |
||
56 | * Constructor |
||
57 | * |
||
58 | * @param ConverterFactory[] $factories |
||
59 | */ |
||
60 | 48 | public function __construct(array $factories) |
|
64 | |||
65 | /** |
||
66 | * Get a response body converter for type |
||
67 | * |
||
68 | * @param TypeToken $type |
||
69 | * @return ResponseBodyConverter |
||
70 | * @throws LogicException |
||
71 | */ |
||
72 | 16 | View Code Duplication | public function getResponseBodyConverter(TypeToken $type): ResponseBodyConverter |
95 | |||
96 | /** |
||
97 | * Get a request body converter for type |
||
98 | * |
||
99 | * @param TypeToken $type |
||
100 | * @return RequestBodyConverter |
||
101 | * @throws \LogicException |
||
102 | */ |
||
103 | 7 | View Code Duplication | public function getRequestBodyConverter(TypeToken $type): RequestBodyConverter |
126 | |||
127 | /** |
||
128 | * Get a string converter for type |
||
129 | * |
||
130 | * @param TypeToken $type |
||
131 | * @return StringConverter |
||
132 | * @throws \LogicException |
||
133 | */ |
||
134 | 13 | View Code Duplication | public function getStringConverter(TypeToken $type): StringConverter |
157 | } |
||
158 |
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.