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 implements StreamInterface{ |
||
| 21 | |||
| 22 | public const MODES_READ = [ |
||
| 23 | 'a+' => true, |
||
| 24 | 'c+' => true, |
||
| 25 | 'c+b' => true, |
||
| 26 | 'c+t' => true, |
||
| 27 | 'r' => true, |
||
| 28 | 'r+' => true, |
||
| 29 | 'rb' => true, |
||
| 30 | 'rt' => true, |
||
| 31 | 'r+b' => true, |
||
| 32 | 'r+t' => true, |
||
| 33 | 'w+' => true, |
||
| 34 | 'w+b' => true, |
||
| 35 | 'w+t' => true, |
||
| 36 | 'x+' => true, |
||
| 37 | 'x+b' => true, |
||
| 38 | 'x+t' => true, |
||
| 39 | ]; |
||
| 40 | |||
| 41 | public const MODES_WRITE = [ |
||
| 42 | 'a' => true, |
||
| 43 | 'a+' => true, |
||
| 44 | 'c+' => true, |
||
| 45 | 'c+b' => true, |
||
| 46 | 'c+t' => true, |
||
| 47 | 'r+' => true, |
||
| 48 | 'rw' => true, |
||
| 49 | 'r+b' => true, |
||
| 50 | 'r+t' => true, |
||
| 51 | 'w' => true, |
||
| 52 | 'w+' => true, |
||
| 53 | 'wb' => true, |
||
| 54 | 'w+b' => true, |
||
| 55 | 'w+t' => true, |
||
| 56 | 'x+' => true, |
||
| 57 | 'x+b' => true, |
||
| 58 | 'x+t' => true, |
||
| 59 | ]; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var resource |
||
| 63 | */ |
||
| 64 | private $stream; |
||
| 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 string|null |
||
| 83 | */ |
||
| 84 | private $uri; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var int|null |
||
| 88 | */ |
||
| 89 | private $size; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Stream constructor. |
||
| 93 | * |
||
| 94 | * @param resource $stream |
||
| 95 | */ |
||
| 96 | public function __construct($stream){ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Closes the stream when the destructed |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | public function __destruct(){ |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritdoc |
||
| 123 | */ |
||
| 124 | public function __toString(){ |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | public function close():void{ |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @inheritdoc |
||
| 155 | */ |
||
| 156 | public function detach(){ |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @inheritdoc |
||
| 171 | */ |
||
| 172 | public function getSize():?int{ |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @inheritdoc |
||
| 200 | */ |
||
| 201 | public function tell():int{ |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritdoc |
||
| 213 | */ |
||
| 214 | public function eof():bool{ |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @inheritdoc |
||
| 220 | */ |
||
| 221 | public function isSeekable():bool{ |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @inheritdoc |
||
| 227 | */ |
||
| 228 | public function seek($offset, $whence = SEEK_SET):void{ |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @inheritdoc |
||
| 241 | */ |
||
| 242 | public function rewind():void{ |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @inheritdoc |
||
| 248 | */ |
||
| 249 | public function isWritable():bool{ |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @inheritdoc |
||
| 255 | */ |
||
| 256 | public function write($string):int{ |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @inheritdoc |
||
| 275 | */ |
||
| 276 | public function isReadable():bool{ |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @inheritdoc |
||
| 282 | */ |
||
| 283 | public function read($length):string{ |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @inheritdoc |
||
| 307 | */ |
||
| 308 | public function getContents():string{ |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get stream metadata as an associative array or retrieve a specific key. |
||
| 320 | * |
||
| 321 | * The keys returned are identical to the keys returned from PHP's |
||
| 322 | * stream_get_meta_data() function. |
||
| 323 | * |
||
| 324 | * @link http://php.net/manual/en/function.stream-get-meta-data.php |
||
| 325 | * |
||
| 326 | * @param string $key Specific metadata to retrieve. |
||
| 327 | * |
||
| 328 | * @return array|mixed|null Returns an associative array if no key is |
||
| 329 | * provided. Returns a specific key value if a key is provided and the |
||
| 330 | * value is found, or null if the key is not found. |
||
| 331 | */ |
||
| 332 | public function getMetadata($key = null){ |
||
| 345 | |||
| 346 | } |
||
| 347 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: