| Total Complexity | 45 | 
| Total Lines | 266 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like FilesystemStreamProxy 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.
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 FilesystemStreamProxy, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 12 | class FilesystemStreamProxy implements StreamWrapperInterface  | 
            ||
| 13 | { | 
            ||
| 14 | public static bool $registered = false;  | 
            ||
| 15 | /**  | 
            ||
| 16 | * @var resource|null  | 
            ||
| 17 | */  | 
            ||
| 18 | public $context;  | 
            ||
| 19 | public StreamWrapperInterface $decorated;  | 
            ||
| 20 | public bool $ignored = false;  | 
            ||
| 21 | |||
| 22 | public static ?FilesystemStreamCollector $collector = null;  | 
            ||
| 23 | public static array $ignoredPathPatterns = [];  | 
            ||
| 24 | public static array $ignoredClasses = [];  | 
            ||
| 25 | public array $operations = [];  | 
            ||
| 26 | |||
| 27 | public function __construct()  | 
            ||
| 28 |     { | 
            ||
| 29 | $this->decorated = new StreamWrapper();  | 
            ||
| 30 | $this->decorated->context = $this->context;  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 31 | }  | 
            ||
| 32 | |||
| 33 | public function __call(string $name, array $arguments)  | 
            ||
| 34 |     { | 
            ||
| 35 |         try { | 
            ||
| 36 | self::unregister();  | 
            ||
| 37 |             return $this->decorated->{$name}(...$arguments); | 
            ||
| 38 |         } finally { | 
            ||
| 39 | self::register();  | 
            ||
| 40 | }  | 
            ||
| 41 | }  | 
            ||
| 42 | |||
| 43 | public function __destruct()  | 
            ||
| 44 |     { | 
            ||
| 45 |         foreach ($this->operations as $name => $operation) { | 
            ||
| 46 | self::$collector->collect(  | 
            ||
| 47 | operation: $name,  | 
            ||
| 48 | path: $operation['path'],  | 
            ||
| 49 | args: $operation['args'],  | 
            ||
| 50 | );  | 
            ||
| 51 | }  | 
            ||
| 52 | }  | 
            ||
| 53 | |||
| 54 | public function __get(string $name)  | 
            ||
| 55 |     { | 
            ||
| 56 |         return $this->decorated->{$name}; | 
            ||
| 57 | }  | 
            ||
| 58 | |||
| 59 | public static function register(): void  | 
            ||
| 60 |     { | 
            ||
| 61 |         if (self::$registered) { | 
            ||
| 62 | return;  | 
            ||
| 63 | }  | 
            ||
| 64 | /**  | 
            ||
| 65 | * It's important to trigger autoloader before unregistering the file stream handler  | 
            ||
| 66 | */  | 
            ||
| 67 | class_exists(StreamWrapper::class);  | 
            ||
| 68 |         stream_wrapper_unregister('file'); | 
            ||
| 69 |         stream_wrapper_register('file', self::class, STREAM_IS_URL); | 
            ||
| 70 | self::$registered = true;  | 
            ||
| 71 | }  | 
            ||
| 72 | |||
| 73 | public static function unregister(): void  | 
            ||
| 74 |     { | 
            ||
| 75 |         if (!self::$registered) { | 
            ||
| 76 | return;  | 
            ||
| 77 | }  | 
            ||
| 78 |         @stream_wrapper_restore('file'); | 
            ||
| 79 | self::$registered = false;  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * TODO: optimise the check. Maybe a hashmap?  | 
            ||
| 84 | */  | 
            ||
| 85 | private function setIgnored(): void  | 
            ||
| 86 |     { | 
            ||
| 87 | $backtrace = debug_backtrace();  | 
            ||
| 88 | /**  | 
            ||
| 89 | * 0 – Called method  | 
            ||
| 90 | * 1 – Proxy  | 
            ||
| 91 | * 2 – Real using place / Composer\ClassLoader include function  | 
            ||
| 92 | * 3 – Whatever / Composer\ClassLoader  | 
            ||
| 93 | */  | 
            ||
| 94 |         if (isset($backtrace[3]['class']) && in_array($backtrace[3]['class'], self::$ignoredClasses, true)) { | 
            ||
| 95 | $this->ignored = true;  | 
            ||
| 96 | return;  | 
            ||
| 97 | }  | 
            ||
| 98 | |||
| 99 |         if (!isset($backtrace[2])) { | 
            ||
| 100 | return;  | 
            ||
| 101 | }  | 
            ||
| 102 | $path = $backtrace[2]['file'];  | 
            ||
| 103 | |||
| 104 | $result = false;  | 
            ||
| 105 |         foreach (self::$ignoredPathPatterns as $ignoredPathPattern) { | 
            ||
| 106 |             if (preg_match($ignoredPathPattern, $path) > 0) { | 
            ||
| 107 | $result = true;  | 
            ||
| 108 | break;  | 
            ||
| 109 | }  | 
            ||
| 110 | }  | 
            ||
| 111 | $this->ignored = $result;  | 
            ||
| 112 | }  | 
            ||
| 113 | |||
| 114 | public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool  | 
            ||
| 115 |     { | 
            ||
| 116 | $this->setIgnored();  | 
            ||
| 117 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 118 | }  | 
            ||
| 119 | |||
| 120 | public function stream_read(int $count): string|false  | 
            ||
| 121 |     { | 
            ||
| 122 |         if (!$this->ignored) { | 
            ||
| 123 | $this->operations['read'] = [  | 
            ||
| 124 | 'path' => $this->decorated->filename,  | 
            ||
| 125 | 'args' => [],  | 
            ||
| 126 | ];  | 
            ||
| 127 | }  | 
            ||
| 128 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 129 | }  | 
            ||
| 130 | |||
| 131 | public function stream_set_option(int $option, int $arg1, ?int $arg2): bool  | 
            ||
| 132 |     { | 
            ||
| 133 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 134 | }  | 
            ||
| 135 | |||
| 136 | public function stream_tell(): int  | 
            ||
| 137 |     { | 
            ||
| 138 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 139 | }  | 
            ||
| 140 | |||
| 141 | public function stream_eof(): bool  | 
            ||
| 142 |     { | 
            ||
| 143 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 144 | }  | 
            ||
| 145 | |||
| 146 | public function stream_seek(int $offset, int $whence = SEEK_SET): bool  | 
            ||
| 147 |     { | 
            ||
| 148 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 149 | }  | 
            ||
| 150 | |||
| 151 | public function stream_cast(int $castAs)  | 
            ||
| 152 |     { | 
            ||
| 153 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 154 | }  | 
            ||
| 155 | |||
| 156 | public function stream_stat(): array|false  | 
            ||
| 157 |     { | 
            ||
| 158 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 159 | }  | 
            ||
| 160 | |||
| 161 | public function dir_closedir(): bool  | 
            ||
| 162 |     { | 
            ||
| 163 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | public function dir_opendir(string $path, int $options): bool  | 
            ||
| 167 |     { | 
            ||
| 168 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 169 | }  | 
            ||
| 170 | |||
| 171 | public function dir_readdir(): false|string  | 
            ||
| 172 |     { | 
            ||
| 173 |         if (!$this->ignored) { | 
            ||
| 174 | $this->operations['readdir'] = [  | 
            ||
| 175 | 'path' => $this->decorated->filename,  | 
            ||
| 176 | 'args' => [],  | 
            ||
| 177 | ];  | 
            ||
| 178 | }  | 
            ||
| 179 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 180 | }  | 
            ||
| 181 | |||
| 182 | public function dir_rewinddir(): bool  | 
            ||
| 183 |     { | 
            ||
| 184 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 185 | }  | 
            ||
| 186 | |||
| 187 | public function mkdir(string $path, int $mode, int $options): bool  | 
            ||
| 188 |     { | 
            ||
| 189 |         if (!$this->ignored) { | 
            ||
| 190 | $this->operations[__FUNCTION__] = [  | 
            ||
| 191 | 'path' => $path,  | 
            ||
| 192 | 'args' => [  | 
            ||
| 193 | 'mode' => $mode,  | 
            ||
| 194 | 'options' => $options,  | 
            ||
| 195 | ],  | 
            ||
| 196 | ];  | 
            ||
| 197 | }  | 
            ||
| 198 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 199 | }  | 
            ||
| 200 | |||
| 201 | public function rename(string $path_from, string $path_to): bool  | 
            ||
| 202 |     { | 
            ||
| 203 |         if (!$this->ignored) { | 
            ||
| 204 | $this->operations[__FUNCTION__] = [  | 
            ||
| 205 | 'path' => $path_from,  | 
            ||
| 206 | 'args' => [  | 
            ||
| 207 | 'path_to' => $path_to,  | 
            ||
| 208 | ],  | 
            ||
| 209 | ];  | 
            ||
| 210 | }  | 
            ||
| 211 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 212 | }  | 
            ||
| 213 | |||
| 214 | public function rmdir(string $path, int $options): bool  | 
            ||
| 215 |     { | 
            ||
| 216 |         if (!$this->ignored) { | 
            ||
| 217 | $this->operations[__FUNCTION__] = [  | 
            ||
| 218 | 'path' => $path,  | 
            ||
| 219 | 'args' => [  | 
            ||
| 220 | 'options' => $options,  | 
            ||
| 221 | ],  | 
            ||
| 222 | ];  | 
            ||
| 223 | }  | 
            ||
| 224 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 225 | }  | 
            ||
| 226 | |||
| 227 | public function stream_close(): void  | 
            ||
| 228 |     { | 
            ||
| 229 | $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 230 | }  | 
            ||
| 231 | |||
| 232 | public function stream_flush(): bool  | 
            ||
| 235 | }  | 
            ||
| 236 | |||
| 237 | public function stream_lock(int $operation): bool  | 
            ||
| 238 |     { | 
            ||
| 239 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 240 | }  | 
            ||
| 241 | |||
| 242 | public function stream_metadata(string $path, int $option, mixed $value): bool  | 
            ||
| 243 |     { | 
            ||
| 244 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 245 | }  | 
            ||
| 246 | |||
| 247 | public function stream_truncate(int $new_size): bool  | 
            ||
| 248 |     { | 
            ||
| 249 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 250 | }  | 
            ||
| 251 | |||
| 252 | public function stream_write(string $data): int  | 
            ||
| 253 |     { | 
            ||
| 254 |         if (!$this->ignored) { | 
            ||
| 255 | $this->operations['write'] = [  | 
            ||
| 256 | 'path' => $this->decorated->filename,  | 
            ||
| 257 | 'args' => [],  | 
            ||
| 258 | ];  | 
            ||
| 259 | }  | 
            ||
| 260 | |||
| 261 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 262 | }  | 
            ||
| 263 | |||
| 264 | public function unlink(string $path): bool  | 
            ||
| 273 | }  | 
            ||
| 274 | |||
| 275 | public function url_stat(string $path, int $flags): array|false  | 
            ||
| 276 |     { | 
            ||
| 277 | return $this->__call(__FUNCTION__, func_get_args());  | 
            ||
| 278 | }  | 
            ||
| 279 | }  | 
            ||
| 280 |