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 | class Stream implements StreamInterface |
||
14 | { |
||
15 | /** @var array Hash of readable and writable stream types */ |
||
16 | const READ_WRITE_HASH = [ |
||
17 | 'read' => [ |
||
18 | 'r' => true, |
||
19 | 'w+' => true, |
||
20 | 'r+' => true, |
||
21 | 'x+' => true, |
||
22 | 'c+' => true, |
||
23 | 'rb' => true, |
||
24 | 'w+b' => true, |
||
25 | 'r+b' => true, |
||
26 | 'x+b' => true, |
||
27 | 'c+b' => true, |
||
28 | 'rt' => true, |
||
29 | 'w+t' => true, |
||
30 | 'r+t' => true, |
||
31 | 'x+t' => true, |
||
32 | 'c+t' => true, |
||
33 | 'a+' => true |
||
34 | ], |
||
35 | 'write' => [ |
||
36 | 'w' => true, |
||
37 | 'w+' => true, |
||
38 | 'rw' => true, |
||
39 | 'r+' => true, |
||
40 | 'x+' => true, |
||
41 | 'c+' => true, |
||
42 | 'wb' => true, |
||
43 | 'w+b' => true, |
||
44 | 'r+b' => true, |
||
45 | 'x+b' => true, |
||
46 | 'c+b' => true, |
||
47 | 'w+t' => true, |
||
48 | 'r+t' => true, |
||
49 | 'x+t' => true, |
||
50 | 'c+t' => true, |
||
51 | 'a' => true, |
||
52 | 'a+' => true |
||
53 | ] |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * @var resource |
||
58 | */ |
||
59 | private $stream; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | private $size; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $seekable; |
||
70 | |||
71 | /** |
||
72 | * @var bool |
||
73 | */ |
||
74 | private $readable; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | private $writable; |
||
80 | |||
81 | /** |
||
82 | * @var array|mixed|null |
||
83 | */ |
||
84 | private $uri; |
||
85 | |||
86 | /** |
||
87 | * @var array |
||
88 | */ |
||
89 | private $customMetadata; |
||
90 | |||
91 | /** |
||
92 | * This constructor accepts an associative array of options. |
||
93 | * |
||
94 | * - size: (int) If a read stream would otherwise have an indeterminate |
||
95 | * size, but the size is known due to foreknownledge, then you can |
||
96 | * provide that size, in bytes. |
||
97 | * - metadata: (array) Any additional metadata to return when the metadata |
||
98 | * of the stream is accessed. |
||
99 | * |
||
100 | * @param resource $stream Stream resource to wrap. |
||
101 | * @param array $options Associative array of options. |
||
102 | * |
||
103 | * @throws \InvalidArgumentException if the stream is not a stream resource |
||
104 | */ |
||
105 | 92 | public function __construct($stream, $options = []) |
|
124 | |||
125 | 1 | public function __get($name) |
|
133 | |||
134 | /** |
||
135 | * Closes the stream when the destructed |
||
136 | */ |
||
137 | 68 | public function __destruct() |
|
141 | |||
142 | 8 | public function __toString() : string |
|
151 | |||
152 | 4 | public function getContents() : string |
|
162 | |||
163 | 73 | public function close() |
|
173 | |||
174 | 74 | public function detach() |
|
187 | |||
188 | 26 | public function getSize() |
|
212 | |||
213 | 23 | public function isReadable() : bool |
|
217 | |||
218 | 8 | public function isWritable() : bool |
|
222 | |||
223 | 41 | public function isSeekable() : bool |
|
227 | |||
228 | 33 | public function eof() : bool |
|
232 | |||
233 | 28 | public function tell() : int |
|
243 | |||
244 | 7 | public function rewind() |
|
248 | |||
249 | 44 | public function seek($offset, $whence = SEEK_SET) |
|
258 | |||
259 | 38 | public function read($length) |
|
267 | |||
268 | 18 | public function write($string) |
|
284 | |||
285 | 91 | public function getMetadata($key = null) |
|
299 | } |
||
300 |