| Conditions | 10 |
| Paths | 13 |
| Total Lines | 96 |
| 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 |
||
| 128 | function spip_livrer_fichier_partie($fichier, $range=null){ |
||
| 129 | if (!file_exists($fichier)){ |
||
| 130 | throw new \Exception(sprintf('File not found: %s', $fichier)); |
||
| 131 | } |
||
| 132 | |||
| 133 | if (!is_readable($fichier)){ |
||
| 134 | throw new \Exception(sprintf('File not readable: %s', $fichier)); |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | // Par defaut on envoie tout |
||
| 139 | $byteOffset = 0; |
||
| 140 | $byteLength = $fileSize = filesize($fichier); |
||
| 141 | |||
| 142 | |||
| 143 | // Parse Content-Range header for byte offsets, looks like "bytes=11525-" OR "bytes=11525-12451" |
||
| 144 | if ($range and preg_match('%bytes=(\d+)-(\d+)?%i', $range, $match)){ |
||
| 145 | ### Offset signifies where we should begin to read the file |
||
| 146 | $byteOffset = (int)$match[1]; |
||
| 147 | |||
| 148 | |||
| 149 | ### Length is for how long we should read the file according to the browser, and can never go beyond the file size |
||
| 150 | if (isset($match[2])){ |
||
| 151 | $finishBytes = (int)$match[2]; |
||
| 152 | $byteLength = $finishBytes+1; |
||
| 153 | } else { |
||
| 154 | $finishBytes = $fileSize-1; |
||
| 155 | } |
||
| 156 | |||
| 157 | $cr_header = sprintf('Content-Range: bytes %d-%d/%d', $byteOffset, $finishBytes, $fileSize); |
||
| 158 | } |
||
| 159 | else { |
||
| 160 | // si pas de range valide, on delegue a la methode d'envoi complet |
||
| 161 | spip_livrer_fichier_entier($fichier); |
||
| 162 | // redondant, mais facilite la comprehension du code |
||
| 163 | exit(); |
||
| 164 | } |
||
| 165 | |||
| 166 | // Remove headers that might unnecessarily clutter up the output |
||
| 167 | header_remove('Cache-Control'); |
||
| 168 | header_remove('Pragma'); |
||
| 169 | |||
| 170 | // partial content |
||
| 171 | header("HTTP/1.1 206 Partial content"); |
||
| 172 | header($cr_header); ### Decrease by 1 on byte-length since this definition is zero-based index of bytes being sent |
||
| 173 | |||
| 174 | |||
| 175 | $byteRange = $byteLength-$byteOffset; |
||
| 176 | |||
| 177 | header(sprintf('Content-Length: %d', $byteRange)); |
||
| 178 | |||
| 179 | // Variable containing the buffer |
||
| 180 | $buffer = ''; |
||
| 181 | // Just a reasonable buffer size |
||
| 182 | $bufferSize = 512*16; |
||
| 183 | // Contains how much is left to read of the byteRange |
||
| 184 | $bytePool = $byteRange; |
||
| 185 | |||
| 186 | if (!$handle = fopen($fichier, 'r')){ |
||
| 187 | throw new \Exception(sprintf("Could not get handle for file %s", $fichier)); |
||
| 188 | } |
||
| 189 | |||
| 190 | if (fseek($handle, $byteOffset, SEEK_SET)==-1){ |
||
| 191 | throw new \Exception(sprintf("Could not seek to byte offset %d", $byteOffset)); |
||
| 192 | } |
||
| 193 | |||
| 194 | |||
| 195 | while ($bytePool>0){ |
||
| 196 | // How many bytes we request on this iteration |
||
| 197 | $chunkSizeRequested = min($bufferSize, $bytePool); |
||
| 198 | |||
| 199 | // Try readin $chunkSizeRequested bytes from $handle and put data in $buffer |
||
| 200 | $buffer = fread($handle, $chunkSizeRequested); |
||
| 201 | |||
| 202 | // Store how many bytes were actually read |
||
| 203 | $chunkSizeActual = strlen($buffer); |
||
| 204 | |||
| 205 | // If we didn't get any bytes that means something unexpected has happened since $bytePool should be zero already |
||
| 206 | if ($chunkSizeActual==0){ |
||
| 207 | // For production servers this should go in your php error log, since it will break the output |
||
| 208 | trigger_error('Chunksize became 0', E_USER_WARNING); |
||
| 209 | break; |
||
| 210 | } |
||
| 211 | |||
| 212 | // Decrease byte pool with amount of bytes that were read during this iteration |
||
| 213 | $bytePool -= $chunkSizeActual; |
||
| 214 | |||
| 215 | // Write the buffer to output |
||
| 216 | print $buffer; |
||
| 217 | |||
| 218 | // Try to output the data to the client immediately |
||
| 219 | flush(); |
||
| 220 | } |
||
| 221 | |||
| 222 | exit(); |
||
| 223 | } |
||
| 224 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.