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 Iterator, 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 | * Current iterator value |
||
52 | * |
||
53 | * @var mixed |
||
54 | */ |
||
55 | protected $current_line; |
||
56 | |||
57 | /** |
||
58 | * Current iterator key |
||
59 | * |
||
60 | * @var int |
||
61 | */ |
||
62 | protected $current_line_number; |
||
63 | |||
64 | /** |
||
65 | * Flags for the StreamIterator |
||
66 | * |
||
67 | * @var int |
||
68 | */ |
||
69 | protected $flags = 0; |
||
70 | |||
71 | /** |
||
72 | * the field delimiter (one character only) |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $delimiter = ','; |
||
77 | |||
78 | /** |
||
79 | * the field enclosure character (one character only) |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $enclosure = '"'; |
||
84 | |||
85 | /** |
||
86 | * the field escape character (one character only) |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $escape = '\\'; |
||
91 | |||
92 | /** |
||
93 | * New instance |
||
94 | * |
||
95 | * @param resource $stream stream type resource |
||
96 | */ |
||
97 | 76 | public function __construct($stream) |
|
113 | |||
114 | /** |
||
115 | * close the file pointer |
||
116 | */ |
||
117 | 68 | public function __destruct() |
|
121 | |||
122 | /** |
||
123 | * Set CSV control |
||
124 | * |
||
125 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
126 | * |
||
127 | * @param string $delimiter |
||
128 | * @param string $enclosure |
||
129 | * @param string $escape |
||
130 | */ |
||
131 | 30 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
137 | |||
138 | /** |
||
139 | * Set StreamIterator Flags |
||
140 | * |
||
141 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
142 | * |
||
143 | * @param int $flags |
||
144 | */ |
||
145 | 54 | public function setFlags(int $flags) |
|
149 | |||
150 | /** |
||
151 | * Write a field array as a CSV line |
||
152 | * |
||
153 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
154 | * |
||
155 | * @param array $fields |
||
156 | * @param string $delimiter |
||
157 | * @param string $enclosure |
||
158 | * @param string $escape |
||
159 | * |
||
160 | * @return int|bool |
||
161 | */ |
||
162 | 12 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
172 | |||
173 | /** |
||
174 | * Retrieves the current line of the file. |
||
175 | * |
||
176 | * @see http://php.net/manual/en/splfileobject.current.php |
||
177 | * |
||
178 | * @return mixed |
||
179 | */ |
||
180 | 34 | public function current() |
|
196 | |||
197 | /** |
||
198 | * Retrieves the current line as a CSV Record |
||
199 | * |
||
200 | * @return array|bool |
||
201 | */ |
||
202 | 32 | protected function getCurrentRecord() |
|
210 | |||
211 | /** |
||
212 | * Retrieves the current line as a string |
||
213 | * |
||
214 | * @return string|bool |
||
215 | */ |
||
216 | 50 | protected function getCurrentLine() |
|
224 | |||
225 | /** |
||
226 | * Get line number |
||
227 | * |
||
228 | * @see http://php.net/manual/en/splfileobject.key.php |
||
229 | * |
||
230 | * @return int |
||
231 | */ |
||
232 | 32 | public function key() |
|
236 | |||
237 | /** |
||
238 | * Read next line |
||
239 | * |
||
240 | * @see http://php.net/manual/en/splfileobject.next.php |
||
241 | * |
||
242 | */ |
||
243 | 30 | public function next() |
|
248 | |||
249 | /** |
||
250 | * Rewind the file to the first line |
||
251 | * |
||
252 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
253 | * |
||
254 | */ |
||
255 | 54 | public function rewind() |
|
264 | |||
265 | /** |
||
266 | * Not at EOF |
||
267 | * |
||
268 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | 34 | public function valid() |
|
280 | |||
281 | /** |
||
282 | * Gets line from file |
||
283 | * |
||
284 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
285 | * |
||
286 | * @return string|bool |
||
287 | */ |
||
288 | 50 | public function fgets() |
|
295 | |||
296 | /** |
||
297 | * Output all remaining data on a file pointer |
||
298 | * |
||
299 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
300 | * |
||
301 | * @return int |
||
302 | */ |
||
303 | 18 | public function fpassthru() |
|
307 | |||
308 | /** |
||
309 | * Read from file |
||
310 | * |
||
311 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
312 | * |
||
313 | * @param int $length The number of bytes to read |
||
314 | * |
||
315 | * @return string|false |
||
316 | */ |
||
317 | 2 | public function fread($length) |
|
321 | |||
322 | /** |
||
323 | * Seek to a position |
||
324 | * |
||
325 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
326 | * |
||
327 | * @param int $offset |
||
328 | * @param int $whence |
||
329 | * |
||
330 | * @return int |
||
331 | */ |
||
332 | 6 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
336 | |||
337 | /** |
||
338 | * Seek to specified line |
||
339 | * |
||
340 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
341 | * |
||
342 | * @param int $position |
||
343 | * |
||
344 | * @throws LogicException if the line positon is negative |
||
345 | */ |
||
346 | 20 | public function seek($position) |
|
361 | |||
362 | /** |
||
363 | * Write to stream |
||
364 | * |
||
365 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
366 | * |
||
367 | * @param string $str |
||
368 | * @param int $length |
||
369 | * |
||
370 | * @return int|bool |
||
371 | */ |
||
372 | 2 | public function fwrite(string $str, int $length = 0) |
|
376 | |||
377 | /** |
||
378 | * append a filter |
||
379 | * |
||
380 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
381 | * |
||
382 | * @param string $filter_name |
||
383 | * @param int $read_write |
||
384 | * @param mixed $params |
||
385 | * |
||
386 | * @return resource |
||
387 | */ |
||
388 | 14 | public function appendFilter(string $filter_name, int $read_write, $params = null) |
|
397 | |||
398 | /** |
||
399 | * remove a registered filter |
||
400 | * |
||
401 | * @see http://php.net/manual/en/function.stream-filter-remove.php |
||
402 | * |
||
403 | * @param resource $resource |
||
404 | */ |
||
405 | 14 | public function removeFilter($resource) |
|
409 | |||
410 | /** |
||
411 | * Flushes the output to a file |
||
412 | * |
||
413 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
414 | * |
||
415 | * @return bool |
||
416 | */ |
||
417 | 4 | public function fflush() |
|
421 | |||
422 | /** |
||
423 | * @inheritdoc |
||
424 | */ |
||
425 | 2 | public function __clone() |
|
429 | } |
||
430 |