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 |
||
13 | final class Stream implements StreamInterface |
||
14 | { |
||
15 | /** @var resource A resource reference */ |
||
16 | private $stream; |
||
17 | |||
18 | /** @var bool */ |
||
19 | private $seekable; |
||
20 | |||
21 | /** @var bool */ |
||
22 | private $readable; |
||
23 | |||
24 | /** @var bool */ |
||
25 | private $writable; |
||
26 | |||
27 | /** @var array|mixed|null|void */ |
||
28 | private $uri; |
||
29 | |||
30 | /** @var int */ |
||
31 | private $size; |
||
32 | |||
33 | /** @var array Hash of readable and writable stream types */ |
||
34 | private static $readWriteHash = [ |
||
35 | 'read' => [ |
||
36 | 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true, |
||
37 | 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, |
||
38 | 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true, |
||
39 | 'x+t' => true, 'c+t' => true, 'a+' => true, |
||
40 | ], |
||
41 | 'write' => [ |
||
42 | 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, |
||
43 | 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true, |
||
44 | 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true, |
||
45 | 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true, |
||
46 | ], |
||
47 | ]; |
||
48 | |||
49 | 43 | private function __construct() |
|
52 | |||
53 | /** |
||
54 | * @param resource $resource |
||
55 | */ |
||
56 | 43 | public static function createFromResource($resource): self |
|
72 | |||
73 | 15 | public static function create(string $content): self |
|
81 | |||
82 | /** |
||
83 | * Closes the stream when the destructed. |
||
84 | */ |
||
85 | 43 | public function __destruct() |
|
89 | |||
90 | 9 | public function __toString(): string |
|
102 | |||
103 | 43 | public function close(): void |
|
112 | |||
113 | 43 | public function detach() |
|
126 | |||
127 | 18 | public function getSize(): ?int |
|
151 | |||
152 | 3 | public function tell(): int |
|
160 | |||
161 | 8 | public function eof(): bool |
|
165 | |||
166 | 16 | public function isSeekable(): bool |
|
170 | |||
171 | 22 | public function seek($offset, $whence = SEEK_SET): void |
|
179 | |||
180 | 8 | public function rewind(): void |
|
184 | |||
185 | 4 | public function isWritable(): bool |
|
189 | |||
190 | 19 | public function write($string): int |
|
205 | |||
206 | 4 | public function isReadable(): bool |
|
210 | |||
211 | 8 | public function read($length): string |
|
219 | |||
220 | 11 | public function getContents(): string |
|
232 | |||
233 | 43 | public function getMetadata($key = null) |
|
245 | } |
||
246 |