Complex classes like CsvReader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CsvReader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class CsvReader implements CountableReader, \SeekableIterator |
||
| 13 | { |
||
| 14 | const DUPLICATE_HEADERS_INCREMENT = 1; |
||
| 15 | const DUPLICATE_HEADERS_MERGE = 2; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Number of the row that contains the column names |
||
| 19 | * |
||
| 20 | * @var integer |
||
| 21 | */ |
||
| 22 | protected $headerRowNumber; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * CSV file |
||
| 26 | * |
||
| 27 | * @var \SplFileObject |
||
| 28 | */ |
||
| 29 | protected $file; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Column headers as read from the CSV file |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $columnHeaders = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Number of column headers, stored and re-used for performance |
||
| 40 | * |
||
| 41 | * In case of duplicate headers, this is always the number of unmerged headers. |
||
| 42 | * |
||
| 43 | * @var integer |
||
| 44 | */ |
||
| 45 | protected $headersCount; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Total number of rows in the CSV file |
||
| 49 | * |
||
| 50 | * @var integer |
||
| 51 | */ |
||
| 52 | protected $count; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Faulty CSV rows |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $errors = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Strict parsing - skip any lines mismatching header length |
||
| 63 | * |
||
| 64 | * @var boolean |
||
| 65 | */ |
||
| 66 | protected $strict = true; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * How to handle duplicate headers |
||
| 70 | * |
||
| 71 | * @var integer |
||
| 72 | */ |
||
| 73 | protected $duplicateHeadersFlag; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param \SplFileObject $file |
||
| 77 | * @param string $delimiter |
||
| 78 | * @param string $enclosure |
||
| 79 | * @param string $escape |
||
| 80 | */ |
||
| 81 | 19 | public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\') |
|
| 82 | { |
||
| 83 | 19 | ini_set('auto_detect_line_endings', true); |
|
| 84 | |||
| 85 | 19 | $this->file = $file; |
|
| 86 | 19 | $this->file->setFlags( |
|
| 87 | 19 | \SplFileObject::READ_CSV | |
|
| 88 | 19 | \SplFileObject::SKIP_EMPTY | |
|
| 89 | 19 | \SplFileObject::READ_AHEAD | |
|
| 90 | \SplFileObject::DROP_NEW_LINE |
||
| 91 | 19 | ); |
|
| 92 | 19 | $this->file->setCsvControl( |
|
| 93 | 19 | $delimiter, |
|
| 94 | 19 | $enclosure, |
|
| 95 | $escape |
||
| 96 | 19 | ); |
|
| 97 | 19 | } |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Return the current row as an array |
||
| 101 | * |
||
| 102 | * If a header row has been set, an associative array will be returned |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | */ |
||
| 106 | 11 | public function current() |
|
| 107 | { |
||
| 108 | // If the CSV has no column headers just return the line |
||
| 109 | 11 | if (empty($this->columnHeaders)) { |
|
| 110 | return $this->file->current(); |
||
| 111 | } |
||
| 112 | |||
| 113 | // Since the CSV has column headers use them to construct an associative array for the columns in this line |
||
| 114 | do { |
||
| 115 | 11 | $line = $this->file->current(); |
|
| 116 | |||
| 117 | // In non-strict mode pad/slice the line to match the column headers |
||
| 118 | 11 | if (!$this->isStrict()) { |
|
| 119 | 3 | if ($this->headersCount > count($line)) { |
|
| 120 | 2 | $line = array_pad($line, $this->headersCount, null); // Line too short |
|
| 121 | 2 | } else { |
|
| 122 | 3 | $line = array_slice($line, 0, $this->headersCount); // Line too long |
|
| 123 | } |
||
| 124 | 3 | } |
|
| 125 | |||
| 126 | // See if values for duplicate headers should be merged |
||
| 127 | 11 | if (self::DUPLICATE_HEADERS_MERGE === $this->duplicateHeadersFlag) { |
|
| 128 | 1 | $line = $this->mergeDuplicates($line); |
|
|
|
|||
| 129 | 1 | } |
|
| 130 | |||
| 131 | // Count the number of elements in both: they must be equal. |
||
| 132 | 11 | if (count($this->columnHeaders) === count($line)) { |
|
| 133 | 10 | return array_combine(array_keys($this->columnHeaders), $line); |
|
| 134 | } |
||
| 135 | |||
| 136 | // They are not equal, so log the row as error and skip it. |
||
| 137 | 3 | if ($this->valid()) { |
|
| 138 | 3 | $this->errors[$this->key()] = $line; |
|
| 139 | 3 | $this->next(); |
|
| 140 | 3 | } |
|
| 141 | 3 | } while($this->valid()); |
|
| 142 | |||
| 143 | 2 | return null; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get column headers |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | 4 | public function getColumnHeaders() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Set column headers |
||
| 158 | * |
||
| 159 | * @param array $columnHeaders |
||
| 160 | */ |
||
| 161 | 15 | public function setColumnHeaders(array $columnHeaders) |
|
| 162 | { |
||
| 163 | 15 | $this->columnHeaders = array_count_values($columnHeaders); |
|
| 164 | 15 | $this->headersCount = count($columnHeaders); |
|
| 165 | 15 | } |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Set header row number |
||
| 169 | * |
||
| 170 | * @param integer $rowNumber Number of the row that contains column header names |
||
| 171 | * @param integer $duplicates How to handle duplicates (optional). One of: |
||
| 172 | * - CsvReader::DUPLICATE_HEADERS_INCREMENT; |
||
| 173 | * increments duplicates (dup, dup1, dup2 etc.) |
||
| 174 | * - CsvReader::DUPLICATE_HEADERS_MERGE; merges |
||
| 175 | * values for duplicate headers into an array |
||
| 176 | * (dup => [value1, value2, value3]) |
||
| 177 | * |
||
| 178 | * @throws DuplicateHeadersException If duplicate headers are encountered |
||
| 179 | * and no duplicate handling has been |
||
| 180 | * specified |
||
| 181 | */ |
||
| 182 | 11 | public function setHeaderRowNumber($rowNumber, $duplicates = null) |
|
| 183 | { |
||
| 184 | 11 | $this->duplicateHeadersFlag = $duplicates; |
|
| 185 | 11 | $this->headerRowNumber = $rowNumber; |
|
| 186 | 11 | $headers = $this->readHeaderRow($rowNumber); |
|
| 187 | |||
| 188 | 10 | $this->setColumnHeaders($headers); |
|
| 189 | 10 | } |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Rewind the file pointer |
||
| 193 | * |
||
| 194 | * If a header row has been set, the pointer is set just below the header |
||
| 195 | * row. That way, when you iterate over the rows, that header row is |
||
| 196 | * skipped. |
||
| 197 | */ |
||
| 198 | 17 | public function rewind() |
|
| 199 | { |
||
| 200 | 17 | $this->file->rewind(); |
|
| 201 | 17 | if (null !== $this->headerRowNumber) { |
|
| 202 | 10 | $this->file->seek($this->headerRowNumber + 1); |
|
| 203 | 10 | } |
|
| 204 | 17 | } |
|
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | 9 | public function count() |
|
| 210 | { |
||
| 211 | 9 | if (null === $this->count) { |
|
| 212 | 9 | $position = $this->key(); |
|
| 213 | |||
| 214 | 9 | $this->count = iterator_count($this); |
|
| 215 | |||
| 216 | 9 | $this->seek($position); |
|
| 217 | 9 | } |
|
| 218 | |||
| 219 | 9 | return $this->count; |
|
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | 15 | public function next() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | 15 | public function valid() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | 12 | public function key() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | 10 | public function seek($pointer) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | 1 | public function getFields() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Get a row |
||
| 264 | * |
||
| 265 | * @param integer $number Row number |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | 2 | public function getRow($number) |
|
| 270 | { |
||
| 271 | 2 | $this->seek($number); |
|
| 272 | |||
| 273 | 2 | return $this->current(); |
|
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get rows that have an invalid number of columns |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | 5 | public function getErrors() |
|
| 282 | { |
||
| 283 | 5 | if (0 === $this->key()) { |
|
| 284 | // Iterator has not yet been processed, so do that now |
||
| 285 | 5 | foreach ($this as $row) { /* noop */ } |
|
| 286 | 5 | } |
|
| 287 | |||
| 288 | 5 | return $this->errors; |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Does the reader contain any invalid rows? |
||
| 293 | * |
||
| 294 | * @return boolean |
||
| 295 | */ |
||
| 296 | 5 | public function hasErrors() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Should the reader use strict parsing? |
||
| 303 | * |
||
| 304 | * @return boolean |
||
| 305 | */ |
||
| 306 | 11 | public function isStrict() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Set strict parsing |
||
| 313 | * |
||
| 314 | * @param boolean $strict |
||
| 315 | */ |
||
| 316 | 6 | public function setStrict($strict) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Read header row from CSV file |
||
| 323 | * |
||
| 324 | * @param integer $rowNumber Row number |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | * |
||
| 328 | * @throws DuplicateHeadersException |
||
| 329 | */ |
||
| 330 | 11 | protected function readHeaderRow($rowNumber) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Add an increment to duplicate headers |
||
| 354 | * |
||
| 355 | * So the following line: |
||
| 356 | * |duplicate|duplicate|duplicate| |
||
| 357 | * |first |second |third | |
||
| 358 | * |
||
| 359 | * Yields value: |
||
| 360 | * $duplicate => 'first', $duplicate1 => 'second', $duplicate2 => 'third' |
||
| 361 | * |
||
| 362 | * @param array $headers |
||
| 363 | * |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | 1 | protected function incrementHeaders(array $headers) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Merges values for duplicate headers into an array |
||
| 396 | * |
||
| 397 | * So the following line: |
||
| 398 | * |duplicate|duplicate|duplicate| |
||
| 399 | * |first |second |third | |
||
| 400 | * |
||
| 401 | * Yields value: |
||
| 402 | * $duplicate => ['first', 'second', 'third'] |
||
| 403 | * |
||
| 404 | * @param array $line |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | 1 | protected function mergeDuplicates(array $line) |
|
| 425 | } |
||
| 426 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.