Complex classes like ZipDirectoryReader 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 ZipDirectoryReader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ZipDirectoryReader { |
||
| 32 | /** |
||
| 33 | * Read a ZIP file and call a function for each file discovered in it. |
||
| 34 | * |
||
| 35 | * Because this class is aimed at verification, an error is raised on |
||
| 36 | * suspicious or ambiguous input, instead of emulating some standard |
||
| 37 | * behavior. |
||
| 38 | * |
||
| 39 | * @param string $fileName The archive file name |
||
| 40 | * @param array $callback The callback function. It will be called for each file |
||
| 41 | * with a single associative array each time, with members: |
||
| 42 | * |
||
| 43 | * - name: The file name. Directories conventionally have a trailing |
||
| 44 | * slash. |
||
| 45 | * |
||
| 46 | * - mtime: The file modification time, in MediaWiki 14-char format |
||
| 47 | * |
||
| 48 | * - size: The uncompressed file size |
||
| 49 | * |
||
| 50 | * @param array $options An associative array of read options, with the option |
||
| 51 | * name in the key. This may currently contain: |
||
| 52 | * |
||
| 53 | * - zip64: If this is set to true, then we will emulate a |
||
| 54 | * library with ZIP64 support, like OpenJDK 7. If it is set to |
||
| 55 | * false, then we will emulate a library with no knowledge of |
||
| 56 | * ZIP64. |
||
| 57 | * |
||
| 58 | * NOTE: The ZIP64 code is untested and probably doesn't work. It |
||
| 59 | * turned out to be easier to just reject ZIP64 archive uploads, |
||
| 60 | * since they are likely to be very rare. Confirming safety of a |
||
| 61 | * ZIP64 file is fairly complex. What do you do with a file that is |
||
| 62 | * ambiguous and broken when read with a non-ZIP64 reader, but valid |
||
| 63 | * when read with a ZIP64 reader? This situation is normal for a |
||
| 64 | * valid ZIP64 file, and working out what non-ZIP64 readers will make |
||
| 65 | * of such a file is not trivial. |
||
| 66 | * |
||
| 67 | * @return Status A Status object. The following fatal errors are defined: |
||
| 68 | * |
||
| 69 | * - zip-file-open-error: The file could not be opened. |
||
| 70 | * |
||
| 71 | * - zip-wrong-format: The file does not appear to be a ZIP file. |
||
| 72 | * |
||
| 73 | * - zip-bad: There was something wrong or ambiguous about the file |
||
| 74 | * data. |
||
| 75 | * |
||
| 76 | * - zip-unsupported: The ZIP file uses features which |
||
| 77 | * ZipDirectoryReader does not support. |
||
| 78 | * |
||
| 79 | * The default messages for those fatal errors are written in a way that |
||
| 80 | * makes sense for upload verification. |
||
| 81 | * |
||
| 82 | * If a fatal error is returned, more information about the error will be |
||
| 83 | * available in the debug log. |
||
| 84 | * |
||
| 85 | * Note that the callback function may be called any number of times before |
||
| 86 | * a fatal error is returned. If this occurs, the data sent to the callback |
||
| 87 | * function should be discarded. |
||
| 88 | */ |
||
| 89 | public static function read( $fileName, $callback, $options = [] ) { |
||
| 94 | |||
| 95 | /** The file name */ |
||
| 96 | protected $fileName; |
||
| 97 | |||
| 98 | /** The opened file resource */ |
||
| 99 | protected $file; |
||
| 100 | |||
| 101 | /** The cached length of the file, or null if it has not been loaded yet. */ |
||
| 102 | protected $fileLength; |
||
| 103 | |||
| 104 | /** A segmented cache of the file contents */ |
||
| 105 | protected $buffer; |
||
| 106 | |||
| 107 | /** The file data callback */ |
||
| 108 | protected $callback; |
||
| 109 | |||
| 110 | /** The ZIP64 mode */ |
||
| 111 | protected $zip64 = false; |
||
| 112 | |||
| 113 | /** Stored headers */ |
||
| 114 | protected $eocdr, $eocdr64, $eocdr64Locator; |
||
|
|
|||
| 115 | |||
| 116 | protected $data; |
||
| 117 | |||
| 118 | /** The "extra field" ID for ZIP64 central directory entries */ |
||
| 119 | const ZIP64_EXTRA_HEADER = 0x0001; |
||
| 120 | |||
| 121 | /** The segment size for the file contents cache */ |
||
| 122 | const SEGSIZE = 16384; |
||
| 123 | |||
| 124 | /** The index of the "general field" bit for UTF-8 file names */ |
||
| 125 | const GENERAL_UTF8 = 11; |
||
| 126 | |||
| 127 | /** The index of the "general field" bit for central directory encryption */ |
||
| 128 | const GENERAL_CD_ENCRYPTED = 13; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Private constructor |
||
| 132 | * @param string $fileName |
||
| 133 | * @param callable $callback |
||
| 134 | * @param array $options |
||
| 135 | */ |
||
| 136 | protected function __construct( $fileName, $callback, $options ) { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Read the directory according to settings in $this. |
||
| 147 | * |
||
| 148 | * @return Status |
||
| 149 | */ |
||
| 150 | function execute() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Throw an error, and log a debug message |
||
| 187 | * @param mixed $code |
||
| 188 | * @param string $debugMessage |
||
| 189 | * @throws ZipDirectoryReaderError |
||
| 190 | */ |
||
| 191 | function error( $code, $debugMessage ) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Read the header which is at the end of the central directory, |
||
| 198 | * unimaginatively called the "end of central directory record" by the ZIP |
||
| 199 | * spec. |
||
| 200 | */ |
||
| 201 | function readEndOfCentralDirectoryRecord() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Read the header called the "ZIP64 end of central directory locator". An |
||
| 249 | * error will be raised if it does not exist. |
||
| 250 | */ |
||
| 251 | function readZip64EndOfCentralDirectoryLocator() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Read the header called the "ZIP64 end of central directory record". It |
||
| 274 | * may replace the regular "end of central directory record" in ZIP64 files. |
||
| 275 | */ |
||
| 276 | function readZip64EndOfCentralDirectoryRecord() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Find the location of the central directory, as would be seen by a |
||
| 310 | * non-ZIP64 reader. |
||
| 311 | * |
||
| 312 | * @return array List containing offset, size and end position. |
||
| 313 | */ |
||
| 314 | function findOldCentralDirectory() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Find the location of the central directory, as would be seen by a |
||
| 331 | * ZIP64-compliant reader. |
||
| 332 | * |
||
| 333 | * @return array List containing offset, size and end position. |
||
| 334 | */ |
||
| 335 | function findZip64CentralDirectory() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Read the central directory at the given location |
||
| 370 | * @param int $offset |
||
| 371 | * @param int $size |
||
| 372 | */ |
||
| 373 | function readCentralDirectory( $offset, $size ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Interpret ZIP64 "extra field" data and return an associative array. |
||
| 463 | * @param string $extraField |
||
| 464 | * @return array|bool |
||
| 465 | */ |
||
| 466 | function unpackZip64Extra( $extraField ) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Get the length of the file. |
||
| 499 | * @return int |
||
| 500 | */ |
||
| 501 | function getFileLength() { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Get the file contents from a given offset. If there are not enough bytes |
||
| 512 | * in the file to satisfy the request, an exception will be thrown. |
||
| 513 | * |
||
| 514 | * @param int $start The byte offset of the start of the block. |
||
| 515 | * @param int $length The number of bytes to return. If omitted, the remainder |
||
| 516 | * of the file will be returned. |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | function getBlock( $start, $length = null ) { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Get a section of the file starting at position $segIndex * self::SEGSIZE, |
||
| 555 | * of length self::SEGSIZE. The result is cached. This is a helper function |
||
| 556 | * for getBlock(). |
||
| 557 | * |
||
| 558 | * If there are not enough bytes in the file to satisfy the request, the |
||
| 559 | * return value will be truncated. If a request is made for a segment beyond |
||
| 560 | * the end of the file, an empty string will be returned. |
||
| 561 | * |
||
| 562 | * @param int $segIndex |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | function getSegment( $segIndex ) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Get the size of a structure in bytes. See unpack() for the format of $struct. |
||
| 589 | * @param array $struct |
||
| 590 | * @return int |
||
| 591 | */ |
||
| 592 | function getStructSize( $struct ) { |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Unpack a binary structure. This is like the built-in unpack() function |
||
| 608 | * except nicer. |
||
| 609 | * |
||
| 610 | * @param string $string The binary data input |
||
| 611 | * |
||
| 612 | * @param array $struct An associative array giving structure members and their |
||
| 613 | * types. In the key is the field name. The value may be either an |
||
| 614 | * integer, in which case the field is a little-endian unsigned integer |
||
| 615 | * encoded in the given number of bytes, or an array, in which case the |
||
| 616 | * first element of the array is the type name, and the subsequent |
||
| 617 | * elements are type-dependent parameters. Only one such type is defined: |
||
| 618 | * - "string": The second array element gives the length of string. |
||
| 619 | * Not null terminated. |
||
| 620 | * |
||
| 621 | * @param int $offset The offset into the string at which to start unpacking. |
||
| 622 | * |
||
| 623 | * @throws MWException |
||
| 624 | * @return array Unpacked associative array. Note that large integers in the input |
||
| 625 | * may be represented as floating point numbers in the return value, so |
||
| 626 | * the use of weak comparison is advised. |
||
| 627 | */ |
||
| 628 | function unpack( $string, $struct, $offset = 0 ) { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Returns a bit from a given position in an integer value, converted to |
||
| 675 | * boolean. |
||
| 676 | * |
||
| 677 | * @param int $value |
||
| 678 | * @param int $bitIndex The index of the bit, where 0 is the LSB. |
||
| 679 | * @return bool |
||
| 680 | */ |
||
| 681 | function testBit( $value, $bitIndex ) { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Debugging helper function which dumps a string in hexdump -C format. |
||
| 687 | * @param string $s |
||
| 688 | */ |
||
| 689 | function hexDump( $s ) { |
||
| 718 | } |
||
| 719 | |||
| 738 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.