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 |
||
7 | class Access |
||
8 | { |
||
9 | use SplitPathTrait; |
||
10 | use ValueTrait; |
||
11 | |||
12 | /** |
||
13 | * Set an array item to a given value using "dot" notation. |
||
14 | * |
||
15 | * If no key is given to the method, the entire array will be replaced. |
||
16 | * |
||
17 | * @param array $array |
||
18 | * @param string $key |
||
19 | * @param mixed $value |
||
20 | * |
||
21 | * @return array |
||
22 | */ |
||
23 | public function set(array &$array, $key, $value) |
||
50 | |||
51 | /** |
||
52 | * Get an item from an array using "dot" notation. |
||
53 | * |
||
54 | * @param array $array |
||
55 | * @param string|callable $key |
||
56 | * @param mixed $default |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | public function get(array $array, $key = null, $default = null) |
||
80 | |||
81 | /** |
||
82 | * Add an element to the array at a specific location |
||
83 | * using the "dot" notation. |
||
84 | * |
||
85 | * @param array $array |
||
86 | * @param $key |
||
87 | * @param $value |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | public function add(array &$array, $key, $value) |
||
104 | |||
105 | /** |
||
106 | * Get a value from the array, and remove it. |
||
107 | * |
||
108 | * @param array $array |
||
109 | * @param string $key |
||
110 | * @param string|null $default |
||
111 | * |
||
112 | * @return mixed |
||
113 | */ |
||
114 | public function pull(array &$array, $key, $default = null) |
||
122 | |||
123 | /** |
||
124 | * Check if an item exists in an array using "dot" notation. |
||
125 | * |
||
126 | * @param array $array |
||
127 | * @param string $key |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function has(array $array, $key) |
||
151 | |||
152 | /** |
||
153 | * Updates data at the given path. |
||
154 | * |
||
155 | * @param array $array |
||
156 | * @param array|string $key |
||
157 | * @param callable $cb Callback to update the value. |
||
158 | * |
||
159 | * @return mixed Updated data. |
||
160 | */ |
||
161 | public function update(array $array, $key, callable $cb) |
||
178 | |||
179 | /** |
||
180 | * Remove one or many array items from a given array using "dot" notation. |
||
181 | * |
||
182 | * @param array $array |
||
183 | * @param array|string $keys |
||
184 | */ |
||
185 | public function forget(array &$array, $keys) |
||
213 | |||
214 | /** |
||
215 | * Reset all numerical indexes of an array (start from zero). |
||
216 | * Non-numerical indexes will stay untouched. Returns a new array. |
||
217 | * |
||
218 | * @param array $array |
||
219 | * @param bool|false $deep |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | public function reset(array $array, $deep = false) |
||
241 | |||
242 | /** |
||
243 | * Get all of the given array except for a specified array of items. |
||
244 | * |
||
245 | * @param array $array |
||
246 | * @param string[] $keys |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | public function except(array $array, $keys) |
||
256 | } |
||
257 |