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 |
||
| 9 | final class Collection implements \Iterator, \Countable |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * API Client |
||
| 13 | * |
||
| 14 | * @var Client |
||
| 15 | */ |
||
| 16 | private $_client; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * limit to give to API |
||
| 20 | * |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private $_limit; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * offset to give to API |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | private $_offset; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * resource name for collection |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $_resource; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * array of filters to pass to API |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $_filters; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Total number of elements in the collection |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $_total; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * pointer in the paginated results |
||
| 55 | * |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | private $_position; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A paginated set of elements from the API |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $_result; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Create a new collection |
||
| 69 | * |
||
| 70 | * @param Client $client client connection to the API |
||
| 71 | * @param string $resource name of API resource to request |
||
| 72 | * @param array $filters key value pair array of search filters |
||
| 73 | */ |
||
| 74 | public function __construct(Client $client, $resource, array $filters = []) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @see Countable::count() |
||
| 86 | * |
||
| 87 | * @return int |
||
| 88 | */ |
||
| 89 | public function count() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @see Iterator::rewind() |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | public function rewind() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @see Iterator::key() |
||
| 114 | * |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function key() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @see Iterator::valid() |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function valid() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @see Iterator::next() |
||
| 144 | * |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | public function next() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @see Iterator::current() |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function current() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Returns the values from a single field this collection, identified by the given $key. |
||
| 192 | * |
||
| 193 | * @param string $key The name of the field for which the values will be returned. |
||
| 194 | * |
||
| 195 | * @return iterable |
||
| 196 | */ |
||
| 197 | public function column($key) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return an iterable generator containing only the fields specified in the $keys array. |
||
| 206 | * |
||
| 207 | * @param array $keys The list of field names to be returned. |
||
| 208 | * |
||
| 209 | * @return \Generator |
||
| 210 | */ |
||
| 211 | public function select(array $keys) |
||
| 219 | } |
||
| 220 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..