Complex classes like ResourceStream 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 ResourceStream, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class ResourceStream implements StreamInterface |
||
| 10 | { |
||
| 11 | /** @var resource */ |
||
| 12 | protected $resource; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param resource $resource |
||
| 16 | */ |
||
| 17 | 27 | public function __construct($resource) |
|
| 24 | |||
| 25 | /** |
||
| 26 | * {@inheritdoc} |
||
| 27 | */ |
||
| 28 | 15 | public function __toString() |
|
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | 2 | public function close() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | 2 | public function detach() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 4 | public function getSize() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | 2 | public function tell() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 4 | public function eof() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | 18 | public function isSeekable() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | 15 | public function seek($offset, $whence = SEEK_SET) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | 13 | public function rewind() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | 10 | public function isWritable() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | 9 | public function write($string) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | 19 | public function isReadable() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | 3 | public function read($length) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | 15 | public function getContents() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | 4 | public function getMetadata($key = null) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @throws \RuntimeException |
||
| 213 | */ |
||
| 214 | 18 | protected function isAvailableAndException() |
|
| 220 | } |
||
| 221 |