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 |
||
| 51 | class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * header offset. |
||
| 55 | * |
||
| 56 | * @var int|null |
||
| 57 | */ |
||
| 58 | protected $header_offset; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * header record. |
||
| 62 | * |
||
| 63 | * @var string[] |
||
| 64 | */ |
||
| 65 | protected $header = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * records count. |
||
| 69 | * |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | protected $nb_records = -1; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | protected $is_empty_records_skipped = true; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 3 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | 27 | protected function resetProperties() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the header offset. |
||
| 104 | * |
||
| 105 | * If no CSV header offset is set this method MUST return null |
||
| 106 | * |
||
| 107 | * @return int|null |
||
| 108 | */ |
||
| 109 | 15 | public function getHeaderOffset() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Returns the CSV record used as header. |
||
| 116 | * |
||
| 117 | * The returned header is represented as an array of string values |
||
| 118 | * |
||
| 119 | * @return string[] |
||
| 120 | */ |
||
| 121 | 21 | public function getHeader(): array |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Determine the CSV record header. |
||
| 138 | * |
||
| 139 | * @throws Exception If the header offset is set and no record is found or is the empty array |
||
| 140 | * |
||
| 141 | * @return string[] |
||
| 142 | */ |
||
| 143 | 12 | protected function setHeader(int $offset): array |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Returns the row at a given offset. |
||
| 159 | * |
||
| 160 | * @return array|false |
||
| 161 | */ |
||
| 162 | 12 | protected function seekRow(int $offset) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the document as an Iterator. |
||
| 175 | */ |
||
| 176 | 18 | protected function getDocument(): Iterator |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Strip the BOM sequence from a record. |
||
| 193 | * |
||
| 194 | * @param string[] $record |
||
| 195 | * |
||
| 196 | * @return string[] |
||
| 197 | */ |
||
| 198 | 12 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | 9 | public function __call($method, array $arguments) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * {@inheritdoc} |
||
| 229 | */ |
||
| 230 | 3 | public function count(): int |
|
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | 6 | public function getIterator(): Iterator |
|
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | 3 | public function jsonSerialize(): array |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Returns the CSV records as an iterator object. |
||
| 257 | * |
||
| 258 | * Each CSV record is represented as a simple array containing strings or null values. |
||
| 259 | * |
||
| 260 | * If the CSV document has a header record then each record is combined |
||
| 261 | * to the header record and the header record is removed from the iterator. |
||
| 262 | * |
||
| 263 | * If the CSV document is inconsistent. Missing record fields are |
||
| 264 | * filled with null values while extra record fields are strip from |
||
| 265 | * the returned object. |
||
| 266 | * |
||
| 267 | * @param string[] $header an optional header to use instead of the CSV document header |
||
| 268 | */ |
||
| 269 | 33 | public function getRecords(array $header = []): Iterator |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Returns the header to be used for iteration. |
||
| 306 | * |
||
| 307 | * @param string[] $header |
||
| 308 | * |
||
| 309 | * @throws Exception If the header contains non unique column name |
||
| 310 | * |
||
| 311 | * @return string[] |
||
| 312 | */ |
||
| 313 | 27 | protected function computeHeader(array $header) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Combine the CSV header to each record if present. |
||
| 328 | * |
||
| 329 | * @param string[] $header |
||
| 330 | */ |
||
| 331 | 33 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Strip the BOM sequence from the returned records if necessary. |
||
| 351 | */ |
||
| 352 | 27 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Selects the record to be used as the CSV header. |
||
| 372 | * |
||
| 373 | * Because the header is represented as an array, to be valid |
||
| 374 | * a header MUST contain only unique string value. |
||
| 375 | * |
||
| 376 | * @param int|null $offset the header record offset |
||
| 377 | * |
||
| 378 | * @throws Exception if the offset is a negative integer |
||
| 379 | * |
||
| 380 | * @return static |
||
| 381 | */ |
||
| 382 | 24 | public function setHeaderOffset($offset): self |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Enable skipping empty records. |
||
| 404 | */ |
||
| 405 | 12 | public function skipEmptyRecord(): self |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Disable skipping empty records. |
||
| 417 | */ |
||
| 418 | 12 | public function preserveEmptyRecord(): self |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Tells whether empty records are skipped by the instance. |
||
| 430 | */ |
||
| 431 | 12 | public function isEmptyRecordSkipped(): bool |
|
| 435 | } |
||
| 436 |