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 | 2 | protected $header = []; |
|
| 68 | |||
| 69 | 2 | /** |
|
| 70 | * records count. |
||
| 71 | * |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | protected $nb_records = -1; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | 10 | protected $stream_filter_mode = STREAM_FILTER_READ; |
|
| 80 | |||
| 81 | 10 | /** |
|
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the header offset. |
||
| 91 | 10 | * |
|
| 92 | * If no CSV header offset is set this method MUST return null |
||
| 93 | 10 | * |
|
| 94 | 8 | * @return int|null |
|
| 95 | */ |
||
| 96 | public function getHeaderOffset() |
||
| 100 | |||
| 101 | 4 | /** |
|
| 102 | * Returns the CSV record used as header. |
||
| 103 | 4 | * |
|
| 104 | * The returned header is represented as an array of string values |
||
| 105 | * |
||
| 106 | * @return string[] |
||
| 107 | */ |
||
| 108 | public function getHeader(): array |
||
| 122 | 4 | ||
| 123 | 2 | /** |
|
| 124 | * Determine the CSV record header. |
||
| 125 | * |
||
| 126 | 2 | * @throws Exception If the header offset is set and no record is found or is the empty array |
|
| 127 | * |
||
| 128 | * @return string[] |
||
| 129 | */ |
||
| 130 | protected function setHeader(int $offset): array |
||
| 143 | 4 | ||
| 144 | 1 | /** |
|
| 145 | 1 | * Returns the row at a given offset. |
|
| 146 | */ |
||
| 147 | protected function seekRow(int $offset) |
||
| 166 | |||
| 167 | 8 | /** |
|
| 168 | 2 | * Strip the BOM sequence from a record. |
|
| 169 | * |
||
| 170 | * @param string[] $record |
||
| 171 | 6 | * |
|
| 172 | 6 | * @return string[] |
|
| 173 | 4 | */ |
|
| 174 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
||
| 189 | |||
| 190 | /** |
||
| 191 | 4 | * {@inheritdoc} |
|
| 192 | */ |
||
| 193 | public function __call($method, array $arguments) |
||
| 202 | |||
| 203 | 2 | /** |
|
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | public function count(): int |
||
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | 2 | */ |
|
| 218 | public function getIterator(): Iterator |
||
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | public function jsonSerialize(): array |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the CSV records as an iterator object. |
||
| 233 | * |
||
| 234 | * Each CSV record is represented as a simple array containig strings or null values. |
||
| 235 | * |
||
| 236 | * If the CSV document has a header record then each record is combined |
||
| 237 | * to the header record and the header record is removed from the iterator. |
||
| 238 | 12 | * |
|
| 239 | * If the CSV document is inconsistent. Missing record fields are |
||
| 240 | 12 | * filled with null values while extra record fields are strip from |
|
| 241 | 10 | * the returned object. |
|
| 242 | 10 | * |
|
| 243 | 10 | * @param string[] $header an optional header to use instead of the CSV document header |
|
| 244 | 10 | */ |
|
| 245 | 10 | public function getRecords(array $header = []): Iterator |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Returns the header to be used for iteration. |
||
| 267 | 16 | * |
|
| 268 | * @param string[] $header |
||
| 269 | 16 | * |
|
| 270 | 14 | * @throws Exception If the header contains non unique column name |
|
| 271 | * |
||
| 272 | * @return string[] |
||
| 273 | 16 | */ |
|
| 274 | 14 | protected function computeHeader(array $header) |
|
| 286 | |||
| 287 | /** |
||
| 288 | 20 | * Combine the CSV header to each record if present. |
|
| 289 | * |
||
| 290 | 20 | * @param string[] $header |
|
| 291 | 14 | */ |
|
| 292 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Strip the BOM sequence from the returned records if necessary. |
||
| 312 | */ |
||
| 313 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Selects the record to be used as the CSV header. |
||
| 333 | * |
||
| 334 | * Because the header is represented as an array, to be valid |
||
| 335 | * a header MUST contain only unique string value. |
||
| 336 | * |
||
| 337 | * @param int|null $offset the header record offset |
||
| 338 | * |
||
| 339 | * @throws Exception if the offset is a negative integer |
||
| 340 | * |
||
| 341 | * @return static |
||
| 342 | */ |
||
| 343 | public function setHeaderOffset($offset): self |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | protected function resetProperties() |
||
| 371 | } |
||
| 372 |