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 |
||
| 50 | class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * header offset. |
||
| 54 | * |
||
| 55 | * @var int|null |
||
| 56 | */ |
||
| 57 | protected $header_offset; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * header record. |
||
| 61 | * |
||
| 62 | * @var string[] |
||
| 63 | */ |
||
| 64 | protected $header = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * records count. |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $nb_records = -1; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 3 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | 24 | protected function resetProperties() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Returns the header offset. |
||
| 98 | * |
||
| 99 | * If no CSV header offset is set this method MUST return null |
||
| 100 | * |
||
| 101 | * @return int|null |
||
| 102 | */ |
||
| 103 | 15 | public function getHeaderOffset() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Returns the CSV record used as header. |
||
| 110 | * |
||
| 111 | * The returned header is represented as an array of string values |
||
| 112 | * |
||
| 113 | * @return string[] |
||
| 114 | */ |
||
| 115 | 18 | public function getHeader(): array |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Determine the CSV record header. |
||
| 132 | * |
||
| 133 | * @throws Exception If the header offset is set and no record is found or is the empty array |
||
| 134 | * |
||
| 135 | * @return string[] |
||
| 136 | */ |
||
| 137 | 12 | protected function setHeader(int $offset): array |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Returns the row at a given offset. |
||
| 153 | * |
||
| 154 | * @return array|false |
||
| 155 | */ |
||
| 156 | 9 | protected function seekRow(int $offset) |
|
| 157 | { |
||
| 158 | 9 | foreach ($this->getDocument() as $index => $record) { |
|
| 159 | 9 | if ($offset === $index) { |
|
| 160 | 9 | return [null] === $record ? false : $record; |
|
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | 9 | return false; |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns the document as an Iterator. |
||
| 169 | */ |
||
| 170 | 15 | protected function getDocument(): Iterator |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Strip the BOM sequence from a record. |
||
| 185 | * |
||
| 186 | * @param string[] $record |
||
| 187 | * |
||
| 188 | * @return string[] |
||
| 189 | */ |
||
| 190 | 12 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | 9 | public function __call($method, array $arguments) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | 3 | public function count(): int |
|
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | */ |
||
| 234 | 3 | public function getIterator(): Iterator |
|
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | 3 | public function jsonSerialize(): array |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Returns the CSV records as an iterator object. |
||
| 249 | * |
||
| 250 | * Each CSV record is represented as a simple array containig strings or null values. |
||
| 251 | * |
||
| 252 | * If the CSV document has a header record then each record is combined |
||
| 253 | * to the header record and the header record is removed from the iterator. |
||
| 254 | * |
||
| 255 | * If the CSV document is inconsistent. Missing record fields are |
||
| 256 | * filled with null values while extra record fields are strip from |
||
| 257 | * the returned object. |
||
| 258 | * |
||
| 259 | * @param string[] $header an optional header to use instead of the CSV document header |
||
| 260 | */ |
||
| 261 | 18 | public function getRecords(array $header = []): Iterator |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Returns the header to be used for iteration. |
||
| 282 | * |
||
| 283 | * @param string[] $header |
||
| 284 | * |
||
| 285 | * @throws Exception If the header contains non unique column name |
||
| 286 | * |
||
| 287 | * @return string[] |
||
| 288 | */ |
||
| 289 | 24 | protected function computeHeader(array $header) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Combine the CSV header to each record if present. |
||
| 304 | * |
||
| 305 | * @param string[] $header |
||
| 306 | */ |
||
| 307 | 30 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Strip the BOM sequence from the returned records if necessary. |
||
| 327 | */ |
||
| 328 | 24 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Selects the record to be used as the CSV header. |
||
| 348 | * |
||
| 349 | * Because the header is represented as an array, to be valid |
||
| 350 | * a header MUST contain only unique string value. |
||
| 351 | * |
||
| 352 | * @param int|null $offset the header record offset |
||
| 353 | * |
||
| 354 | * @throws Exception if the offset is a negative integer |
||
| 355 | * |
||
| 356 | * @return static |
||
| 357 | */ |
||
| 358 | 24 | public function setHeaderOffset($offset): self |
|
| 377 | } |
||
| 378 |