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 |
||
20 | final class Stream extends StreamAbstract{ |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | private $seekable; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $readable; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | private $writable; |
||
36 | |||
37 | /** |
||
38 | * @var string|null |
||
39 | */ |
||
40 | private $uri; |
||
41 | |||
42 | /** |
||
43 | * @var int|null |
||
44 | */ |
||
45 | private $size; |
||
46 | |||
47 | /** |
||
48 | * Stream constructor. |
||
49 | * |
||
50 | * @param resource $stream |
||
51 | */ |
||
52 | public function __construct($stream){ |
||
67 | |||
68 | /** |
||
69 | * @inheritdoc |
||
70 | */ |
||
71 | public function __toString(){ |
||
93 | |||
94 | /** |
||
95 | * @inheritdoc |
||
96 | */ |
||
97 | public function close():void{ |
||
105 | |||
106 | /** |
||
107 | * @inheritdoc |
||
108 | */ |
||
109 | public function detach(){ |
||
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | public function getSize():?int{ |
||
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | public function tell():int{ |
||
163 | |||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | public function eof():bool{ |
||
170 | |||
171 | /** |
||
172 | * @inheritdoc |
||
173 | */ |
||
174 | public function isSeekable():bool{ |
||
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | public function seek($offset, $whence = SEEK_SET):void{ |
||
191 | |||
192 | /** |
||
193 | * @inheritdoc |
||
194 | */ |
||
195 | public function rewind():void{ |
||
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | public function isWritable():bool{ |
||
205 | |||
206 | /** |
||
207 | * @inheritdoc |
||
208 | */ |
||
209 | public function write($string):int{ |
||
225 | |||
226 | /** |
||
227 | * @inheritdoc |
||
228 | */ |
||
229 | public function isReadable():bool{ |
||
232 | |||
233 | /** |
||
234 | * @inheritdoc |
||
235 | */ |
||
236 | public function read($length):string{ |
||
257 | |||
258 | /** |
||
259 | * @inheritdoc |
||
260 | */ |
||
261 | public function getContents():string{ |
||
270 | |||
271 | /** |
||
272 | * @inheritdoc |
||
273 | */ |
||
274 | public function getMetadata($key = null){ |
||
287 | |||
288 | } |
||
289 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: