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 |
||
| 47 | class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * header offset. |
||
| 51 | * |
||
| 52 | * @var int|null |
||
| 53 | */ |
||
| 54 | protected $header_offset; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * header record. |
||
| 58 | * |
||
| 59 | * @var string[] |
||
| 60 | */ |
||
| 61 | protected $header = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * records count. |
||
| 65 | * |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | protected $nb_records = -1; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | protected function resetProperties(): void |
||
| 92 | 24 | ||
| 93 | /** |
||
| 94 | * Returns the header offset. |
||
| 95 | * |
||
| 96 | * If no CSV header offset is set this method MUST return null |
||
| 97 | * |
||
| 98 | */ |
||
| 99 | public function getHeaderOffset(): ?int |
||
| 103 | 15 | ||
| 104 | /** |
||
| 105 | * Returns the CSV record used as header. |
||
| 106 | * |
||
| 107 | * The returned header is represented as an array of string values |
||
| 108 | * |
||
| 109 | * @return string[] |
||
| 110 | */ |
||
| 111 | public function getHeader(): array |
||
| 125 | 6 | ||
| 126 | /** |
||
| 127 | * Determine the CSV record header. |
||
| 128 | * |
||
| 129 | * @throws Exception If the header offset is set and no record is found or is the empty array |
||
| 130 | * |
||
| 131 | * @return string[] |
||
| 132 | */ |
||
| 133 | protected function setHeader(int $offset): array |
||
| 146 | 3 | ||
| 147 | /** |
||
| 148 | * Returns the row at a given offset. |
||
| 149 | * |
||
| 150 | * @return string[]|false |
||
| 151 | */ |
||
| 152 | protected function seekRow(int $offset) |
||
| 162 | 9 | ||
| 163 | /** |
||
| 164 | * Returns the document as an Iterator. |
||
| 165 | */ |
||
| 166 | protected function getDocument(): Iterator |
||
| 180 | 9 | ||
| 181 | /** |
||
| 182 | * Strip the BOM sequence from a record. |
||
| 183 | * |
||
| 184 | * @param string[] $record |
||
| 185 | * |
||
| 186 | * @return string[] |
||
| 187 | */ |
||
| 188 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
||
| 203 | 3 | ||
| 204 | /** |
||
| 205 | * {@inheritdoc} |
||
| 206 | */ |
||
| 207 | public function __call(string $method, array $arguments): iterable |
||
| 225 | 3 | ||
| 226 | /** |
||
| 227 | * {@inheritdoc} |
||
| 228 | 3 | */ |
|
| 229 | public function count(): int |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function getIterator(): Iterator |
||
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | public function jsonSerialize(): array |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns the CSV records as an iterator object. |
||
| 256 | * |
||
| 257 | * Each CSV record is represented as a simple array containing strings or null values. |
||
| 258 | * |
||
| 259 | * If the CSV document has a header record then each record is combined |
||
| 260 | * to the header record and the header record is removed from the iterator. |
||
| 261 | 18 | * |
|
| 262 | * If the CSV document is inconsistent. Missing record fields are |
||
| 263 | 18 | * filled with null values while extra record fields are strip from |
|
| 264 | 15 | * the returned object. |
|
| 265 | 15 | * |
|
| 266 | 15 | * @param string[] $header an optional header to use instead of the CSV document header |
|
| 267 | 15 | */ |
|
| 268 | 15 | public function getRecords(array $header = []): Iterator |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the header to be used for iteration. |
||
| 289 | 24 | * |
|
| 290 | * @param string[] $header |
||
| 291 | 24 | * |
|
| 292 | 21 | * @throws Exception If the header contains non unique column name |
|
| 293 | * |
||
| 294 | * @return string[] |
||
| 295 | 24 | */ |
|
| 296 | 21 | protected function computeHeader(array $header): array |
|
| 308 | |||
| 309 | 30 | /** |
|
| 310 | 21 | * Combine the CSV header to each record if present. |
|
| 311 | * |
||
| 312 | * @param string[] $header |
||
| 313 | 12 | */ |
|
| 314 | 12 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
| 331 | 15 | ||
| 332 | /** |
||
| 333 | * Strip the BOM sequence from the returned records if necessary. |
||
| 334 | 9 | */ |
|
| 335 | 9 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Selects the record to be used as the CSV header. |
||
| 355 | * |
||
| 356 | * Because the header is represented as an array, to be valid |
||
| 357 | * a header MUST contain only unique string value. |
||
| 358 | 24 | * |
|
| 359 | * @param int|null $offset the header record offset |
||
| 360 | 24 | * |
|
| 361 | 12 | * @throws Exception if the offset is a negative integer |
|
| 362 | * |
||
| 363 | * @return static |
||
| 364 | 12 | */ |
|
| 365 | 3 | public function setHeaderOffset(?int $offset): self |
|
| 380 | } |
||
| 381 |