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 |
||
41 | class Reader extends AbstractCsv implements TabularDataReader, JsonSerializable |
||
42 | { |
||
43 | /** |
||
44 | * header offset. |
||
45 | * |
||
46 | * @var int|null |
||
47 | */ |
||
48 | protected $header_offset; |
||
49 | |||
50 | /** |
||
51 | * header record. |
||
52 | * |
||
53 | * @var string[] |
||
54 | */ |
||
55 | protected $header = []; |
||
56 | |||
57 | /** |
||
58 | * records count. |
||
59 | * |
||
60 | * @var int |
||
61 | */ |
||
62 | protected $nb_records = -1; |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
68 | |||
69 | /** |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $is_empty_records_included = false; |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 3 | protected function resetProperties(): void |
|
91 | |||
92 | /** |
||
93 | 33 | * Returns the header offset. |
|
94 | * |
||
95 | 33 | * If no CSV header offset is set this method MUST return null |
|
96 | 33 | * |
|
97 | 33 | */ |
|
98 | 33 | public function getHeaderOffset(): ?int |
|
102 | |||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | */ |
||
106 | 21 | public function getHeader(): array |
|
120 | 24 | ||
121 | 18 | /** |
|
122 | * Determine the CSV record header. |
||
123 | * |
||
124 | 9 | * @throws Exception If the header offset is set and no record is found or is the empty array |
|
125 | 3 | * |
|
126 | * @return string[] |
||
127 | */ |
||
128 | 9 | protected function setHeader(int $offset): array |
|
141 | |||
142 | 12 | /** |
|
143 | 12 | * Returns the row at a given offset. |
|
144 | 6 | */ |
|
145 | protected function seekRow(int $offset): array |
||
155 | |||
156 | /** |
||
157 | * Returns the document as an Iterator. |
||
158 | 12 | */ |
|
159 | protected function getDocument(): Iterator |
||
173 | |||
174 | 21 | /** |
|
175 | 4 | * Strip the BOM sequence from a record. |
|
176 | * |
||
177 | 4 | * @param string[] $record |
|
178 | * |
||
179 | * @return string[] |
||
180 | 17 | */ |
|
181 | 17 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
196 | 12 | ||
197 | 3 | /** |
|
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 9 | public function fetchColumn($index = 0): Iterator |
|
206 | |||
207 | 3 | /** |
|
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | public function fetchOne(int $nth_record = 0): array |
||
216 | 9 | ||
217 | 3 | /** |
|
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | 6 | public function fetchPairs($offset_index = 0, $value_index = 1): Iterator |
|
226 | 3 | ||
227 | /** |
||
228 | 3 | * {@inheritdoc} |
|
229 | 3 | */ |
|
230 | public function count(): int |
||
238 | 6 | ||
239 | /** |
||
240 | 6 | * {@inheritdoc} |
|
241 | */ |
||
242 | public function getIterator(): Iterator |
||
246 | 3 | ||
247 | /** |
||
248 | 3 | * {@inheritdoc} |
|
249 | */ |
||
250 | 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 | 36 | * the returned object. |
|
266 | * |
||
267 | 36 | * @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 | 30 | * @throws Exception If the header contains non unique column name |
|
310 | * |
||
311 | 30 | * @return string[] |
|
312 | 27 | */ |
|
313 | protected function computeHeader(array $header) |
||
325 | |||
326 | /** |
||
327 | 36 | * Combine the CSV header to each record if present. |
|
328 | * |
||
329 | 36 | * @param string[] $header |
|
330 | 27 | */ |
|
331 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
||
351 | 30 | ||
352 | /** |
||
353 | 30 | * Strip the BOM sequence from the returned records if necessary. |
|
354 | 21 | */ |
|
355 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
||
372 | |||
373 | /** |
||
374 | * Selects the record to be used as the CSV header. |
||
375 | * |
||
376 | * Because the header is represented as an array, to be valid |
||
377 | * a header MUST contain only unique string value. |
||
378 | * |
||
379 | * @param int|null $offset the header record offset |
||
380 | * |
||
381 | 27 | * @throws Exception if the offset is a negative integer |
|
382 | * |
||
383 | 27 | * @return static |
|
384 | 18 | */ |
|
385 | public function setHeaderOffset(?int $offset): self |
||
400 | 12 | ||
401 | /** |
||
402 | 12 | * Enable skipping empty records. |
|
403 | 12 | */ |
|
404 | 12 | public function skipEmptyRecords(): self |
|
413 | 12 | ||
414 | /** |
||
415 | 12 | * Disable skipping empty records. |
|
416 | 12 | */ |
|
417 | 12 | public function includeEmptyRecords(): self |
|
426 | 12 | ||
427 | /** |
||
428 | 12 | * Tells whether empty records are skipped by the instance. |
|
429 | */ |
||
430 | public function isEmptyRecordsIncluded(): bool |
||
434 | } |
||
435 |