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 |
||
6 | class DiffStorageStoreRowData implements DiffStorageStoreRowDataInterface { |
||
7 | /** @var array */ |
||
8 | private $row; |
||
9 | /** @var array */ |
||
10 | private $keys; |
||
11 | /** @var array */ |
||
12 | private $valueKeys; |
||
13 | /** @var array */ |
||
14 | private $converter; |
||
15 | /** @var array */ |
||
16 | private $foreignRow; |
||
17 | |||
18 | /** |
||
19 | * @param array $row |
||
20 | * @param array $foreignRow |
||
21 | * @param array $keys |
||
22 | * @param array $valueKeys |
||
23 | * @param array $converter |
||
24 | */ |
||
25 | public function __construct(array $row = [], array $foreignRow = [], array $keys, array $valueKeys, array $converter) { |
||
32 | |||
33 | /** |
||
34 | * @param array $options |
||
35 | * @return array |
||
36 | */ |
||
37 | public function getData(array $options = []) { |
||
40 | |||
41 | /** |
||
42 | * @param array $options |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getForeignData(array $options = []) { |
||
48 | |||
49 | /** |
||
50 | * @param array $options |
||
51 | * @return array |
||
52 | */ |
||
53 | public function getKeyData(array $options = []) { |
||
58 | |||
59 | /** |
||
60 | * @param array $options |
||
61 | * @return array |
||
62 | */ |
||
63 | public function getValueData(array $options = []) { |
||
68 | |||
69 | /** |
||
70 | * @param array $fields |
||
71 | * @return array |
||
72 | */ |
||
73 | public function getDiff(array $fields = null) { |
||
99 | |||
100 | /** |
||
101 | * @param array $fields |
||
102 | * @param mixed $format |
||
103 | * @return array |
||
104 | * @throws Exception |
||
105 | */ |
||
106 | public function getDiffFormatted(array $fields = null, $format = null) { |
||
117 | |||
118 | /** |
||
119 | * @param array $row |
||
120 | * @param array $options |
||
121 | * @return array |
||
122 | */ |
||
123 | private function applyOptions(array $row, array $options) { |
||
124 | if(count($options) < 1) { |
||
125 | return $row; |
||
126 | } |
||
127 | View Code Duplication | if(array_key_exists('keys', $options) && is_array($options['keys'])) { |
|
|
|||
128 | $row = array_intersect_key($row, array_combine($options['keys'], $options['keys'])); |
||
129 | } |
||
130 | View Code Duplication | if(array_key_exists('ignore', $options) && is_array($options['ignore'])) { |
|
131 | $row = array_diff_key($row, array_combine($options['ignore'], $options['ignore'])); |
||
132 | } |
||
133 | return $row; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param array $row |
||
138 | * @return array |
||
139 | */ |
||
140 | private function formatRow($row) { |
||
146 | } |
||
147 |
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.