| Conditions | 1 |
| Paths | 1 |
| Total Lines | 65 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 39 | public function addFile($data, $name, $time = 0) |
||
| 40 | { |
||
| 41 | $name = str_replace('\\', '/', $name); |
||
| 42 | |||
| 43 | $dtime = dechex($this->unix2DosTime($time)); |
||
| 44 | $hexdtime = '\x' . $dtime[6] . $dtime[7] . '\x' . $dtime[4] . $dtime[5] . '\x' . $dtime[2] . $dtime[3] . '\x' . $dtime[0] . $dtime[1]; |
||
| 45 | eval('$hexdtime = \'' . $hexdtime . '\';'); |
||
|
|
|||
| 46 | |||
| 47 | $fr = "\x50\x4b\x03\x04"; |
||
| 48 | $fr .= "\x14\x00"; // ver needed to extract |
||
| 49 | $fr .= "\x00\x00"; // gen purpose bit flag |
||
| 50 | $fr .= "\x08\x00"; // compression method |
||
| 51 | $fr .= $hexdtime; // last mod time and date |
||
| 52 | |||
| 53 | // "local file header" segment |
||
| 54 | $unc_len = strlen($data); |
||
| 55 | $crc = crc32($data); |
||
| 56 | $zdata = gzcompress($data); |
||
| 57 | $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug |
||
| 58 | $c_len = strlen($zdata); |
||
| 59 | $fr .= pack('V', $crc); // crc32 |
||
| 60 | $fr .= pack('V', $c_len); // compressed filesize |
||
| 61 | $fr .= pack('V', $unc_len); // uncompressed filesize |
||
| 62 | $fr .= pack('v', strlen($name)); // length of filename |
||
| 63 | $fr .= pack('v', 0); // extra field length |
||
| 64 | $fr .= $name; |
||
| 65 | |||
| 66 | // "file data" segment |
||
| 67 | $fr .= $zdata; |
||
| 68 | |||
| 69 | // "data descriptor" segment (optional but necessary if archive is not |
||
| 70 | // served as file) |
||
| 71 | $fr .= pack('V', $crc); // crc32 |
||
| 72 | $fr .= pack('V', $c_len); // compressed filesize |
||
| 73 | $fr .= pack('V', $unc_len); // uncompressed filesize |
||
| 74 | |||
| 75 | // add this entry to array |
||
| 76 | $this->datasec[] = $fr; |
||
| 77 | $new_offset = strlen(implode('', $this->datasec)); |
||
| 78 | |||
| 79 | // now add to central directory record |
||
| 80 | $cdrec = "\x50\x4b\x01\x02"; |
||
| 81 | $cdrec .= "\x00\x00"; // version made by |
||
| 82 | $cdrec .= "\x14\x00"; // version needed to extract |
||
| 83 | $cdrec .= "\x00\x00"; // gen purpose bit flag |
||
| 84 | $cdrec .= "\x08\x00"; // compression method |
||
| 85 | $cdrec .= $hexdtime; // last mod time & date |
||
| 86 | $cdrec .= pack('V', $crc); // crc32 |
||
| 87 | $cdrec .= pack('V', $c_len); // compressed filesize |
||
| 88 | $cdrec .= pack('V', $unc_len); // uncompressed filesize |
||
| 89 | $cdrec .= pack('v', strlen($name)); // length of filename |
||
| 90 | $cdrec .= pack('v', 0); // extra field length |
||
| 91 | $cdrec .= pack('v', 0); // file comment length |
||
| 92 | $cdrec .= pack('v', 0); // disk number start |
||
| 93 | $cdrec .= pack('v', 0); // internal file attributes |
||
| 94 | $cdrec .= pack('V', 32); // external file attributes - 'archive' bit set |
||
| 95 | |||
| 96 | $cdrec .= pack('V', $this->old_offset); // relative offset of local header |
||
| 97 | $this->old_offset = $new_offset; |
||
| 98 | |||
| 99 | $cdrec .= $name; |
||
| 100 | |||
| 101 | // optional extra field, file comment goes here |
||
| 102 | // save to central directory |
||
| 103 | $this->ctrl_dir[] = $cdrec; |
||
| 104 | } // end of the 'addFile()' method |
||
| 118 |