Complex classes like Reader 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 Reader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Reader extends AbstractCsv implements Countable, IteratorAggregate |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @inheritdoc |
||
| 41 | */ |
||
| 42 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The value to pad if the record is less than header size. |
||
| 46 | * |
||
| 47 | * @var mixed |
||
| 48 | */ |
||
| 49 | protected $record_padding_value; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * CSV Document header offset |
||
| 53 | * |
||
| 54 | * @var int|null |
||
| 55 | */ |
||
| 56 | protected $header_offset; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * CSV Document Header record |
||
| 60 | * |
||
| 61 | * @var string[] |
||
| 62 | */ |
||
| 63 | protected $header = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Records count |
||
| 67 | * |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | protected $nb_records = -1; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Detect Delimiters occurences in the CSV |
||
| 74 | * |
||
| 75 | * Returns a associative array where each key represents |
||
| 76 | * a valid delimiter and each value the number of occurences |
||
| 77 | * |
||
| 78 | * @param string[] $delimiters the delimiters to consider |
||
| 79 | * @param int $nb_records Detection is made using $nb_records of the CSV |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | 6 | public function fetchDelimitersOccurrence(array $delimiters, int $nb_records = 1): array |
|
| 84 | { |
||
| 85 | $filter = function ($value): bool { |
||
| 86 | 4 | return 1 == strlen($value); |
|
| 87 | 3 | }; |
|
| 88 | |||
| 89 | 6 | $nb_records = $this->filterMinRange($nb_records, 1, __METHOD__.'() expects the number of records to consider to be a valid positive integer, %s given'); |
|
| 90 | 4 | $delimiters = array_unique(array_filter($delimiters, $filter)); |
|
| 91 | $reducer = function (array $res, string $delimiter) use ($nb_records): array { |
||
| 92 | 4 | $res[$delimiter] = $this->getCellCount($delimiter, $nb_records); |
|
| 93 | |||
| 94 | 4 | return $res; |
|
| 95 | 4 | }; |
|
| 96 | |||
| 97 | 4 | $res = array_reduce($delimiters, $reducer, []); |
|
| 98 | 4 | arsort($res, SORT_NUMERIC); |
|
| 99 | |||
| 100 | 4 | return $res; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns the cell count for a specified delimiter |
||
| 105 | * and a specified number of records |
||
| 106 | * |
||
| 107 | * @param string $delimiter CSV delimiter |
||
| 108 | * @param int $nb_records CSV records to consider |
||
| 109 | * |
||
| 110 | * @return int |
||
| 111 | */ |
||
| 112 | 2 | protected function getCellCount(string $delimiter, int $nb_records): int |
|
| 113 | { |
||
| 114 | $filter = function ($record): bool { |
||
| 115 | 2 | return is_array($record) && count($record) > 1; |
|
| 116 | 1 | }; |
|
| 117 | |||
| 118 | 2 | $this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
| 119 | 2 | $this->document->setCsvControl($delimiter, $this->enclosure, $this->escape); |
|
| 120 | 2 | $iterator = new CallbackFilterIterator(new LimitIterator($this->document, 0, $nb_records), $filter); |
|
| 121 | |||
| 122 | 2 | return count(iterator_to_array($iterator, false), COUNT_RECURSIVE); |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns the record padding value |
||
| 127 | * |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | 2 | public function getRecordPaddingValue() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Returns the record offset used as header |
||
| 137 | * |
||
| 138 | * If no CSV record is used this method MUST return null |
||
| 139 | * |
||
| 140 | * @return int|null |
||
| 141 | */ |
||
| 142 | 2 | public function getHeaderOffset() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Returns wether the selected header can be combine to each record |
||
| 149 | * |
||
| 150 | * A valid header must be empty or contains unique string field names |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | 2 | public function supportsHeaderAsRecordKeys(): bool |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the CSV record header |
||
| 163 | * |
||
| 164 | * The returned header is represented as an array of string values |
||
| 165 | * |
||
| 166 | * @return string[] |
||
| 167 | */ |
||
| 168 | 4 | public function getHeader(): array |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Determine the CSV record header |
||
| 183 | * |
||
| 184 | * @param int $offset |
||
| 185 | * |
||
| 186 | * @throws RuntimeException If the header offset is an integer |
||
| 187 | * and the corresponding record is missing |
||
| 188 | * or is an empty array |
||
| 189 | * |
||
| 190 | * @return string[] |
||
| 191 | */ |
||
| 192 | 6 | protected function setHeader(int $offset): array |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Strip the BOM sequence from a record |
||
| 211 | * |
||
| 212 | * @param string[] $record |
||
| 213 | * @param int $bom_length |
||
| 214 | * @param string $enclosure |
||
| 215 | * |
||
| 216 | * @return string[] |
||
| 217 | */ |
||
| 218 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @inheritdoc |
||
| 234 | */ |
||
| 235 | 6 | public function __call($method, array $arguments) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @inheritdoc |
||
| 247 | */ |
||
| 248 | 2 | public function count(): int |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @inheritdoc |
||
| 259 | */ |
||
| 260 | 2 | public function getIterator(): Iterator |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Returns the CSV records in an iterator object. |
||
| 267 | * |
||
| 268 | * Each CSV record is represented as a simple array of string or null values. |
||
| 269 | * |
||
| 270 | * If the CSV document has a header record then each record is combined |
||
| 271 | * to each header record and the header record is removed from the iterator. |
||
| 272 | * |
||
| 273 | * If the CSV document is inconsistent. Missing record fields are |
||
| 274 | * filled with null values while extra record fields are strip from |
||
| 275 | * the returned object. |
||
| 276 | * |
||
| 277 | * |
||
| 278 | * @param array $header |
||
| 279 | * @throws RuntimeException If the header contains non unique column name |
||
| 280 | * @return Iterator |
||
| 281 | 6 | */ |
|
| 282 | public function getRecords(array $header = []): Iterator |
||
| 301 | |||
| 302 | protected function computeHeader(array $header) |
||
| 314 | 8 | ||
| 315 | 8 | /** |
|
| 316 | * Add the CSV header if present and valid |
||
| 317 | 8 | * |
|
| 318 | 4 | * @param Iterator $iterator |
|
| 319 | * @param array $header |
||
| 320 | * |
||
| 321 | 8 | * @return Iterator |
|
| 322 | 8 | */ |
|
| 323 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
||
| 340 | |||
| 341 | 6 | /** |
|
| 342 | 6 | * Strip the BOM sequence if present |
|
| 343 | 6 | * |
|
| 344 | 2 | * @param Iterator $iterator |
|
| 345 | * @param string $bom |
||
| 346 | * |
||
| 347 | 6 | * @return Iterator |
|
| 348 | 6 | */ |
|
| 349 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set the record padding value |
||
| 369 | * |
||
| 370 | * @param mixed $record_padding_value |
||
| 371 | * |
||
| 372 | * @return static |
||
| 373 | */ |
||
| 374 | public function setRecordPaddingValue($record_padding_value): self |
||
| 380 | 2 | ||
| 381 | /** |
||
| 382 | * Selects the record to be used as the CSV header |
||
| 383 | 2 | * |
|
| 384 | 2 | * Because of the header is represented as an array, to be valid |
|
| 385 | 2 | * a header MUST contain only unique string value. |
|
| 386 | * |
||
| 387 | * @param int|null $offset the header record offset |
||
| 388 | 2 | * |
|
| 389 | * @return static |
||
| 390 | */ |
||
| 391 | public function setHeaderOffset($offset): self |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @inheritdoc |
||
| 407 | */ |
||
| 408 | protected function resetProperties() |
||
| 413 | } |
||
| 414 |