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