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 |
||
7 | class Stream implements StreamInterface |
||
8 | { |
||
9 | private $readable = [ |
||
10 | 'r', |
||
11 | 'w+', |
||
12 | 'r+', |
||
13 | 'x+', |
||
14 | 'c+', |
||
15 | 'rb', |
||
16 | 'w+b', |
||
17 | 'r+b', |
||
18 | 'x+b', |
||
19 | 'c+b', |
||
20 | 'rt', |
||
21 | 'w+t', |
||
22 | 'r+t', |
||
23 | 'x+t', |
||
24 | 'c+t', |
||
25 | 'a+', |
||
26 | ]; |
||
27 | |||
28 | private $writable = [ |
||
29 | 'w', |
||
30 | 'w+', |
||
31 | 'rw', |
||
32 | 'r+', |
||
33 | 'x+', |
||
34 | 'c+', |
||
35 | 'wb', |
||
36 | 'w+b', |
||
37 | 'r+b', |
||
38 | 'x+b', |
||
39 | 'c+b', |
||
40 | 'w+t', |
||
41 | 'r+t', |
||
42 | 'x+t', |
||
43 | 'c+t', |
||
44 | 'a', |
||
45 | 'a+', |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * @var resource |
||
50 | */ |
||
51 | private $resource; |
||
52 | |||
53 | /** |
||
54 | * @param resource $resource |
||
55 | * @throws \InvalidArgumentException |
||
56 | */ |
||
57 | 174 | public function __construct($resource) |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 6 | public function __toString() |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 2 | public function close() |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 24 | public function detach() |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 4 | public function getSize() |
|
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 12 | public function tell() |
|
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 6 | public function eof() |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 26 | public function isSeekable() |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 22 | public function seek($offset, $whence = SEEK_SET) |
|
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | 14 | public function rewind() |
|
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | 46 | public function isWritable() |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | 6 | public function write($string) |
|
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | 86 | public function isReadable() |
|
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | 6 | public function read($length) |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 28 | public function getContents() |
|
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | 146 | public function getMetadata($key = null) |
|
247 | |||
248 | /** |
||
249 | * @throws \RuntimeException |
||
250 | */ |
||
251 | 6 | private function throwIfUnwritable() |
|
257 | |||
258 | /** |
||
259 | * @throws \RuntimeException |
||
260 | */ |
||
261 | 34 | private function throwIfUnreadable() |
|
267 | |||
268 | /** |
||
269 | * @throws \RuntimeException |
||
270 | */ |
||
271 | 22 | private function throwIfUnSeekable() |
|
277 | } |
||
278 |