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 |
||
20 | class EntryCollection implements ResponseInterface, \IteratorAggregate, \ArrayAccess, \Countable |
||
21 | { |
||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $items = []; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $rawData = []; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $metadata = []; |
||
36 | |||
37 | /** |
||
38 | * EntryCollection constructor. |
||
39 | * |
||
40 | * @param $data |
||
41 | */ |
||
42 | 8 | public function __construct($data) |
|
55 | |||
56 | /** |
||
57 | * Pick the metadata out of the raw data |
||
58 | * |
||
59 | * @param $data |
||
60 | * |
||
61 | 2 | * @return array|null |
|
62 | */ |
||
63 | 2 | protected function pickMetadata($data) |
|
74 | |||
75 | /** |
||
76 | * Pick the "rows" (items) out of the raw data |
||
77 | * |
||
78 | * @param $data |
||
79 | * |
||
80 | * @return array |
||
81 | 2 | */ |
|
82 | protected function pickRows($data) |
||
93 | 2 | ||
94 | /** |
||
95 | * Get the response raw data |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | public function getRawData() |
||
103 | |||
104 | /** |
||
105 | * Get the response entries |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getData() |
||
113 | |||
114 | /** |
||
115 | * Get the response metadata |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | public function getMetaData() |
||
123 | 2 | ||
124 | /** |
||
125 | * Create a new iterator based on this collection |
||
126 | * |
||
127 | * @return \ArrayIterator |
||
128 | */ |
||
129 | public function getIterator() |
||
133 | |||
134 | 2 | public function offsetExists($offset) |
|
138 | |||
139 | public function offsetGet($offset) |
||
143 | |||
144 | public function offsetSet($offset, $value) |
||
148 | |||
149 | public function offsetUnset($offset) |
||
153 | |||
154 | /** |
||
155 | * Gets the number of entries in this collection |
||
156 | * |
||
157 | * @return int |
||
158 | */ |
||
159 | public function count() |
||
163 | |||
164 | /** |
||
165 | * Gets an object representation of this collection |
||
166 | * |
||
167 | * @return object |
||
168 | */ |
||
169 | public function jsonSerialize() |
||
176 | } |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.