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 |
||
12 | class Stream implements StreamInterface |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var stream body / the resource |
||
17 | */ |
||
18 | protected $body; |
||
19 | |||
20 | /** |
||
21 | * @var The stream uri, if any |
||
22 | */ |
||
23 | protected $uri; |
||
24 | |||
25 | /** |
||
26 | * @var Is the stream seekable? |
||
27 | */ |
||
28 | protected $seekable; |
||
29 | |||
30 | /** |
||
31 | * @var Is the stream readable? |
||
32 | */ |
||
33 | protected $readable; |
||
34 | |||
35 | /** |
||
36 | * @var Is the stream writable? |
||
37 | */ |
||
38 | protected $writable; |
||
39 | |||
40 | /** |
||
41 | * @var The stream metadata, if any |
||
42 | */ |
||
43 | protected $metaData = []; |
||
44 | |||
45 | /** |
||
46 | * @var The overriding options (size, uri, etc.) |
||
47 | */ |
||
48 | protected $options = []; |
||
49 | |||
50 | /** |
||
51 | * @var Default output format |
||
52 | */ |
||
53 | protected $defaultFormat; // 'JSON' |
||
54 | |||
55 | /** |
||
56 | * @var The accepted overriding options |
||
57 | */ |
||
58 | protected $overridingOptions = [ |
||
59 | 'size', |
||
60 | 'uri', |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * Sets up the resource. |
||
65 | * |
||
66 | * @param resource $stream |
||
67 | * @param array $options |
||
68 | * @return void |
||
|
|||
69 | */ |
||
70 | public function __construct($stream, $metaData = [], $options = []) { |
||
80 | |||
81 | /** |
||
82 | * Sets the stream metadata. |
||
83 | * |
||
84 | * @param resource $stream The stream |
||
85 | * @param array $userData The overriding user metadata |
||
86 | * @return void |
||
87 | */ |
||
88 | protected function setMetadata($stream, array $userData = []) { |
||
92 | |||
93 | /** |
||
94 | * Sets the options such as the size, effectively overriding those from the stream. |
||
95 | * |
||
96 | * @param array $options The overriding options |
||
97 | * @return void |
||
98 | */ |
||
99 | protected function setOptions(array $options) { |
||
106 | |||
107 | /** |
||
108 | * Reads all data from the stream into a string, from the beginning to end. |
||
109 | * |
||
110 | * This method MUST attempt to seek to the beginning of the stream before |
||
111 | * reading data and read the stream until the end is reached. |
||
112 | * |
||
113 | * Warning: This could attempt to load a large amount of data into memory. |
||
114 | * |
||
115 | * This method MUST NOT raise an exception in order to conform with PHP's |
||
116 | * string casting operations. |
||
117 | * |
||
118 | * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
119 | * @return string |
||
120 | */ |
||
121 | public function __toString() |
||
137 | |||
138 | /** |
||
139 | * Closes the stream and any underlying resources. |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function close() |
||
152 | |||
153 | /** |
||
154 | * Separates any underlying resources from the stream. |
||
155 | * |
||
156 | * After the stream has been detached, the stream is in an unusable state. |
||
157 | * |
||
158 | * @return resource|null Underlying PHP stream, if any |
||
159 | */ |
||
160 | public function detach() |
||
175 | |||
176 | /** |
||
177 | * Get the size of the stream if known. |
||
178 | * |
||
179 | * @return int|null Returns the size in bytes if known, or null if unknown. |
||
180 | */ |
||
181 | public function getSize() |
||
198 | |||
199 | /** |
||
200 | * Returns the current position of the file read/write pointer |
||
201 | * |
||
202 | * @return int Position of the file pointer |
||
203 | * @throws \RuntimeException on error. |
||
204 | */ |
||
205 | public function tell() |
||
213 | |||
214 | /** |
||
215 | * Returns true if the stream is at the end of the stream. |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function eof() |
||
224 | |||
225 | /** |
||
226 | * Returns whether or not the stream is seekable. |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function isSeekable() |
||
234 | |||
235 | /** |
||
236 | * Seek to a position in the stream. |
||
237 | * |
||
238 | * @link http://www.php.net/manual/en/function.fseek.php |
||
239 | * @param int $offset Stream offset |
||
240 | * @param int $whence Specifies how the cursor position will be calculated |
||
241 | * based on the seek offset. Valid values are identical to the built-in |
||
242 | * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to |
||
243 | * offset bytes SEEK_CUR: Set position to current location plus offset |
||
244 | * SEEK_END: Set position to end-of-stream plus offset. |
||
245 | * @throws \RuntimeException on failure. |
||
246 | */ |
||
247 | public function seek($offset, $whence = SEEK_SET) |
||
257 | |||
258 | /** |
||
259 | * Seek to the beginning of the stream. |
||
260 | * |
||
261 | * If the stream is not seekable, this method will raise an exception; |
||
262 | * otherwise, it will perform a seek(0). |
||
263 | * |
||
264 | * @see seek() |
||
265 | * @link http://www.php.net/manual/en/function.fseek.php |
||
266 | * @throws \RuntimeException on failure. |
||
267 | */ |
||
268 | public function rewind() |
||
272 | |||
273 | /** |
||
274 | * Returns whether or not the stream is writable. |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function isWritable() |
||
282 | |||
283 | /** |
||
284 | * Write data to the stream. |
||
285 | * |
||
286 | * @param string $string The string that is to be written. |
||
287 | * @return int Returns the number of bytes written to the stream. |
||
288 | * @throws \RuntimeException on failure. |
||
289 | */ |
||
290 | public function write($string) |
||
303 | |||
304 | /** |
||
305 | * Returns whether or not the stream is readable. |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | public function isReadable() |
||
313 | |||
314 | /** |
||
315 | * Read data from the stream. |
||
316 | * |
||
317 | * @param int $length Read up to $length bytes from the object and return |
||
318 | * them. Fewer than $length bytes may be returned if underlying stream |
||
319 | * call returns fewer bytes. |
||
320 | * @return string Returns the data read from the stream, or an empty string |
||
321 | * if no bytes are available. |
||
322 | * @throws \RuntimeException if an error occurs. |
||
323 | */ |
||
324 | public function read($length) |
||
332 | |||
333 | /** |
||
334 | * Returns the remaining contents in a string |
||
335 | * |
||
336 | * @return string |
||
337 | * @throws \RuntimeException if unable to read or an error occurs while |
||
338 | * reading. |
||
339 | */ |
||
340 | public function getContents() |
||
348 | |||
349 | /** |
||
350 | * Get stream metadata as an associative array or retrieve a specific key. |
||
351 | * |
||
352 | * The keys returned are identical to the keys returned from PHP's |
||
353 | * stream_get_meta_data() function. |
||
354 | * |
||
355 | * @link http://php.net/manual/en/function.stream-get-meta-data.php |
||
356 | * @param string $key Specific metadata to retrieve. |
||
357 | * @return array|mixed|null Returns an associative array if no key is |
||
358 | * provided. Returns a specific key value if a key is provided and the |
||
359 | * value is found, or null if the key is not found. |
||
360 | */ |
||
361 | public function getMetadata($key = null) |
||
375 | |||
376 | /** |
||
377 | * Gets the value of body. |
||
378 | * |
||
379 | * @return mixed |
||
380 | */ |
||
381 | public function getBody() |
||
389 | |||
390 | /** |
||
391 | * Sets the value of body. |
||
392 | * |
||
393 | * @param mixed $body the body |
||
394 | * |
||
395 | * @return self |
||
396 | */ |
||
397 | protected function setBody($body) |
||
403 | |||
404 | /** |
||
405 | * Is the stream JSONable? |
||
406 | * |
||
407 | * @return boolean |
||
408 | */ |
||
409 | protected function isJsonable() { |
||
423 | |||
424 | /** |
||
425 | * Refresh the stream options such as size. |
||
426 | * |
||
427 | * @param type name description |
||
428 | * @return type description |
||
429 | */ |
||
430 | // protected function refresh($which = null) { |
||
431 | // if (isset($which) && null !== $which) { |
||
432 | // $this -> |
||
433 | // } |
||
434 | // } |
||
435 | |||
436 | protected function setOperations() { |
||
442 | |||
443 | protected function unsetOperations() { |
||
448 | |||
449 | |||
450 | |||
451 | /** |
||
452 | * Free resources |
||
453 | * |
||
454 | * @return void |
||
455 | */ |
||
456 | public function __destruct() |
||
460 | } |
||
461 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.