| Conditions | 2 |
| Paths | 2 |
| Total Lines | 51 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 110 | function addFile($data, $name, $time = 0) {
|
||
| 111 | $name = str_replace('\\', '/', $name);
|
||
| 112 | $hexdtime = pack('V', $this->unix2DosTime($time));
|
||
| 113 | $fr = "\x50\x4b\x03\x04"; |
||
| 114 | $fr .= "\x14\x00"; // ver needed to extract |
||
| 115 | $fr .= "\x00\x00"; // gen purpose bit flag |
||
| 116 | $fr .= "\x08\x00"; // compression method |
||
| 117 | $fr .= $hexdtime; // last mod time and date |
||
| 118 | // "local file header" segment |
||
| 119 | $unc_len = strlen($data); |
||
| 120 | $crc = crc32($data); |
||
| 121 | $zdata = gzcompress($data); |
||
| 122 | $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug |
||
| 123 | $c_len = strlen($zdata); |
||
| 124 | $fr .= pack('V', $crc); // crc32
|
||
| 125 | $fr .= pack('V', $c_len); // compressed filesize
|
||
| 126 | $fr .= pack('V', $unc_len); // uncompressed filesize
|
||
| 127 | $fr .= pack('v', strlen($name)); // length of filename
|
||
| 128 | $fr .= pack('v', 0); // extra field length
|
||
| 129 | $fr .= $name; |
||
| 130 | // "file data" segment |
||
| 131 | $fr .= $zdata; |
||
| 132 | // echo this entry on the fly, ... |
||
| 133 | if ($this->doWrite) {
|
||
| 134 | echo $fr; |
||
| 135 | } else { // ... OR add this entry to array
|
||
| 136 | $this->datasec[] = $fr; |
||
| 137 | } |
||
| 138 | // now add to central directory record |
||
| 139 | $cdrec = "\x50\x4b\x01\x02"; |
||
| 140 | $cdrec .= "\x00\x00"; // version made by |
||
| 141 | $cdrec .= "\x14\x00"; // version needed to extract |
||
| 142 | $cdrec .= "\x00\x00"; // gen purpose bit flag |
||
| 143 | $cdrec .= "\x08\x00"; // compression method |
||
| 144 | $cdrec .= $hexdtime; // last mod time & date |
||
| 145 | $cdrec .= pack('V', $crc); // crc32
|
||
| 146 | $cdrec .= pack('V', $c_len); // compressed filesize
|
||
| 147 | $cdrec .= pack('V', $unc_len); // uncompressed filesize
|
||
| 148 | $cdrec .= pack('v', strlen($name)); // length of filename
|
||
| 149 | $cdrec .= pack('v', 0); // extra field length
|
||
| 150 | $cdrec .= pack('v', 0); // file comment length
|
||
| 151 | $cdrec .= pack('v', 0); // disk number start
|
||
| 152 | $cdrec .= pack('v', 0); // internal file attributes
|
||
| 153 | $cdrec .= pack('V', 32); // external file attributes
|
||
| 154 | // - 'archive' bit set |
||
| 155 | $cdrec .= pack('V', $this->old_offset); // relative offset of local header
|
||
| 156 | $this->old_offset += strlen($fr); |
||
| 157 | $cdrec .= $name; |
||
| 158 | // optional extra field, file comment goes here |
||
| 159 | // save to central directory |
||
| 160 | $this->ctrl_dir[] = $cdrec; |
||
| 161 | } |
||
| 227 | // end of the 'ZipFile' class |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.