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