Complex classes like Stream 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 Stream, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class Stream implements SeekableIterator |
||
50 | { |
||
51 | /** |
||
52 | * Attached filters. |
||
53 | * |
||
54 | * @var resource[] |
||
55 | */ |
||
56 | protected $filters = []; |
||
57 | |||
58 | /** |
||
59 | * stream resource. |
||
60 | * |
||
61 | * @var resource |
||
62 | */ |
||
63 | protected $stream; |
||
64 | |||
65 | /** |
||
66 | * Tell whether the stream should be closed on object destruction. |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected $should_close_stream = false; |
||
71 | |||
72 | /** |
||
73 | * Current iterator value. |
||
74 | * |
||
75 | * @var mixed |
||
76 | */ |
||
77 | protected $value; |
||
78 | |||
79 | /** |
||
80 | * Current iterator key. |
||
81 | * |
||
82 | * @var int |
||
83 | */ |
||
84 | protected $offset; |
||
85 | |||
86 | /** |
||
87 | * Flags for the Document. |
||
88 | * |
||
89 | * @var int |
||
90 | */ |
||
91 | protected $flags = 0; |
||
92 | |||
93 | /** |
||
94 | * the field delimiter (one character only). |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $delimiter = ','; |
||
99 | |||
100 | /** |
||
101 | * the field enclosure character (one character only). |
||
102 | * |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $enclosure = '"'; |
||
106 | |||
107 | /** |
||
108 | * the field escape character (one character only). |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | protected $escape = '\\'; |
||
113 | |||
114 | /** |
||
115 | * Tell whether the current stream is seekable;. |
||
116 | * |
||
117 | * @var bool |
||
118 | */ |
||
119 | protected $is_seekable = false; |
||
120 | |||
121 | /** |
||
122 | * New instance. |
||
123 | * |
||
124 | * @param resource $resource stream type resource |
||
125 | */ |
||
126 | 51 | public function __construct($resource) |
|
127 | { |
||
128 | 51 | if (!is_resource($resource)) { |
|
129 | 3 | throw new TypeError(sprintf('Argument passed must be a stream resource, %s given', gettype($resource))); |
|
130 | } |
||
131 | |||
132 | 48 | if ('stream' !== ($type = get_resource_type($resource))) { |
|
133 | 3 | throw new TypeError(sprintf('Argument passed must be a stream resource, %s resource given', $type)); |
|
134 | } |
||
135 | |||
136 | 45 | $this->is_seekable = stream_get_meta_data($resource)['seekable']; |
|
137 | 45 | $this->stream = $resource; |
|
138 | 45 | } |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function __destruct() |
||
144 | { |
||
145 | 45 | $walker = static function ($filter): bool { |
|
146 | 12 | return @stream_filter_remove($filter); |
|
147 | 45 | }; |
|
148 | |||
149 | 45 | array_walk_recursive($this->filters, $walker); |
|
150 | |||
151 | 45 | if ($this->should_close_stream && is_resource($this->stream)) { |
|
152 | 30 | fclose($this->stream); |
|
153 | } |
||
154 | |||
155 | 45 | unset($this->stream); |
|
156 | 45 | } |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 3 | public function __clone() |
|
162 | { |
||
163 | 3 | throw new Exception(sprintf('An object of class %s cannot be cloned', static::class)); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | 3 | public function __debugInfo() |
|
170 | { |
||
171 | 3 | return stream_get_meta_data($this->stream) + [ |
|
172 | 3 | 'delimiter' => $this->delimiter, |
|
173 | 3 | 'enclosure' => $this->enclosure, |
|
174 | 3 | 'escape' => $this->escape, |
|
175 | 3 | 'stream_filters' => array_keys($this->filters), |
|
176 | ]; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Return a new instance from a file path. |
||
181 | * |
||
182 | * @param resource|null $context |
||
183 | * |
||
184 | * @throws Exception if the stream resource can not be created |
||
185 | * |
||
186 | * @return static |
||
187 | */ |
||
188 | 21 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null) |
|
189 | { |
||
190 | 21 | $args = [$path, $open_mode]; |
|
191 | 21 | if (null !== $context) { |
|
192 | 3 | $args[] = false; |
|
193 | 3 | $args[] = $context; |
|
194 | } |
||
195 | |||
196 | 21 | if (!$resource = @fopen(...$args)) { |
|
197 | 6 | throw new Exception(sprintf('`%s`: failed to open stream: No such file or directory', $path)); |
|
198 | } |
||
199 | |||
200 | 15 | $instance = new static($resource); |
|
201 | 15 | $instance->should_close_stream = true; |
|
202 | |||
203 | 15 | return $instance; |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Return a new instance from a string. |
||
208 | * |
||
209 | * @return static |
||
210 | */ |
||
211 | 18 | public static function createFromString(string $content = '') |
|
212 | { |
||
213 | 18 | $resource = fopen('php://temp', 'r+'); |
|
214 | 18 | fwrite($resource, $content); |
|
215 | |||
216 | 18 | $instance = new static($resource); |
|
217 | 18 | $instance->should_close_stream = true; |
|
218 | |||
219 | 18 | return $instance; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * Return the URI of the underlying stream. |
||
224 | */ |
||
225 | 12 | public function getPathname(): string |
|
226 | { |
||
227 | 12 | return stream_get_meta_data($this->stream)['uri']; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * append a filter. |
||
232 | * |
||
233 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
234 | * |
||
235 | * @param null|mixed $params |
||
236 | * @throws Exception if the filter can not be appended |
||
237 | */ |
||
238 | 18 | public function appendFilter(string $filtername, int $read_write, $params = null) |
|
248 | |||
249 | /** |
||
250 | * Set CSV control. |
||
251 | * |
||
252 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
253 | */ |
||
254 | 24 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
258 | |||
259 | /** |
||
260 | * Filter Csv control characters. |
||
261 | * |
||
262 | * @throws Exception If the Csv control character is not one character only. |
||
263 | */ |
||
264 | 45 | protected function filterControl(string $delimiter, string $enclosure, string $escape, string $caller): array |
|
265 | { |
||
266 | 45 | if (1 !== strlen($delimiter)) { |
|
267 | 6 | throw new Exception(sprintf('%s() expects delimiter to be a single character', $caller)); |
|
268 | } |
||
269 | |||
270 | 42 | if (1 !== strlen($enclosure)) { |
|
271 | 3 | throw new Exception(sprintf('%s() expects enclosure to be a single character', $caller)); |
|
272 | } |
||
273 | |||
274 | 39 | if (1 === strlen($escape) || ('' === $escape && 70400 <= PHP_VERSION_ID)) { |
|
275 | 33 | return [$delimiter, $enclosure, $escape]; |
|
276 | } |
||
277 | |||
278 | 6 | throw new Exception(sprintf('%s() expects escape to be a single character', $caller)); |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * Set CSV control. |
||
283 | * |
||
284 | * @see http://php.net/manual/en/splfileobject.getcsvcontrol.php |
||
285 | * |
||
286 | * @return string[] |
||
287 | */ |
||
288 | 45 | public function getCsvControl() |
|
292 | |||
293 | /** |
||
294 | * Set CSV stream flags. |
||
295 | * |
||
296 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
297 | */ |
||
298 | 33 | public function setFlags(int $flags) |
|
302 | |||
303 | /** |
||
304 | * Write a field array as a CSV line. |
||
305 | * |
||
306 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
307 | * |
||
308 | * @return int|bool |
||
309 | */ |
||
310 | 21 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
316 | |||
317 | /** |
||
318 | * Get line number. |
||
319 | * |
||
320 | * @see http://php.net/manual/en/splfileobject.key.php |
||
321 | * |
||
322 | * @return int |
||
323 | */ |
||
324 | 21 | public function key() |
|
328 | |||
329 | /** |
||
330 | * Read next line. |
||
331 | * |
||
332 | * @see http://php.net/manual/en/splfileobject.next.php |
||
333 | */ |
||
334 | 18 | public function next() |
|
339 | |||
340 | /** |
||
341 | * Rewind the file to the first line. |
||
342 | * |
||
343 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
344 | * |
||
345 | * @throws Exception if the stream resource is not seekable |
||
346 | */ |
||
347 | 36 | public function rewind() |
|
360 | |||
361 | /** |
||
362 | * Not at EOF. |
||
363 | * |
||
364 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
365 | * |
||
366 | * @return bool |
||
367 | */ |
||
368 | 30 | public function valid() |
|
376 | |||
377 | /** |
||
378 | * Retrieves the current line of the file. |
||
379 | * |
||
380 | * @see http://php.net/manual/en/splfileobject.current.php |
||
381 | */ |
||
382 | 21 | public function current() |
|
392 | |||
393 | /** |
||
394 | * Retrieves the current line as a CSV Record. |
||
395 | * |
||
396 | * @return array|bool |
||
397 | */ |
||
398 | 21 | protected function getCurrentRecord() |
|
406 | |||
407 | /** |
||
408 | * Seek to specified line. |
||
409 | * |
||
410 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
411 | * |
||
412 | * @param int $position |
||
413 | * @throws Exception if the position is negative |
||
414 | */ |
||
415 | 12 | public function seek($position) |
|
433 | |||
434 | /** |
||
435 | * Output all remaining data on a file pointer. |
||
436 | * |
||
437 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
438 | * |
||
439 | * @return int |
||
440 | */ |
||
441 | 3 | public function fpassthru() |
|
445 | |||
446 | /** |
||
447 | * Read from file. |
||
448 | * |
||
449 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
450 | * |
||
451 | * @param int $length The number of bytes to read |
||
452 | * |
||
453 | * @return string|false |
||
454 | */ |
||
455 | 33 | public function fread($length) |
|
459 | |||
460 | /** |
||
461 | * Gets a line from file. |
||
462 | * |
||
463 | * @see http://php.net/manual/en/splfileobject.fgets.php |
||
464 | * |
||
465 | * @return string|false |
||
466 | */ |
||
467 | 6 | public function fgets() |
|
471 | |||
472 | /** |
||
473 | * Seek to a position. |
||
474 | * |
||
475 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
476 | * |
||
477 | * @throws Exception if the stream resource is not seekable |
||
478 | * |
||
479 | * @return int |
||
480 | */ |
||
481 | 18 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
489 | |||
490 | /** |
||
491 | * Write to stream. |
||
492 | * |
||
493 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
494 | * |
||
495 | * @return int|bool |
||
496 | */ |
||
497 | 3 | public function fwrite(string $str, int $length = null) |
|
506 | |||
507 | /** |
||
508 | * Flushes the output to a file. |
||
509 | * |
||
510 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
511 | * |
||
512 | * @return bool |
||
513 | */ |
||
514 | 3 | public function fflush() |
|
518 | } |
||
519 |