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 |
||
| 42 | class Reader extends AbstractCsv implements TabularDataReader, IteratorAggregate, JsonSerializable |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * header offset. |
||
| 46 | * |
||
| 47 | * @var int|null |
||
| 48 | */ |
||
| 49 | protected $header_offset; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * header record. |
||
| 53 | * |
||
| 54 | * @var string[] |
||
| 55 | */ |
||
| 56 | protected $header = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * records count. |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $nb_records = -1; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | protected $is_empty_records_included = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | 3 | */ |
|
| 86 | protected function resetProperties(): void |
||
| 92 | |||
| 93 | 33 | /** |
|
| 94 | * Returns the header offset. |
||
| 95 | 33 | * |
|
| 96 | 33 | * If no CSV header offset is set this method MUST return null |
|
| 97 | 33 | * |
|
| 98 | 33 | */ |
|
| 99 | public function getHeaderOffset(): ?int |
||
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritDoc} |
||
| 106 | 21 | */ |
|
| 107 | public function getHeader(): array |
||
| 121 | 18 | ||
| 122 | /** |
||
| 123 | * Determine the CSV record header. |
||
| 124 | 9 | * |
|
| 125 | 3 | * @throws Exception If the header offset is set and no record is found or is the empty array |
|
| 126 | * |
||
| 127 | * @return string[] |
||
| 128 | 9 | */ |
|
| 129 | protected function setHeader(int $offset): array |
||
| 142 | 12 | ||
| 143 | 12 | /** |
|
| 144 | 6 | * Returns the row at a given offset. |
|
| 145 | */ |
||
| 146 | protected function seekRow(int $offset): array |
||
| 156 | |||
| 157 | /** |
||
| 158 | 12 | * Returns the document as an Iterator. |
|
| 159 | */ |
||
| 160 | 12 | protected function getDocument(): Iterator |
|
| 174 | 21 | ||
| 175 | 4 | /** |
|
| 176 | * Strip the BOM sequence from a record. |
||
| 177 | 4 | * |
|
| 178 | * @param string[] $record |
||
| 179 | * |
||
| 180 | 17 | * @return string[] |
|
| 181 | 17 | */ |
|
| 182 | 17 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
| 197 | 3 | ||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | 9 | */ |
|
| 201 | 9 | public function fetchColumn($index = 0): Iterator |
|
| 207 | 3 | ||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | */ |
||
| 211 | public function fetchOne(int $nth_record = 0): array |
||
| 217 | 3 | ||
| 218 | /** |
||
| 219 | * {@inheritdoc} |
||
| 220 | 6 | */ |
|
| 221 | public function fetchPairs($offset_index = 0, $value_index = 1): Iterator |
||
| 227 | |||
| 228 | 3 | /** |
|
| 229 | 3 | * {@inheritdoc} |
|
| 230 | */ |
||
| 231 | public function count(): int |
||
| 239 | |||
| 240 | 6 | /** |
|
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | public function getIterator(): Iterator |
||
| 247 | |||
| 248 | 3 | /** |
|
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function jsonSerialize(): array |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the CSV records as an iterator object. |
||
| 258 | * |
||
| 259 | * Each CSV record is represented as a simple array containing strings or null values. |
||
| 260 | * |
||
| 261 | * If the CSV document has a header record then each record is combined |
||
| 262 | * to the header record and the header record is removed from the iterator. |
||
| 263 | * |
||
| 264 | * If the CSV document is inconsistent. Missing record fields are |
||
| 265 | 36 | * filled with null values while extra record fields are strip from |
|
| 266 | * the returned object. |
||
| 267 | 36 | * |
|
| 268 | * @param string[] $header an optional header to use instead of the CSV document header |
||
| 269 | 33 | */ |
|
| 270 | 33 | public function getRecords(array $header = []): Iterator |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the header to be used for iteration. |
||
| 307 | * |
||
| 308 | * @param string[] $header |
||
| 309 | 30 | * |
|
| 310 | * @throws Exception If the header contains non unique column name |
||
| 311 | 30 | * |
|
| 312 | 27 | * @return string[] |
|
| 313 | */ |
||
| 314 | protected function computeHeader(array $header) |
||
| 326 | |||
| 327 | 36 | /** |
|
| 328 | * Combine the CSV header to each record if present. |
||
| 329 | 36 | * |
|
| 330 | 27 | * @param string[] $header |
|
| 331 | */ |
||
| 332 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
||
| 352 | |||
| 353 | 30 | /** |
|
| 354 | 21 | * Strip the BOM sequence from the returned records if necessary. |
|
| 355 | */ |
||
| 356 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Selects the record to be used as the CSV header. |
||
| 376 | * |
||
| 377 | * Because the header is represented as an array, to be valid |
||
| 378 | * a header MUST contain only unique string value. |
||
| 379 | * |
||
| 380 | * @param int|null $offset the header record offset |
||
| 381 | 27 | * |
|
| 382 | * @throws Exception if the offset is a negative integer |
||
| 383 | 27 | * |
|
| 384 | 18 | * @return static |
|
| 385 | */ |
||
| 386 | public function setHeaderOffset(?int $offset): self |
||
| 401 | |||
| 402 | 12 | /** |
|
| 403 | 12 | * Enable skipping empty records. |
|
| 404 | 12 | */ |
|
| 405 | public function skipEmptyRecords(): self |
||
| 414 | |||
| 415 | 12 | /** |
|
| 416 | 12 | * Disable skipping empty records. |
|
| 417 | 12 | */ |
|
| 418 | public function includeEmptyRecords(): self |
||
| 427 | |||
| 428 | 12 | /** |
|
| 429 | * Tells whether empty records are skipped by the instance. |
||
| 430 | */ |
||
| 431 | public function isEmptyRecordsIncluded(): bool |
||
| 435 | } |
||
| 436 |