| Conditions | 26 |
| Paths | 596 |
| Total Lines | 120 |
| Code Lines | 50 |
| 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 |
||
| 65 | function force_download($filename = '', $data = '', $set_mime = FALSE) |
||
| 66 | { |
||
| 67 | if ($filename === '' OR $data === '') |
||
| 68 | { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | elseif ($data === NULL) |
||
| 72 | { |
||
| 73 | if ( ! @is_file($filename) OR ($filesize = @filesize($filename)) === FALSE) |
||
| 74 | { |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $filepath = $filename; |
||
| 79 | $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename)); |
||
| 80 | $filename = end($filename); |
||
| 81 | } |
||
| 82 | else |
||
| 83 | { |
||
| 84 | $filesize = strlen($data); |
||
| 85 | } |
||
| 86 | |||
| 87 | // Set the default MIME type to send |
||
| 88 | $mime = 'application/octet-stream'; |
||
| 89 | |||
| 90 | $x = explode('.', $filename); |
||
| 91 | $extension = end($x); |
||
| 92 | |||
| 93 | if ($set_mime === TRUE) |
||
| 94 | { |
||
| 95 | if (count($x) === 1 OR $extension === '') |
||
| 96 | { |
||
| 97 | /* If we're going to detect the MIME type, |
||
| 98 | * we'll need a file extension. |
||
| 99 | */ |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | // Load the mime types |
||
| 104 | $mimes =& get_mimes(); |
||
| 105 | |||
| 106 | // Only change the default MIME if we can find one |
||
| 107 | if (isset($mimes[$extension])) |
||
| 108 | { |
||
| 109 | $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension]; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | /* It was reported that browsers on Android 2.1 (and possibly older as well) |
||
| 114 | * need to have the filename extension upper-cased in order to be able to |
||
| 115 | * download it. |
||
| 116 | * |
||
| 117 | * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/ |
||
| 118 | */ |
||
| 119 | if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT'])) |
||
| 120 | { |
||
| 121 | $x[count($x) - 1] = strtoupper($extension); |
||
| 122 | $filename = implode('.', $x); |
||
| 123 | } |
||
| 124 | |||
| 125 | if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) |
||
| 126 | { |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | if (ENVIRONMENT !== 'testing') |
||
| 131 | { |
||
| 132 | // Clean output buffer |
||
| 133 | if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) |
||
| 134 | { |
||
| 135 | @ob_clean(); |
||
| 136 | } |
||
| 137 | |||
| 138 | // Generate the server headers |
||
| 139 | header('Content-Type: '.$mime); |
||
| 140 | header('Content-Disposition: attachment; filename="'.$filename.'"'); |
||
| 141 | header('Expires: 0'); |
||
| 142 | header('Content-Transfer-Encoding: binary'); |
||
| 143 | header('Content-Length: '.$filesize); |
||
| 144 | header('Cache-Control: private, no-transform, no-store, must-revalidate'); |
||
| 145 | } |
||
| 146 | |||
| 147 | if (ENVIRONMENT === 'testing') |
||
| 148 | { |
||
| 149 | while (ob_get_level() > 2) |
||
| 150 | { |
||
| 151 | ob_end_clean(); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | // If we have raw data - just dump it |
||
| 156 | if ($data !== NULL) |
||
| 157 | { |
||
| 158 | if (ENVIRONMENT !== 'testing') |
||
| 159 | { |
||
| 160 | exit($data); |
||
| 161 | } |
||
| 162 | else |
||
| 163 | { |
||
| 164 | echo($data); |
||
| 165 | throw new CIPHPUnitTestExitException('exit() from force_download()'); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | // Flush 1MB chunks of data |
||
| 170 | while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) |
||
| 171 | { |
||
| 172 | echo $data; |
||
| 173 | } |
||
| 174 | |||
| 175 | fclose($fp); |
||
| 176 | if (ENVIRONMENT !== 'testing') |
||
| 177 | { |
||
| 178 | exit; |
||
| 179 | } |
||
| 180 | else |
||
| 181 | { |
||
| 182 | throw new CIPHPUnitTestExitException('exit() from force_download()'); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.