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 |
||
14 | class Stream implements StreamInterface |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var stream body / the resource |
||
19 | */ |
||
20 | protected $body; |
||
21 | |||
22 | /** |
||
23 | * @var The stream uri, if any |
||
24 | */ |
||
25 | protected $uri; |
||
26 | |||
27 | /** |
||
28 | * @var boolean Is the stream seekable? |
||
29 | */ |
||
30 | protected $seekable; |
||
31 | |||
32 | /** |
||
33 | * @var boolean Is the stream readable? |
||
34 | */ |
||
35 | protected $readable; |
||
36 | |||
37 | /** |
||
38 | * @var boolean Is the stream writable? |
||
39 | */ |
||
40 | protected $writable; |
||
41 | |||
42 | /** |
||
43 | * @var array The stream metadata, if any |
||
44 | */ |
||
45 | protected $metaData = []; |
||
46 | |||
47 | /** |
||
48 | * @var array The overriding options (size, uri, etc.) |
||
49 | */ |
||
50 | protected $options = []; |
||
51 | |||
52 | /** |
||
53 | * @var string Default output format |
||
54 | */ |
||
55 | protected $defaultFormat; // 'JSON' |
||
56 | |||
57 | /** |
||
58 | * @var array The accepted overriding options |
||
59 | */ |
||
60 | protected $overridingOptions = [ |
||
61 | 'size', |
||
62 | 'uri', |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * @var array Hash of readable and writable stream types |
||
67 | */ |
||
68 | private static $readWriteHash = [ |
||
69 | 'read' => [ |
||
70 | 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true, |
||
71 | 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, |
||
72 | 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true, |
||
73 | 'x+t' => true, 'c+t' => true, 'a+' => true |
||
74 | ], |
||
75 | 'write' => [ |
||
76 | 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, |
||
77 | 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true, |
||
78 | 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true, |
||
79 | 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true |
||
80 | ] |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * Sets up the resource. |
||
85 | * |
||
86 | * @param resource $stream |
||
87 | * @param array $options |
||
88 | */ |
||
89 | public function __construct($stream, $metaData = [], $options = []) |
||
100 | |||
101 | /** |
||
102 | * Sets the stream metadata. |
||
103 | * |
||
104 | * @param resource $stream The stream |
||
105 | * @param array $userData The overriding user metadata |
||
106 | * @return void |
||
107 | */ |
||
108 | protected function setMetadata($stream, array $userData = []) |
||
113 | |||
114 | /** |
||
115 | * Sets the options such as the size, effectively overriding those from the stream. |
||
116 | * |
||
117 | * @param array $options The overriding options |
||
118 | * @return void |
||
119 | */ |
||
120 | protected function setOptions(array $options) |
||
128 | |||
129 | /** |
||
130 | * Reads all data from the stream into a string, from the beginning to end. |
||
131 | * |
||
132 | * This method MUST attempt to seek to the beginning of the stream before |
||
133 | * reading data and read the stream until the end is reached. |
||
134 | * |
||
135 | * Warning: This could attempt to load a large amount of data into memory. |
||
136 | * |
||
137 | * This method MUST NOT raise an exception in order to conform with PHP's |
||
138 | * string casting operations. |
||
139 | * |
||
140 | * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
141 | * @return string |
||
142 | */ |
||
143 | public function __toString() |
||
159 | |||
160 | /** |
||
161 | * Closes the stream and any underlying resources. |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | public function close() |
||
174 | |||
175 | /** |
||
176 | * Separates any underlying resources from the stream. |
||
177 | * |
||
178 | * After the stream has been detached, the stream is in an unusable state. |
||
179 | * |
||
180 | * @return resource|null Underlying PHP stream, if any |
||
181 | */ |
||
182 | public function detach() |
||
197 | |||
198 | /** |
||
199 | * Get the size of the stream if known. |
||
200 | * |
||
201 | * @return int|null Returns the size in bytes if known, or null if unknown. |
||
202 | */ |
||
203 | public function getSize() |
||
220 | |||
221 | /** |
||
222 | * Returns the current position of the file read/write pointer |
||
223 | * |
||
224 | * @return int Position of the file pointer |
||
225 | * @throws \RuntimeException on error. |
||
226 | */ |
||
227 | public function tell() |
||
235 | |||
236 | /** |
||
237 | * Returns true if the stream is at the end of the stream. |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function eof() |
||
246 | |||
247 | /** |
||
248 | * Returns whether or not the stream is seekable. |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function isSeekable() |
||
256 | |||
257 | /** |
||
258 | * Seek to a position in the stream. |
||
259 | * |
||
260 | * @link http://www.php.net/manual/en/function.fseek.php |
||
261 | * @param int $offset Stream offset |
||
262 | * @param int $whence Specifies how the cursor position will be calculated |
||
263 | * based on the seek offset. Valid values are identical to the built-in |
||
264 | * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to |
||
265 | * offset bytes SEEK_CUR: Set position to current location plus offset |
||
266 | * SEEK_END: Set position to end-of-stream plus offset. |
||
267 | * @throws \RuntimeException on failure. |
||
268 | */ |
||
269 | public function seek($offset, $whence = SEEK_SET) |
||
279 | |||
280 | /** |
||
281 | * Seek to the beginning of the stream. |
||
282 | * |
||
283 | * If the stream is not seekable, this method will raise an exception; |
||
284 | * otherwise, it will perform a seek(0). |
||
285 | * |
||
286 | * @see seek() |
||
287 | * @link http://www.php.net/manual/en/function.fseek.php |
||
288 | * @throws \RuntimeException on failure. |
||
289 | */ |
||
290 | public function rewind() |
||
294 | |||
295 | /** |
||
296 | * Returns whether or not the stream is writable. |
||
297 | * |
||
298 | * @return bool |
||
299 | */ |
||
300 | public function isWritable() |
||
304 | |||
305 | /** |
||
306 | * Write data to the stream. |
||
307 | * |
||
308 | * @param string $string The string that is to be written. |
||
309 | * @return int Returns the number of bytes written to the stream. |
||
310 | * @throws \RuntimeException on failure. |
||
311 | */ |
||
312 | public function write($string) |
||
326 | |||
327 | /** |
||
328 | * Returns whether or not the stream is readable. |
||
329 | * |
||
330 | * @return bool |
||
331 | */ |
||
332 | public function isReadable() |
||
336 | |||
337 | /** |
||
338 | * Read data from the stream. |
||
339 | * |
||
340 | * @param int $length Read up to $length bytes from the object and return |
||
341 | * them. Fewer than $length bytes may be returned if underlying stream |
||
342 | * call returns fewer bytes. |
||
343 | * @return string Returns the data read from the stream, or an empty string |
||
344 | * if no bytes are available. |
||
345 | * @throws \RuntimeException if an error occurs. |
||
346 | */ |
||
347 | public function read($length) |
||
355 | |||
356 | /** |
||
357 | * Returns the remaining contents in a string |
||
358 | * |
||
359 | * @return string |
||
360 | * @throws \RuntimeException if unable to read or an error occurs while |
||
361 | * reading. |
||
362 | */ |
||
363 | public function getContents() |
||
371 | |||
372 | /** |
||
373 | * Get stream metadata as an associative array or retrieve a specific key. |
||
374 | * |
||
375 | * The keys returned are identical to the keys returned from PHP's |
||
376 | * stream_get_meta_data() function. |
||
377 | * |
||
378 | * @link http://php.net/manual/en/function.stream-get-meta-data.php |
||
379 | * @param string $key Specific metadata to retrieve. |
||
380 | * @return array|mixed|null Returns an associative array if no key is |
||
381 | * provided. Returns a specific key value if a key is provided and the |
||
382 | * value is found, or null if the key is not found. |
||
383 | */ |
||
384 | public function getMetadata($key = null) |
||
398 | |||
399 | /** |
||
400 | * Gets the value of body. |
||
401 | * |
||
402 | * @return mixed |
||
403 | */ |
||
404 | public function getBody() |
||
412 | |||
413 | /** |
||
414 | * Sets the value of body. |
||
415 | * |
||
416 | * @param mixed $body the body |
||
417 | * |
||
418 | * @return self |
||
419 | */ |
||
420 | protected function setBody($body) |
||
426 | |||
427 | /** |
||
428 | * Is the stream JSONable? |
||
429 | * |
||
430 | * @return boolean |
||
431 | */ |
||
432 | protected function isJsonable() |
||
447 | |||
448 | /** |
||
449 | * Returns an overriding option. |
||
450 | * |
||
451 | * @param string $name The option name |
||
452 | * @return mixed |
||
453 | */ |
||
454 | protected function getOption($name) |
||
471 | |||
472 | /** |
||
473 | * Set all available operations. |
||
474 | * |
||
475 | * @return void |
||
476 | */ |
||
477 | protected function setOperations() |
||
483 | |||
484 | /** |
||
485 | * Unset all available operations. |
||
486 | * |
||
487 | * @return void |
||
488 | */ |
||
489 | protected function unsetOperations() |
||
495 | |||
496 | /** |
||
497 | * Free resources |
||
498 | * |
||
499 | * @return void |
||
500 | */ |
||
501 | public function __destruct() |
||
505 | } |
||
506 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.