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 pointer |
||
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 | * |
||
103 | * @param resource $stream stream type resource |
||
104 | * @throws InvalidArgumentException if the argument passed is not a seeakable stream resource |
||
105 | */ |
||
106 | 18 | public function __construct($stream) |
|
122 | |||
123 | /** |
||
124 | * close the file pointer |
||
125 | */ |
||
126 | 12 | public function __destruct() |
|
134 | |||
135 | /** |
||
136 | * Return a new instance from a file path |
||
137 | * |
||
138 | * @param string $path file path |
||
139 | * @param string $open_mode the file open mode flag |
||
140 | * |
||
141 | * @throws RuntimeException if the stream resource can not be created |
||
142 | * |
||
143 | * @return static |
||
144 | */ |
||
145 | 6 | public static function createFromPath(string $path, string $open_mode = 'r'): self |
|
156 | |||
157 | /** |
||
158 | * Return a new instance from a string |
||
159 | * |
||
160 | * @param string $content the CSV document as a string |
||
161 | * |
||
162 | * @return static |
||
163 | */ |
||
164 | 4 | public static function createFromString(string $content): self |
|
174 | |||
175 | /** |
||
176 | * Set CSV control |
||
177 | * |
||
178 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
179 | * |
||
180 | * @param string $delimiter |
||
181 | * @param string $enclosure |
||
182 | * @param string $escape |
||
183 | */ |
||
184 | 4 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
190 | |||
191 | /** |
||
192 | * Set StreamIterator Flags |
||
193 | * |
||
194 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
195 | * |
||
196 | * @param int $flags |
||
197 | */ |
||
198 | 12 | public function setFlags(int $flags) |
|
202 | |||
203 | /** |
||
204 | * Write a field array as a CSV line |
||
205 | * |
||
206 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
207 | * |
||
208 | * @param array $fields |
||
209 | * @param string $delimiter |
||
210 | * @param string $enclosure |
||
211 | * @param string $escape |
||
212 | * |
||
213 | * @return int|bool |
||
214 | */ |
||
215 | 6 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
225 | |||
226 | /** |
||
227 | * Get line number |
||
228 | * |
||
229 | * @see http://php.net/manual/en/splfileobject.key.php |
||
230 | * |
||
231 | * @return int |
||
232 | */ |
||
233 | 4 | public function key() |
|
237 | |||
238 | /** |
||
239 | * Read next line |
||
240 | * |
||
241 | * @see http://php.net/manual/en/splfileobject.next.php |
||
242 | * |
||
243 | */ |
||
244 | 4 | public function next() |
|
249 | |||
250 | /** |
||
251 | * Rewind the file to the first line |
||
252 | * |
||
253 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
254 | * |
||
255 | */ |
||
256 | 12 | public function rewind() |
|
265 | |||
266 | /** |
||
267 | * Not at EOF |
||
268 | * |
||
269 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | 10 | public function valid() |
|
281 | |||
282 | /** |
||
283 | * Retrieves the current line of the file. |
||
284 | * |
||
285 | * @see http://php.net/manual/en/splfileobject.current.php |
||
286 | * |
||
287 | * @return mixed |
||
288 | */ |
||
289 | 6 | public function current() |
|
305 | |||
306 | /** |
||
307 | * Retrieves the current line as a CSV Record |
||
308 | * |
||
309 | * @return array|bool |
||
310 | */ |
||
311 | 4 | protected function getCurrentRecord() |
|
319 | |||
320 | /** |
||
321 | * Retrieves the current line as a string |
||
322 | * |
||
323 | * @return string|bool |
||
324 | */ |
||
325 | 12 | protected function getCurrentLine() |
|
333 | |||
334 | /** |
||
335 | * Seek to specified line |
||
336 | * |
||
337 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
338 | * |
||
339 | * @param int $position |
||
340 | */ |
||
341 | 2 | public function seek($position) |
|
353 | |||
354 | /** |
||
355 | * Gets line from file |
||
356 | * |
||
357 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
358 | * |
||
359 | * @return string|bool |
||
360 | */ |
||
361 | 14 | public function fgets() |
|
369 | |||
370 | /** |
||
371 | * Output all remaining data on a file pointer |
||
372 | * |
||
373 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
374 | * |
||
375 | * @return int |
||
376 | */ |
||
377 | 2 | public function fpassthru() |
|
381 | |||
382 | /** |
||
383 | * Read from file |
||
384 | * |
||
385 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
386 | * |
||
387 | * @param int $length The number of bytes to read |
||
388 | * |
||
389 | * @return string|false |
||
390 | */ |
||
391 | 6 | public function fread($length) |
|
395 | |||
396 | /** |
||
397 | * Seek to a position |
||
398 | * |
||
399 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
400 | * |
||
401 | * @param int $offset |
||
402 | * @param int $whence |
||
403 | * |
||
404 | * @return int |
||
405 | */ |
||
406 | 4 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
410 | |||
411 | /** |
||
412 | * Write to stream |
||
413 | * |
||
414 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
415 | * |
||
416 | * @param string $str |
||
417 | * @param int $length |
||
418 | * |
||
419 | * @return int|bool |
||
420 | */ |
||
421 | 2 | public function fwrite(string $str, int $length = 0) |
|
425 | |||
426 | /** |
||
427 | * append a filter |
||
428 | * |
||
429 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
430 | * |
||
431 | * @param string $filter_name |
||
432 | * @param int $read_write |
||
433 | * @param mixed $params |
||
434 | * |
||
435 | * @throws InvalidArgumentException if the filter can not be appended |
||
436 | * |
||
437 | * @return resource |
||
438 | */ |
||
439 | 4 | public function appendFilter(string $filter_name, int $read_write, $params = null) |
|
440 | { |
||
441 | 4 | $res = @stream_filter_append($this->stream, $filter_name, $read_write, $params); |
|
442 | 4 | if (is_resource($res)) { |
|
443 | 4 | return $res; |
|
444 | } |
||
445 | |||
446 | throw new InvalidArgumentException(error_get_last()['message']); |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Removes a registered filter |
||
451 | * |
||
452 | * @see http://php.net/manual/en/function.stream-filter-remove.php |
||
453 | * |
||
454 | * @param resource $resource |
||
455 | */ |
||
456 | 4 | public function removeFilter($resource) |
|
460 | |||
461 | /** |
||
462 | * Flushes the output to a file |
||
463 | * |
||
464 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
465 | * |
||
466 | * @return bool |
||
467 | */ |
||
468 | 6 | public function fflush() |
|
472 | |||
473 | /** |
||
474 | * @inheritdoc |
||
475 | */ |
||
476 | 2 | public function __clone() |
|
480 | } |
||
481 |