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 |
||
16 | final class Stream implements StreamInterface |
||
17 | { |
||
18 | /** @var resource|null A resource reference */ |
||
19 | private $stream; |
||
20 | |||
21 | /** @var bool */ |
||
22 | private $seekable; |
||
23 | |||
24 | /** @var bool */ |
||
25 | private $readable; |
||
26 | |||
27 | /** @var bool */ |
||
28 | private $writable; |
||
29 | |||
30 | /** @var array|mixed|void|null */ |
||
31 | private $uri; |
||
32 | |||
33 | /** @var int|null */ |
||
34 | private $size; |
||
35 | |||
36 | /** @var array Hash of readable and writable stream types */ |
||
37 | private const READ_WRITE_HASH = [ |
||
38 | 'read' => [ |
||
39 | 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true, |
||
40 | 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, |
||
41 | 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true, |
||
42 | 'x+t' => true, 'c+t' => true, 'a+' => true, |
||
43 | ], |
||
44 | 'write' => [ |
||
45 | 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, |
||
46 | 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true, |
||
47 | 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true, |
||
48 | 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true, |
||
49 | ], |
||
50 | ]; |
||
51 | |||
52 | 55 | private function __construct() |
|
53 | { |
||
54 | 55 | } |
|
55 | |||
56 | /** |
||
57 | * Creates a new PSR-7 stream. |
||
58 | * |
||
59 | * @param string|resource|StreamInterface $body |
||
60 | * |
||
61 | * @throws \InvalidArgumentException |
||
62 | */ |
||
63 | 55 | public static function create($body = ''): StreamInterface |
|
64 | { |
||
65 | 55 | if ($body instanceof StreamInterface) { |
|
66 | return $body; |
||
67 | } |
||
68 | |||
69 | 55 | if (\is_string($body)) { |
|
70 | 21 | $resource = \fopen('php://temp', 'rw+'); |
|
71 | 21 | \fwrite($resource, $body); |
|
72 | 21 | $body = $resource; |
|
73 | } |
||
74 | |||
75 | 55 | if (\is_resource($body)) { |
|
76 | 55 | $new = new self(); |
|
77 | 55 | $new->stream = $body; |
|
78 | 55 | $meta = \stream_get_meta_data($new->stream); |
|
79 | 55 | $new->seekable = $meta['seekable'] && 0 === \fseek($new->stream, 0, \SEEK_CUR); |
|
80 | 55 | $new->readable = isset(self::READ_WRITE_HASH['read'][$meta['mode']]); |
|
81 | 55 | $new->writable = isset(self::READ_WRITE_HASH['write'][$meta['mode']]); |
|
82 | 55 | $new->uri = $new->getMetadata('uri'); |
|
83 | |||
84 | 55 | return $new; |
|
85 | } |
||
86 | |||
87 | throw new \InvalidArgumentException('First argument to Stream::create() must be a string, resource or StreamInterface.'); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Closes the stream when the destructed. |
||
92 | */ |
||
93 | 55 | public function __destruct() |
|
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | 15 | public function __toString() |
|
126 | |||
127 | 55 | public function close(): void |
|
136 | |||
137 | 55 | public function detach() |
|
150 | |||
151 | 18 | public function getSize(): ?int |
|
175 | |||
176 | 3 | public function tell(): int |
|
184 | |||
185 | 8 | public function eof(): bool |
|
189 | |||
190 | 24 | public function isSeekable(): bool |
|
194 | |||
195 | 28 | public function seek($offset, $whence = \SEEK_SET): void |
|
205 | |||
206 | 8 | public function rewind(): void |
|
210 | |||
211 | 5 | public function isWritable(): bool |
|
215 | |||
216 | 9 | public function write($string): int |
|
231 | |||
232 | 5 | public function isReadable(): bool |
|
236 | |||
237 | 9 | public function read($length): string |
|
245 | |||
246 | 17 | public function getContents(): string |
|
258 | |||
259 | 55 | public function getMetadata($key = null) |
|
273 | } |
||
274 |
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.