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 |
||
11 | class ResultObjectMapper implements ResultObjectMapperInterface |
||
12 | { |
||
13 | /** |
||
14 | * Map each item in response data to instance of ResultObjectInterface |
||
15 | * @param array $response |
||
16 | * @param MethodResultCollectionInterface $method |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | public function mapCollection(array $response, MethodResultCollectionInterface $method) |
||
30 | |||
31 | /** |
||
32 | * Map response data to instance of ResultObjectInterface |
||
33 | * @param array $response |
||
34 | * @param ResultObjectInterface $result |
||
35 | * |
||
36 | * @return ResultObjectInterface |
||
37 | */ |
||
38 | public function map(array $response, ResultObjectInterface $result) |
||
60 | |||
61 | /** |
||
62 | * Extracts camelCased setter name from underscore notation. |
||
63 | * Eg. my_field_name => myFieldName |
||
64 | * @param string $field |
||
65 | * @return string |
||
66 | */ |
||
67 | View Code Duplication | private function getSetterName($field) |
|
75 | |||
76 | /** |
||
77 | * Transform PaymentInstrument result array to object |
||
78 | * @param array $data |
||
79 | * @param string $method |
||
80 | * @return PaymentInstrumentCard|PaymentInstrumentRecurring |
||
81 | * @throws Exception\Runtime for unsupported methods |
||
82 | */ |
||
83 | private function transformPaymentInstrumentValue(array $data, $method) |
||
97 | |||
98 | /** |
||
99 | * Transform AuthorizationInformation result array to object |
||
100 | * @param array $data |
||
101 | * @return AuthorizationInformation |
||
102 | */ |
||
103 | private function transformAuthorizationInformationValue($data) |
||
110 | |||
111 | /** |
||
112 | * @param ARRAY $data |
||
113 | * @return ThreeDS2AuthorizationInformation |
||
114 | */ |
||
115 | private function transformThreeDS2DataValue($data) |
||
121 | } |
||
122 |
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.