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 |
||
| 29 | class Reader extends AbstractCsv |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @inheritdoc |
||
| 33 | */ |
||
| 34 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Return a Filtered Iterator |
||
| 38 | * |
||
| 39 | * @param callable|null $callable a callable function to be applied to each Iterator item |
||
| 40 | * |
||
| 41 | * @return Iterator |
||
| 42 | */ |
||
| 43 | 104 | public function fetch(callable $callable = null) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Apply The callable function |
||
| 60 | * |
||
| 61 | * @param Iterator $iterator |
||
| 62 | * @param callable|null $callable |
||
| 63 | * |
||
| 64 | * @return Iterator |
||
| 65 | */ |
||
| 66 | 104 | protected function applyCallable(Iterator $iterator, callable $callable = null) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Returns a sequential array of all CSV lines |
||
| 77 | * |
||
| 78 | * The callable function will be applied to each Iterator item |
||
| 79 | * |
||
| 80 | * @param callable|null $callable a callable function |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | 42 | public function fetchAll(callable $callable = null) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Convert the Iterator into an array depending on the selected return type |
||
| 91 | * |
||
| 92 | * @param int $type |
||
| 93 | * @param Iterator $iterator |
||
| 94 | * @param bool $use_keys Whether to use the iterator element keys as index |
||
| 95 | * |
||
| 96 | * @return Iterator|array |
||
| 97 | */ |
||
| 98 | 88 | protected function applyReturnType($type, Iterator $iterator, $use_keys = true) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Applies a callback function on the CSV |
||
| 109 | * |
||
| 110 | * The callback function must return TRUE in order to continue |
||
| 111 | * iterating over the iterator. |
||
| 112 | * |
||
| 113 | * @param callable $callable a callable function to apply to each selected CSV rows |
||
| 114 | * |
||
| 115 | * @return int the iteration count |
||
| 116 | */ |
||
| 117 | 6 | public function each(callable $callable) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Returns a single row from the CSV |
||
| 137 | * |
||
| 138 | * By default if no offset is provided the first row of the CSV is selected |
||
| 139 | * |
||
| 140 | * @param int $offset the CSV row offset |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | 8 | public function fetchOne($offset = 0) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Returns a single column from the CSV data |
||
| 156 | * |
||
| 157 | * The callable function will be applied to each value to be return |
||
| 158 | * |
||
| 159 | * By default if no column index is provided the first column of the CSV is selected |
||
| 160 | * |
||
| 161 | * @param int $columnIndex field Index |
||
| 162 | * @param callable|null $callable a callable function to apply to each selected CSV rows column value. |
||
| 163 | * The callable takes up to three parameter |
||
| 164 | * - the column value |
||
| 165 | * - the row index |
||
| 166 | * - the CSV Iterator |
||
| 167 | * |
||
| 168 | * @return Iterator|array |
||
| 169 | */ |
||
| 170 | 16 | public function fetchColumn($columnIndex = 0, callable $callable = null) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Validate a CSV row index |
||
| 192 | * |
||
| 193 | * @param int $index |
||
| 194 | * |
||
| 195 | * @throws InvalidArgumentException If the column index is not a positive integer or 0 |
||
| 196 | */ |
||
| 197 | 30 | protected function assertValidColumnIndex($index) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Retrive CSV data as pairs |
||
| 206 | * |
||
| 207 | * Fetches an associative array of all rows as key-value pairs (first |
||
| 208 | * column is the key, second column is the value). |
||
| 209 | * |
||
| 210 | * By default if no column index is provided: |
||
| 211 | * - the first CSV column is used to provide the keys |
||
| 212 | * - the second CSV column is used to provide the value |
||
| 213 | * |
||
| 214 | * @param int $offsetColumnIndex The column index to server as offset |
||
| 215 | * @param int $valueColumnIndex The column index to server as value |
||
| 216 | * @param callable|null $callable A callable to be applied to each of the rows to be returned. |
||
| 217 | * |
||
| 218 | * @return Generator|array |
||
| 219 | */ |
||
| 220 | 14 | public function fetchPairs($offsetColumnIndex = 0, $valueColumnIndex = 1, callable $callable = null) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Return the key/pairs as a PHP generator |
||
| 240 | * |
||
| 241 | * @param Iterator $iterator |
||
| 242 | * |
||
| 243 | * @return Generator |
||
| 244 | */ |
||
| 245 | 12 | protected function fetchPairsGenerator(Iterator $iterator) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Returns a sequential array of all CSV lines; |
||
| 254 | * |
||
| 255 | * The rows are presented as associated arrays |
||
| 256 | * The callable function will be applied to each Iterator item |
||
| 257 | * |
||
| 258 | * @param int|array $offset_or_keys the name for each key member OR the row Index to be |
||
| 259 | * used as the associated named keys |
||
| 260 | * |
||
| 261 | * @param callable $callable A callable to be applied to each of the rows to be returned. |
||
| 262 | * |
||
| 263 | * @throws InvalidArgumentException If the submitted keys are invalid |
||
| 264 | * |
||
| 265 | * @return Iterator|array |
||
| 266 | */ |
||
| 267 | 26 | public function fetchAssoc($offset_or_keys = 0, callable $callable = null) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Selects the array to be used as key for the fetchAssoc method |
||
| 287 | * |
||
| 288 | * @param int|array $offset_or_keys the assoc key OR the row Index to be used |
||
| 289 | * as the key index |
||
| 290 | * |
||
| 291 | * @throws InvalidArgumentException If the row index and/or the resulting array is invalid |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | 26 | protected function getAssocKeys($offset_or_keys) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Validates the array to be used by the fetchAssoc method |
||
| 319 | * |
||
| 320 | * @param array $keys |
||
| 321 | * |
||
| 322 | * @throws InvalidArgumentException If the submitted array fails the assertion |
||
| 323 | */ |
||
| 324 | 22 | protected function assertValidAssocKeys(array $keys) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Returns whether the submitted value can be used as string |
||
| 333 | * |
||
| 334 | * @param mixed $value |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | 20 | protected function isValidKey($value) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Returns a single row from the CSV without filtering |
||
| 345 | * |
||
| 346 | * @param int $offset |
||
| 347 | * |
||
| 348 | * @throws InvalidArgumentException If the $offset is not valid or the row does not exist |
||
| 349 | * |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | 10 | protected function getRow($offset) |
|
| 370 | } |
||
| 371 |