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 |
||
| 10 | class Stream { |
||
| 11 | const STREAM_OPEN_FOR_INCLUDE = 128; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var Interceptor |
||
| 15 | */ |
||
| 16 | private static $defaultInterceptor; |
||
| 17 | |||
| 18 | 4 | public static function hasInterceptor() { |
|
| 21 | |||
| 22 | 20 | public static function setInterceptor(Interceptor $interceptor) { |
|
| 25 | |||
| 26 | 4 | public static function clearInterceptor() { |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @var resource |
||
| 32 | */ |
||
| 33 | public $context; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var resource |
||
| 37 | */ |
||
| 38 | public $resource; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param callable $callback |
||
| 42 | * @return mixed |
||
| 43 | */ |
||
| 44 | 18 | private function runUnwrapped($callback) { |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Determine file which called stream_open() based on backtrace. |
||
| 53 | * |
||
| 54 | * @param array $backtrace |
||
| 55 | * @return string|null |
||
| 56 | */ |
||
| 57 | 1 | private function getCallingFile($backtrace) { |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Check if the path is relative to the file that included it |
||
| 68 | * |
||
| 69 | * @param string $path |
||
| 70 | * @param array $backtrace |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | 10 | private function fixPath($path, $backtrace) { |
|
| 85 | |||
| 86 | 10 | public function stream_open($path, $mode, $options) { |
|
| 87 | 10 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 88 | return $this->runUnwrapped(function (Interceptor $interceptor) use ($path, $mode, $options, $backtrace) { |
||
| 89 | 10 | $path = $this->fixPath($path, $backtrace); |
|
| 90 | |||
| 91 | 10 | $including = (bool)($options & self::STREAM_OPEN_FOR_INCLUDE); |
|
| 92 | 10 | if ($including && $interceptor->shouldIntercept($path)) { |
|
| 93 | 3 | $this->resource = $interceptor->intercept($path); |
|
| 94 | 3 | return true; |
|
| 95 | } |
||
| 96 | 8 | if (isset($this->context)) { |
|
| 97 | 8 | $this->resource = fopen($path, $mode, $options, $this->context); |
|
| 98 | 8 | } else { |
|
| 99 | 1 | $this->resource = fopen($path, $mode, $options); |
|
| 100 | } |
||
| 101 | 8 | return $this->resource !== false; |
|
| 102 | 10 | }); |
|
| 103 | } |
||
| 104 | |||
| 105 | 10 | public function stream_close() { |
|
| 108 | |||
| 109 | 5 | public function stream_eof() { |
|
| 112 | |||
| 113 | 10 | public function stream_flush() { |
|
| 116 | |||
| 117 | 5 | public function stream_read($count) { |
|
| 120 | |||
| 121 | 1 | public function stream_seek($offset, $whence = SEEK_SET) { |
|
| 124 | |||
| 125 | 4 | public function stream_stat() { |
|
| 128 | |||
| 129 | 1 | public function stream_tell() { |
|
| 132 | |||
| 133 | public function url_stat($path, $flags) { |
||
| 146 | |||
| 147 | 1 | public function dir_closedir() { |
|
| 151 | |||
| 152 | 2 | public function dir_opendir($path) { |
|
| 162 | |||
| 163 | 2 | public function dir_readdir() { |
|
| 166 | |||
| 167 | 1 | public function dir_rewinddir() { |
|
| 171 | |||
| 172 | 1 | public function mkdir($path, $mode, $options) { |
|
| 177 | |||
| 178 | 1 | public function rename($pathFrom, $pathTo) { |
|
| 183 | |||
| 184 | 1 | public function rmdir($path) { |
|
| 189 | |||
| 190 | public function stream_cast() { |
||
| 193 | |||
| 194 | 1 | public function stream_lock($operation) { |
|
| 197 | |||
| 198 | 1 | public function stream_set_option($option, $arg1, $arg2) { |
|
| 212 | |||
| 213 | 1 | public function stream_write($data) { |
|
| 216 | |||
| 217 | 1 | public function unlink($path) { |
|
| 222 | |||
| 223 | public function stream_metadata($path, $option, $value) { |
||
| 250 | |||
| 251 | 1 | public function stream_truncate($new_size) { |
|
| 254 | } |
||
| 255 |