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 |
||
| 53 | class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * header offset. |
||
| 57 | * |
||
| 58 | * @var int|null |
||
| 59 | */ |
||
| 60 | protected $header_offset; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * header record. |
||
| 64 | * |
||
| 65 | * @var string[] |
||
| 66 | */ |
||
| 67 | protected $header = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * records count. |
||
| 71 | * |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | protected $nb_records = -1; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | 3 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | 24 | protected function resetProperties() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the header offset. |
||
| 101 | * |
||
| 102 | * If no CSV header offset is set this method MUST return null |
||
| 103 | * |
||
| 104 | * @return int|null |
||
| 105 | */ |
||
| 106 | 15 | public function getHeaderOffset() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the CSV record used as header. |
||
| 113 | * |
||
| 114 | * The returned header is represented as an array of string values |
||
| 115 | * |
||
| 116 | * @return string[] |
||
| 117 | */ |
||
| 118 | 15 | public function getHeader(): array |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Determine the CSV record header. |
||
| 135 | * |
||
| 136 | * @throws Exception If the header offset is set and no record is found or is the empty array |
||
| 137 | * |
||
| 138 | * @return string[] |
||
| 139 | */ |
||
| 140 | 12 | protected function setHeader(int $offset): array |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the row at a given offset. |
||
| 156 | * |
||
| 157 | * @return array|false |
||
| 158 | */ |
||
| 159 | 6 | protected function seekRow(int $offset) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the document as an Iterator. |
||
| 173 | */ |
||
| 174 | 15 | protected function getDocument(): Iterator |
|
| 175 | { |
||
| 176 | 15 | if ('' === $this->escape && PHP_VERSION_ID < 70400) { |
|
| 177 | 6 | $this->document->setCsvControl($this->delimiter, $this->enclosure); |
|
| 178 | |||
| 179 | 6 | return (new RFC4180Iterator($this->document))->getIterator(); |
|
| 180 | } |
||
| 181 | |||
| 182 | 9 | $this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
| 183 | 9 | $this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
|
| 184 | 9 | $this->document->rewind(); |
|
| 185 | |||
| 186 | 9 | return $this->document; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Strip the BOM sequence from a record. |
||
| 191 | * |
||
| 192 | * @param string[] $record |
||
| 193 | * |
||
| 194 | * @return string[] |
||
| 195 | */ |
||
| 196 | 12 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritdoc} |
||
| 214 | */ |
||
| 215 | 9 | public function __call($method, array $arguments) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | 3 | public function count(): int |
|
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | */ |
||
| 240 | 3 | public function getIterator(): Iterator |
|
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritdoc} |
||
| 247 | */ |
||
| 248 | 3 | public function jsonSerialize(): array |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Returns the CSV records as an iterator object. |
||
| 255 | * |
||
| 256 | * Each CSV record is represented as a simple array containig strings or null values. |
||
| 257 | * |
||
| 258 | * If the CSV document has a header record then each record is combined |
||
| 259 | * to the header record and the header record is removed from the iterator. |
||
| 260 | * |
||
| 261 | * If the CSV document is inconsistent. Missing record fields are |
||
| 262 | * filled with null values while extra record fields are strip from |
||
| 263 | * the returned object. |
||
| 264 | * |
||
| 265 | * @param string[] $header an optional header to use instead of the CSV document header |
||
| 266 | */ |
||
| 267 | 18 | public function getRecords(array $header = []): Iterator |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Returns the header to be used for iteration. |
||
| 288 | * |
||
| 289 | * @param string[] $header |
||
| 290 | * |
||
| 291 | * @throws Exception If the header contains non unique column name |
||
| 292 | * |
||
| 293 | * @return string[] |
||
| 294 | */ |
||
| 295 | 24 | protected function computeHeader(array $header) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Combine the CSV header to each record if present. |
||
| 310 | * |
||
| 311 | * @param string[] $header |
||
| 312 | */ |
||
| 313 | 30 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Strip the BOM sequence from the returned records if necessary. |
||
| 333 | */ |
||
| 334 | 24 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Selects the record to be used as the CSV header. |
||
| 354 | * |
||
| 355 | * Because the header is represented as an array, to be valid |
||
| 356 | * a header MUST contain only unique string value. |
||
| 357 | * |
||
| 358 | * @param int|null $offset the header record offset |
||
| 359 | * |
||
| 360 | * @throws Exception if the offset is a negative integer |
||
| 361 | * |
||
| 362 | * @return static |
||
| 363 | */ |
||
| 364 | 21 | public function setHeaderOffset($offset): self |
|
| 383 | } |
||
| 384 |