Complex classes like StreamIterator 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 StreamIterator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class StreamIterator implements SeekableIterator |
||
33 | { |
||
34 | use ValidatorTrait; |
||
35 | |||
36 | /** |
||
37 | * Attached filters |
||
38 | * |
||
39 | * @var resource[] |
||
40 | */ |
||
41 | protected $filters = []; |
||
42 | |||
43 | /** |
||
44 | * stream resource |
||
45 | * |
||
46 | * @var resource |
||
47 | */ |
||
48 | protected $stream; |
||
49 | |||
50 | /** |
||
51 | * Tell whether the stream should be closed on object destruction |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $should_close_stream = false; |
||
56 | |||
57 | /** |
||
58 | * Current iterator value |
||
59 | * |
||
60 | * @var mixed |
||
61 | */ |
||
62 | protected $value; |
||
63 | |||
64 | /** |
||
65 | * Current iterator key |
||
66 | * |
||
67 | * @var int |
||
68 | */ |
||
69 | protected $offset; |
||
70 | |||
71 | /** |
||
72 | * Flags for the StreamIterator |
||
73 | * |
||
74 | * @var int |
||
75 | */ |
||
76 | protected $flags = 0; |
||
77 | |||
78 | /** |
||
79 | * the field delimiter (one character only) |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $delimiter = ','; |
||
84 | |||
85 | /** |
||
86 | * the field enclosure character (one character only) |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $enclosure = '"'; |
||
91 | |||
92 | /** |
||
93 | * the field escape character (one character only) |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $escape = '\\'; |
||
98 | |||
99 | /** |
||
100 | * New instance |
||
101 | * |
||
102 | * @param resource $resource stream type resource |
||
103 | * |
||
104 | * @throws RuntimeException if the argument passed is not a seeakable stream resource |
||
105 | */ |
||
106 | 34 | public function __construct($resource) |
|
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | public function __destruct() |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | 2 | public function __clone() |
|
148 | |||
149 | /** |
||
150 | * Return a new instance from a file path |
||
151 | * |
||
152 | * @param string $path file path |
||
153 | * @param string $open_mode the file open mode flag |
||
154 | * @param resource|null $context the resource context |
||
155 | * |
||
156 | * @throws RuntimeException if the stream resource can not be created |
||
157 | * |
||
158 | * @return static |
||
159 | */ |
||
160 | 12 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null): self |
|
177 | |||
178 | /** |
||
179 | * Return a new instance from a string |
||
180 | * |
||
181 | * @param string $content the CSV document as a string |
||
182 | * |
||
183 | * @return static |
||
184 | */ |
||
185 | 10 | public static function createFromString(string $content): self |
|
195 | |||
196 | /** |
||
197 | * append a filter |
||
198 | * |
||
199 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
200 | * |
||
201 | * @param string $filter_name |
||
202 | * @param int $read_write |
||
203 | * @param mixed $params |
||
204 | * |
||
205 | * @throws RuntimeException if the filter can not be appended |
||
206 | */ |
||
207 | 10 | public function appendFilter(string $filter_name, int $read_write, $params = null) |
|
217 | |||
218 | /** |
||
219 | * Set CSV control |
||
220 | * |
||
221 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
222 | * |
||
223 | * @param string $delimiter |
||
224 | * @param string $enclosure |
||
225 | * @param string $escape |
||
226 | */ |
||
227 | 2 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
233 | |||
234 | /** |
||
235 | * Set CSV control |
||
236 | * |
||
237 | * @see http://php.net/manual/en/splfileobject.getcsvcontrol.php |
||
238 | * |
||
239 | * @return string[] |
||
240 | */ |
||
241 | 18 | public function getCsvControl() |
|
245 | |||
246 | /** |
||
247 | * Set StreamIterator Flags |
||
248 | * |
||
249 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
250 | * |
||
251 | * @param int $flags |
||
252 | */ |
||
253 | 22 | public function setFlags(int $flags) |
|
257 | |||
258 | /** |
||
259 | * Write a field array as a CSV line |
||
260 | * |
||
261 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
262 | * |
||
263 | * @param array $fields |
||
264 | * @param string $delimiter |
||
265 | * @param string $enclosure |
||
266 | * @param string $escape |
||
267 | * |
||
268 | * @return int|bool |
||
269 | */ |
||
270 | 14 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
280 | |||
281 | /** |
||
282 | * Get line number |
||
283 | * |
||
284 | * @see http://php.net/manual/en/splfileobject.key.php |
||
285 | * |
||
286 | * @return int |
||
287 | */ |
||
288 | 12 | public function key() |
|
292 | |||
293 | /** |
||
294 | * Read next line |
||
295 | * |
||
296 | * @see http://php.net/manual/en/splfileobject.next.php |
||
297 | * |
||
298 | */ |
||
299 | 12 | public function next() |
|
304 | |||
305 | /** |
||
306 | * Rewind the file to the first line |
||
307 | * |
||
308 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
309 | * |
||
310 | */ |
||
311 | 22 | public function rewind() |
|
320 | |||
321 | /** |
||
322 | * Not at EOF |
||
323 | * |
||
324 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | 20 | public function valid() |
|
336 | |||
337 | /** |
||
338 | * Retrieves the current line of the file. |
||
339 | * |
||
340 | * @see http://php.net/manual/en/splfileobject.current.php |
||
341 | * |
||
342 | * @return mixed |
||
343 | */ |
||
344 | 16 | public function current() |
|
360 | |||
361 | /** |
||
362 | * Retrieves the current line as a CSV Record |
||
363 | * |
||
364 | * @return array|bool |
||
365 | */ |
||
366 | 12 | protected function getCurrentRecord() |
|
374 | |||
375 | /** |
||
376 | * Retrieves the current line as a string |
||
377 | * |
||
378 | * @return string|bool |
||
379 | */ |
||
380 | 22 | protected function getCurrentLine() |
|
388 | |||
389 | /** |
||
390 | * Seek to specified line |
||
391 | * |
||
392 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
393 | * |
||
394 | * @param int $position |
||
395 | */ |
||
396 | 4 | public function seek($position) |
|
408 | |||
409 | /** |
||
410 | * Gets line from file |
||
411 | * |
||
412 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
413 | * |
||
414 | * @return string|bool |
||
415 | */ |
||
416 | 26 | public function fgets() |
|
424 | |||
425 | /** |
||
426 | * Output all remaining data on a file pointer |
||
427 | * |
||
428 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
429 | * |
||
430 | * @return int |
||
431 | */ |
||
432 | 2 | public function fpassthru() |
|
436 | |||
437 | /** |
||
438 | * Read from file |
||
439 | * |
||
440 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
441 | * |
||
442 | * @param int $length The number of bytes to read |
||
443 | * |
||
444 | * @return string|false |
||
445 | */ |
||
446 | 8 | public function fread($length) |
|
450 | |||
451 | /** |
||
452 | * Seek to a position |
||
453 | * |
||
454 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
455 | * |
||
456 | * @param int $offset |
||
457 | * @param int $whence |
||
458 | * |
||
459 | * @return int |
||
460 | */ |
||
461 | 4 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
465 | |||
466 | /** |
||
467 | * Write to stream |
||
468 | * |
||
469 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
470 | * |
||
471 | * @param string $str |
||
472 | * @param int $length |
||
473 | * |
||
474 | * @return int|bool |
||
475 | */ |
||
476 | 2 | public function fwrite(string $str, int $length = 0) |
|
480 | |||
481 | /** |
||
482 | * Flushes the output to a file |
||
483 | * |
||
484 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
485 | * |
||
486 | * @return bool |
||
487 | */ |
||
488 | 8 | public function fflush() |
|
492 | } |
||
493 |