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) |
||
48 | |||
49 | /** |
||
50 | * Add an element to an array if it doesn't exist. |
||
51 | * |
||
52 | * @param array $array |
||
53 | * @param string $key |
||
54 | * @param string $value |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public function add(array $array, $key, $value) |
||
66 | |||
67 | /** |
||
68 | * Get an item from an array using "dot" notation. |
||
69 | * |
||
70 | * @param array $array |
||
71 | * @param string|callable $key |
||
|
|||
72 | * @param mixed $default |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function get(array $array, $key = null, $default = null) |
||
96 | |||
97 | /** |
||
98 | * Check if an item exists in an array using "dot" notation. |
||
99 | * |
||
100 | * @param array $array |
||
101 | * @param string $key |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function has(array $array, $key) |
||
125 | |||
126 | /** |
||
127 | * Updates data at the given path. |
||
128 | * |
||
129 | * @param array $array |
||
130 | * @param array|string $key |
||
131 | * @param callable $cb Callback to update the value. |
||
132 | * |
||
133 | * @return mixed Updated data. |
||
134 | */ |
||
135 | public function update(array $array, $key, callable $cb) |
||
152 | |||
153 | /** |
||
154 | * Remove one or many array items from a given array using "dot" notation. |
||
155 | * |
||
156 | * @param array $array |
||
157 | * @param array|string $keys |
||
158 | */ |
||
159 | public function forget(array &$array, $keys) |
||
187 | |||
188 | /** |
||
189 | * Get all of the given array except for a specified array of items. |
||
190 | * |
||
191 | * @param array $array |
||
192 | * @param string[] $keys |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | public function except($array, $keys) |
||
202 | } |
||
203 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.