| Total Complexity | 42 |
| Total Lines | 247 |
| 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 |
||
| 14 | final class FilesystemStreamProxy implements StreamWrapperInterface |
||
| 15 | { |
||
| 16 | public static bool $registered = false; |
||
| 17 | /** |
||
| 18 | * @var resource|null |
||
| 19 | */ |
||
| 20 | public $context; |
||
| 21 | public StreamWrapper $decorated; |
||
| 22 | public bool $ignored = false; |
||
| 23 | |||
| 24 | public static ?FilesystemStreamCollector $collector = null; |
||
| 25 | public static array $ignoredPathPatterns = []; |
||
| 26 | public static array $ignoredClasses = []; |
||
| 27 | public array $operations = []; |
||
| 28 | |||
| 29 | public function __construct() |
||
| 33 | } |
||
| 34 | |||
| 35 | public function __call(string $name, array $arguments) |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | public function __destruct() |
||
| 46 | { |
||
| 47 | if (self::$collector === null) { |
||
| 48 | return; |
||
| 49 | } |
||
| 50 | foreach ($this->operations as $name => $operation) { |
||
| 51 | self::$collector->collect( |
||
| 52 | operation: $name, |
||
| 53 | path: $operation['path'], |
||
| 54 | args: $operation['args'], |
||
| 55 | ); |
||
| 56 | } |
||
| 57 | self::unregister(); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function __get(string $name) |
||
| 61 | { |
||
| 62 | return $this->decorated->{$name}; |
||
| 63 | } |
||
| 64 | |||
| 65 | public static function register(): void |
||
| 66 | { |
||
| 67 | if (self::$registered) { |
||
| 68 | return; |
||
| 69 | } |
||
| 70 | /** |
||
| 71 | * It's important to trigger autoloader before unregistering the file stream handler |
||
| 72 | */ |
||
| 73 | class_exists(BacktraceIgnoreMatcher::class); |
||
| 74 | class_exists(StreamWrapper::class); |
||
| 75 | class_exists(CombinedRegexp::class); |
||
| 76 | stream_wrapper_unregister('file'); |
||
| 77 | stream_wrapper_register('file', self::class, STREAM_IS_URL); |
||
| 78 | self::$registered = true; |
||
| 79 | } |
||
| 80 | |||
| 81 | public static function unregister(): void |
||
| 82 | { |
||
| 83 | if (!self::$registered) { |
||
| 84 | return; |
||
| 85 | } |
||
| 86 | @stream_wrapper_restore('file'); |
||
|
|
|||
| 87 | self::$registered = false; |
||
| 88 | } |
||
| 89 | |||
| 90 | private function isIgnored(): bool |
||
| 91 | { |
||
| 92 | $backtrace = debug_backtrace(); |
||
| 93 | return BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, self::$ignoredClasses) |
||
| 94 | || BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, self::$ignoredPathPatterns); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool |
||
| 98 | { |
||
| 99 | $this->ignored = $this->isIgnored(); |
||
| 100 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function stream_read(int $count): string|false |
||
| 104 | { |
||
| 105 | if (!$this->ignored) { |
||
| 106 | $this->operations['read'] = [ |
||
| 107 | 'path' => $this->decorated->filename, |
||
| 108 | 'args' => [], |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function stream_set_option(int $option, int $arg1, ?int $arg2): bool |
||
| 115 | { |
||
| 116 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function stream_tell(): int |
||
| 120 | { |
||
| 121 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function stream_eof(): bool |
||
| 125 | { |
||
| 126 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function stream_seek(int $offset, int $whence = SEEK_SET): bool |
||
| 130 | { |
||
| 131 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function stream_cast(int $castAs) |
||
| 135 | { |
||
| 136 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function stream_stat(): array|false |
||
| 140 | { |
||
| 141 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function dir_closedir(): bool |
||
| 145 | { |
||
| 146 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function dir_opendir(string $path, int $options): bool |
||
| 150 | { |
||
| 151 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 152 | } |
||
| 153 | |||
| 154 | public function dir_readdir(): false|string |
||
| 155 | { |
||
| 156 | if (!$this->ignored) { |
||
| 157 | $this->operations['readdir'] = [ |
||
| 158 | 'path' => $this->decorated->filename, |
||
| 159 | 'args' => [], |
||
| 160 | ]; |
||
| 161 | } |
||
| 162 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function dir_rewinddir(): bool |
||
| 166 | { |
||
| 167 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function mkdir(string $path, int $mode, int $options): bool |
||
| 171 | { |
||
| 172 | if (!$this->isIgnored()) { |
||
| 173 | $this->operations['mkdir'] = [ |
||
| 174 | 'path' => $path, |
||
| 175 | 'args' => [ |
||
| 176 | 'mode' => $mode, |
||
| 177 | 'options' => $options, |
||
| 178 | ], |
||
| 179 | ]; |
||
| 180 | } |
||
| 181 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 182 | } |
||
| 183 | |||
| 184 | public function rename(string $path_from, string $path_to): bool |
||
| 185 | { |
||
| 186 | if (!$this->isIgnored()) { |
||
| 187 | $this->operations['rename'] = [ |
||
| 188 | 'path' => $path_from, |
||
| 189 | 'args' => [ |
||
| 190 | 'path_to' => $path_to, |
||
| 191 | ], |
||
| 192 | ]; |
||
| 193 | } |
||
| 194 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 195 | } |
||
| 196 | |||
| 197 | public function rmdir(string $path, int $options): bool |
||
| 198 | { |
||
| 199 | if (!$this->isIgnored()) { |
||
| 200 | $this->operations['rmdir'] = [ |
||
| 201 | 'path' => $path, |
||
| 202 | 'args' => [ |
||
| 203 | 'options' => $options, |
||
| 204 | ], |
||
| 205 | ]; |
||
| 206 | } |
||
| 207 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function stream_close(): void |
||
| 211 | { |
||
| 212 | $this->__call(__FUNCTION__, func_get_args()); |
||
| 213 | } |
||
| 214 | |||
| 215 | public function stream_flush(): bool |
||
| 218 | } |
||
| 219 | |||
| 220 | public function stream_lock(int $operation): bool |
||
| 223 | } |
||
| 224 | |||
| 225 | public function stream_metadata(string $path, int $option, mixed $value): bool |
||
| 226 | { |
||
| 227 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function stream_truncate(int $new_size): bool |
||
| 231 | { |
||
| 232 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function stream_write(string $data): int |
||
| 236 | { |
||
| 237 | if (!$this->ignored) { |
||
| 238 | $this->operations['write'] = [ |
||
| 239 | 'path' => $this->decorated->filename, |
||
| 240 | 'args' => [], |
||
| 241 | ]; |
||
| 242 | } |
||
| 243 | |||
| 244 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 245 | } |
||
| 246 | |||
| 247 | public function unlink(string $path): bool |
||
| 256 | } |
||
| 257 | |||
| 258 | public function url_stat(string $path, int $flags): array|false |
||
| 259 | { |
||
| 260 | return $this->__call(__FUNCTION__, func_get_args()); |
||
| 261 | } |
||
| 262 | } |
||
| 263 |
If you suppress an error, we recommend checking for the error condition explicitly: