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