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 | * Get an item from an array using "dot" notation. |
||
| 51 | * |
||
| 52 | * @param array $array |
||
| 53 | * @param string|callable $key |
||
|
|
|||
| 54 | * @param mixed $default |
||
| 55 | * |
||
| 56 | * @return mixed |
||
| 57 | */ |
||
| 58 | public function get(array $array, $key = null, $default = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Add an element to the array at a specific location |
||
| 81 | * using the "dot" notation. |
||
| 82 | * |
||
| 83 | * @param array $array |
||
| 84 | * @param $key |
||
| 85 | * @param $value |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function add(array &$array, $key, $value) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Check if an item exists in an array using "dot" notation. |
||
| 105 | * |
||
| 106 | * @param array $array |
||
| 107 | * @param string $key |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function has(array $array, $key) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Updates data at the given path. |
||
| 134 | * |
||
| 135 | * @param array $array |
||
| 136 | * @param array|string $key |
||
| 137 | * @param callable $cb Callback to update the value. |
||
| 138 | * |
||
| 139 | * @return mixed Updated data. |
||
| 140 | */ |
||
| 141 | public function update(array $array, $key, callable $cb) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Remove one or many array items from a given array using "dot" notation. |
||
| 161 | * |
||
| 162 | * @param array $array |
||
| 163 | * @param array|string $keys |
||
| 164 | */ |
||
| 165 | public function forget(array &$array, $keys) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Reset all numerical indexes of an array (start from zero). |
||
| 196 | * Non-numerical indexes will stay untouched. Returns a new array. |
||
| 197 | * |
||
| 198 | * @param array $array |
||
| 199 | * @param bool|false $deep |
||
| 200 | * |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | public function reset(array $array, $deep = false) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get all of the given array except for a specified array of items. |
||
| 224 | * |
||
| 225 | * @param array $array |
||
| 226 | * @param string[] $keys |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function except(array $array, $keys) |
||
| 236 | } |
||
| 237 |
This check looks for
@paramannotations 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
arrayand 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.