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 ArrayAccessor |
||
7 | { |
||
8 | |||
9 | /** |
||
10 | * Replace an array key with another while keeping value |
||
11 | * |
||
12 | * @param array $array |
||
13 | * @param $key1 |
||
14 | * @param $key2 |
||
15 | * @return array |
||
16 | */ |
||
17 | 1 | public static function replaceKey(&$array, $key1, $key2) |
|
29 | |||
30 | /** |
||
31 | * Get a reference from an array using a path with dot notation. Ex: |
||
32 | * |
||
33 | * array( |
||
34 | * 'person' => [ |
||
35 | * 'name' => 'john', |
||
36 | * 'lastname' => 'smith', |
||
37 | * 'childs' => [ |
||
38 | * [ 'name' => 'victor'], |
||
39 | * [ 'name' => 'david'] |
||
40 | * ] |
||
41 | * ] |
||
42 | * ) |
||
43 | * |
||
44 | * will return for: |
||
45 | * |
||
46 | * person.name = john |
||
47 | * person.childs.0.name = victor |
||
48 | * |
||
49 | * @param array $data |
||
50 | * @param $path |
||
51 | * @param $default |
||
52 | * @return array|null |
||
53 | */ |
||
54 | 3 | public static function dget(array &$data, $path, $default = null) |
|
66 | |||
67 | /** |
||
68 | * Set a reference from an array using a path with dot notation. Ex: |
||
69 | * |
||
70 | * array( |
||
71 | * 'person' => [ |
||
72 | * 'name' => 'john', |
||
73 | * 'lastname'='smith', |
||
74 | * 'childs': [ |
||
75 | * [ 'name' => 'victor'], |
||
76 | * [ 'name' => 'david'] |
||
77 | * ] |
||
78 | * ] |
||
79 | * ) |
||
80 | * |
||
81 | * will set for: |
||
82 | * |
||
83 | * person.name = john |
||
84 | * person.childs.0.name = victor |
||
85 | * |
||
86 | * |
||
87 | * @param array $data |
||
88 | * @param $path |
||
89 | * @param $value |
||
90 | */ |
||
91 | 1 | public static function dset(array &$data, $path, $value) |
|
105 | |||
106 | /** |
||
107 | * Count a reference from an array using a path with do notation. Ex: |
||
108 | * |
||
109 | * array( |
||
110 | * 'person' => [ |
||
111 | * 'name' => 'john', |
||
112 | * 'lastname'='smith', |
||
113 | * 'childs': [ |
||
114 | * [ 'name' => 'victor'], |
||
115 | * [ 'name' => 'david'] |
||
116 | * ] |
||
117 | * ] |
||
118 | * ) |
||
119 | * |
||
120 | * will return for: |
||
121 | * |
||
122 | * person.name = 1 |
||
123 | * person.childs = 2 |
||
124 | * |
||
125 | * @param array $data |
||
126 | * @param $path |
||
127 | * @return int|null |
||
128 | */ |
||
129 | 1 | public static function dcount(array &$data, $path) |
|
142 | |||
143 | /** |
||
144 | * Deletes a reference from an array using a path with do notation. Ex: |
||
145 | * |
||
146 | * array( |
||
147 | * 'person' => [ |
||
148 | * 'name' => 'john', |
||
149 | * 'lastname'='smith', |
||
150 | * 'childs': [ |
||
151 | * [ 'name' => 'victor'], |
||
152 | * [ 'name' => 'david'] |
||
153 | * ] |
||
154 | * ] |
||
155 | * ) |
||
156 | * |
||
157 | * will delete for: |
||
158 | * |
||
159 | * person.name |
||
160 | * |
||
161 | * will result in |
||
162 | * |
||
163 | * array( |
||
164 | * 'person' => [ |
||
165 | * 'lastname'='smith', |
||
166 | * 'childs': [ |
||
167 | * [ 'name' => 'victor'], |
||
168 | * [ 'name' => 'david'] |
||
169 | * ] |
||
170 | * ] |
||
171 | * ) |
||
172 | * |
||
173 | * @param array $data |
||
174 | * @param $path |
||
175 | */ |
||
176 | 1 | public static function ddel(array &$data, $path) |
|
189 | |||
190 | /** |
||
191 | * @param $array |
||
192 | * @param $key |
||
193 | * @param null $default |
||
194 | * @return null |
||
195 | */ |
||
196 | 1 | public static function get_key_exist($array, $key, $default = null) { |
|
204 | } |
||
205 |
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.