| Total Complexity | 47 |
| Total Lines | 476 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Box_Extract 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 Box_Extract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | final class Box_Extract |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var string The open-ended stub pattern |
||
| 38 | */ |
||
| 39 | private const PATTERN_OPEN = BOX_EXTRACT_PATTERN_OPEN; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int The gzip compression flag |
||
| 43 | */ |
||
| 44 | private const GZ = 0x1000; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int The bzip2 compression flag |
||
| 48 | */ |
||
| 49 | private const BZ2 = 0x2000; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | private const MASK = 0x3000; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string The PHAR file path to extract |
||
| 58 | */ |
||
| 59 | private $file; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var resource The open file handle |
||
| 63 | */ |
||
| 64 | private $handle; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int The length of the stub in the PHAR |
||
| 68 | */ |
||
| 69 | private $stub; |
||
| 70 | |||
| 71 | public function __construct(string $file, int $stubLength) |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Finds the phar's stub length using the end pattern. |
||
| 81 | * |
||
| 82 | * A "pattern" is a sequence of characters that indicate the end of a |
||
| 83 | * stub, and the beginning of a manifest. This determines the complete |
||
| 84 | * size of a stub, and is used as an offset to begin parsing the data |
||
| 85 | * contained in the phar's manifest. |
||
| 86 | * |
||
| 87 | * The stub generated included with the Box library uses what I like |
||
| 88 | * to call an open-ended pattern. This pattern uses the function |
||
| 89 | * "__HALT_COMPILER();" at the end, with no following whitespace or |
||
| 90 | * closing PHP tag. By default, this method will use that pattern, |
||
| 91 | * defined as `Extract::PATTERN_OPEN`. |
||
| 92 | * |
||
| 93 | * The Phar class generates its own default stub. The pattern for the |
||
| 94 | * default stub is slightly different than the one used by Box. This |
||
| 95 | * pattern is defined as `Extract::PATTERN_DEFAULT`. |
||
| 96 | * |
||
| 97 | * If you have used your own custom stub, you will need to specify its |
||
| 98 | * pattern as the `$pattern` argument, if you cannot use either of the |
||
| 99 | * pattern constants defined. |
||
| 100 | * |
||
| 101 | * @param string $file The PHAR file path |
||
| 102 | * @param string $pattern The stub end pattern |
||
| 103 | * |
||
| 104 | * @return int The stub length |
||
| 105 | */ |
||
| 106 | public static function findStubLength( |
||
| 107 | string $file, |
||
| 108 | string $pattern = self::PATTERN_OPEN |
||
| 109 | ): int { |
||
| 110 | Assertion::file($file); |
||
| 111 | Assertion::readable($file); |
||
| 112 | |||
| 113 | $fp = fopen($file, 'rb'); |
||
| 114 | |||
| 115 | $stub = null; |
||
| 116 | $offset = 0; |
||
| 117 | $combo = str_split($pattern); |
||
| 118 | |||
| 119 | while (!feof($fp)) { |
||
|
|
|||
| 120 | if (fgetc($fp) === $combo[$offset]) { |
||
| 121 | ++$offset; |
||
| 122 | |||
| 123 | if (!isset($combo[$offset])) { |
||
| 124 | $stub = ftell($fp); |
||
| 125 | |||
| 126 | break; |
||
| 127 | } |
||
| 128 | } else { |
||
| 129 | $offset = 0; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | fclose($fp); |
||
| 134 | |||
| 135 | if (null === $stub) { |
||
| 136 | throw new InvalidArgumentException( |
||
| 137 | sprintf( |
||
| 138 | 'The pattern could not be found in "%s".', |
||
| 139 | $file |
||
| 140 | ) |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | return $stub; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Extracts the PHAR to the directory path. |
||
| 149 | * |
||
| 150 | * If no directory path is given, a temporary one will be generated and |
||
| 151 | * returned. If a directory path is given, the returned directory path |
||
| 152 | * will be the same. |
||
| 153 | * |
||
| 154 | * @param string $dir The directory to extract to |
||
| 155 | * |
||
| 156 | * @return string The directory extracted to |
||
| 157 | */ |
||
| 158 | public function go(string $dir = null): string |
||
| 159 | { |
||
| 160 | // Set up the output directory |
||
| 161 | if (null === $dir) { |
||
| 162 | $dir = rtrim(sys_get_temp_dir(), '\\/') |
||
| 163 | .DIRECTORY_SEPARATOR |
||
| 164 | .'pharextract' |
||
| 165 | .DIRECTORY_SEPARATOR |
||
| 166 | .basename($this->file, '.phar'); |
||
| 167 | } else { |
||
| 168 | $dir = realpath($dir); |
||
| 169 | } |
||
| 170 | |||
| 171 | // Skip if already extracted |
||
| 172 | $md5 = $dir.DIRECTORY_SEPARATOR.md5_file($this->file); |
||
| 173 | |||
| 174 | if (file_exists($md5)) { |
||
| 175 | return $dir; |
||
| 176 | } |
||
| 177 | |||
| 178 | if (!is_dir($dir)) { |
||
| 179 | $this->createDir($dir); |
||
| 180 | } |
||
| 181 | |||
| 182 | // Open the file and skip stub |
||
| 183 | $this->open(); |
||
| 184 | |||
| 185 | if (-1 === fseek($this->handle, $this->stub)) { |
||
| 186 | throw new RuntimeException( |
||
| 187 | sprintf( |
||
| 188 | 'Could not seek to %d in the file "%s".', |
||
| 189 | $this->stub, |
||
| 190 | $this->file |
||
| 191 | ) |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | |||
| 195 | // Read the manifest |
||
| 196 | $info = $this->readManifest(); |
||
| 197 | |||
| 198 | if ($info['flags'] & self::GZ) { |
||
| 199 | if (!function_exists('gzinflate')) { |
||
| 200 | throw new RuntimeException( |
||
| 201 | 'The zlib extension is (gzinflate()) is required for "%s.', |
||
| 202 | $this->file |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | if ($info['flags'] & self::BZ2) { |
||
| 208 | if (!function_exists('bzdecompress')) { |
||
| 209 | throw new RuntimeException( |
||
| 210 | 'The bzip2 extension (bzdecompress()) is required for "%s".', |
||
| 211 | $this->file |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | self::purge($dir); |
||
| 217 | |||
| 218 | $this->createDir($dir); |
||
| 219 | $this->createFile($md5); |
||
| 220 | |||
| 221 | foreach ($info['files'] as $info) { |
||
| 222 | $path = $dir.DIRECTORY_SEPARATOR.$info['path']; |
||
| 223 | $parent = dirname($path); |
||
| 224 | |||
| 225 | if (!is_dir($parent)) { |
||
| 226 | $this->createDir($parent); |
||
| 227 | } |
||
| 228 | |||
| 229 | if (preg_match('{/$}', $info['path'])) { |
||
| 230 | $this->createDir($path, 0777, false); |
||
| 231 | } else { |
||
| 232 | $this->createFile( |
||
| 233 | $path, |
||
| 234 | $this->extractFile($info) |
||
| 235 | ); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | return $dir; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Recursively deletes the directory or file path. |
||
| 244 | * |
||
| 245 | * @param string $path The path to delete |
||
| 246 | */ |
||
| 247 | public static function purge(string $path): void |
||
| 248 | { |
||
| 249 | if (is_dir($path)) { |
||
| 250 | foreach (scandir($path) as $item) { |
||
| 251 | if (('.' === $item) || ('..' === $item)) { |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | self::purge($path.DIRECTORY_SEPARATOR.$item); |
||
| 256 | } |
||
| 257 | |||
| 258 | if (!rmdir($path)) { |
||
| 259 | throw new RuntimeException( |
||
| 260 | sprintf( |
||
| 261 | 'The directory "%s" could not be deleted.', |
||
| 262 | $path |
||
| 263 | ) |
||
| 264 | ); |
||
| 265 | } |
||
| 266 | } else { |
||
| 267 | if (!unlink($path)) { |
||
| 268 | throw new RuntimeException( |
||
| 269 | sprintf( |
||
| 270 | 'The file "%s" could not be deleted.', |
||
| 271 | $path |
||
| 272 | ) |
||
| 273 | ); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Creates a new directory. |
||
| 280 | * |
||
| 281 | * @param string $path The directory path |
||
| 282 | * @param int $chmod The file mode |
||
| 283 | * @param bool $recursive Recursively create path? |
||
| 284 | * |
||
| 285 | * @throws RuntimeException if the path could not be created |
||
| 286 | */ |
||
| 287 | private function createDir(string $path, int $chmod = 0777, bool $recursive = true): void |
||
| 288 | { |
||
| 289 | if (!mkdir($path, $chmod, $recursive)) { |
||
| 290 | throw new RuntimeException( |
||
| 291 | sprintf( |
||
| 292 | 'The directory path "%s" could not be created.', |
||
| 293 | $path |
||
| 294 | ) |
||
| 295 | ); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Creates a new file. |
||
| 301 | * |
||
| 302 | * @param string $path The file path |
||
| 303 | * @param string $contents The file contents |
||
| 304 | * @param int $mode The file mode |
||
| 305 | */ |
||
| 306 | private function createFile($path, $contents = '', $mode = 0666): void |
||
| 307 | { |
||
| 308 | if (false === file_put_contents($path, $contents)) { |
||
| 309 | throw new RuntimeException( |
||
| 310 | sprintf( |
||
| 311 | 'The file "%s" could not be written.', |
||
| 312 | $path |
||
| 313 | ) |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | |||
| 317 | if (!chmod($path, $mode)) { |
||
| 318 | throw new RuntimeException( |
||
| 319 | sprintf( |
||
| 320 | 'The file "%s" could not be chmodded to %o.', |
||
| 321 | $path, |
||
| 322 | $mode |
||
| 323 | ) |
||
| 324 | ); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Extracts a single file from the PHAR. |
||
| 330 | * |
||
| 331 | * @param array $info The file information |
||
| 332 | * |
||
| 333 | * @return string The file data |
||
| 334 | */ |
||
| 335 | private function extractFile(array $info): string |
||
| 336 | { |
||
| 337 | if (0 === $info['size']) { |
||
| 338 | return ''; |
||
| 339 | } |
||
| 340 | |||
| 341 | $data = $this->read($info['compressed_size']); |
||
| 342 | |||
| 343 | if ($info['flags'] & self::GZ) { |
||
| 344 | if (false === ($data = gzinflate($data))) { |
||
| 345 | throw new RuntimeException( |
||
| 346 | sprintf( |
||
| 347 | 'The "%s" file could not be inflated (gzip) from "%s".', |
||
| 348 | $info['path'], |
||
| 349 | $this->file |
||
| 350 | ) |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | } elseif ($info['flags'] & self::BZ2) { |
||
| 354 | if (false === ($data = bzdecompress($data))) { |
||
| 355 | throw new RuntimeException( |
||
| 356 | sprintf( |
||
| 357 | 'The "%s" file could not be inflated (bzip2) from "%s".', |
||
| 358 | $info['path'], |
||
| 359 | $this->file |
||
| 360 | ) |
||
| 361 | ); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | if (($actual = strlen($data)) !== $info['size']) { |
||
| 366 | throw new UnexpectedValueException( |
||
| 367 | sprintf( |
||
| 368 | 'The size of "%s" (%d) did not match what was expected (%d) in "%s".', |
||
| 369 | $info['path'], |
||
| 370 | $actual, |
||
| 371 | $info['size'], |
||
| 372 | $this->file |
||
| 373 | ) |
||
| 374 | ); |
||
| 375 | } |
||
| 376 | |||
| 377 | $crc32 = sprintf('%u', crc32($data) & 0xffffffff); |
||
| 378 | |||
| 379 | if ($info['crc32'] != $crc32) { |
||
| 380 | throw new UnexpectedValueException( |
||
| 381 | sprintf( |
||
| 382 | 'The crc32 checksum (%s) for "%s" did not match what was expected (%s) in "%s".', |
||
| 383 | $crc32, |
||
| 384 | $info['path'], |
||
| 385 | $info['crc32'], |
||
| 386 | $this->file |
||
| 387 | ) |
||
| 388 | ); |
||
| 389 | } |
||
| 390 | |||
| 391 | return $data; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Opens the file for reading. |
||
| 396 | */ |
||
| 397 | private function open(): void |
||
| 398 | { |
||
| 399 | if (null === ($this->handle = fopen($this->file, 'rb'))) { |
||
| 400 | $this->handle = null; |
||
| 401 | |||
| 402 | throw new RuntimeException( |
||
| 403 | sprintf( |
||
| 404 | 'The file "%s" could not be opened for reading.', |
||
| 405 | $this->file |
||
| 406 | ) |
||
| 407 | ); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Reads the number of bytes from the file. |
||
| 413 | * |
||
| 414 | * @param int $bytes The number of bytes |
||
| 415 | * |
||
| 416 | * @return string The binary string read |
||
| 417 | */ |
||
| 418 | private function read(int $bytes): string |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Reads and unpacks the manifest data from the phar. |
||
| 454 | * |
||
| 455 | * @return array the manifest |
||
| 456 | */ |
||
| 457 | private function readManifest(): array |
||
| 510 | } |
||
| 511 | } |
||
| 512 |