Conditions | 21 |
Paths | 116 |
Total Lines | 94 |
Code Lines | 39 |
Lines | 4 |
Ratio | 4.26 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
64 | function force_download($filename = '', $data = '', $set_mime = FALSE) |
||
65 | { |
||
66 | if ($filename === '' OR $data === '') |
||
67 | { |
||
68 | return; |
||
69 | } |
||
70 | elseif ($data === NULL) |
||
71 | { |
||
72 | if ( ! @is_file($filename) OR ($filesize = @filesize($filename)) === FALSE) |
||
73 | { |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | $filepath = $filename; |
||
78 | $filename = explode('/', str_replace(DIRECTORY_SEPARATOR, '/', $filename)); |
||
79 | $filename = end($filename); |
||
80 | } |
||
81 | else |
||
82 | { |
||
83 | $filesize = strlen($data); |
||
84 | } |
||
85 | |||
86 | // Set the default MIME type to send |
||
87 | $mime = 'application/octet-stream'; |
||
88 | |||
89 | $x = explode('.', $filename); |
||
90 | $extension = end($x); |
||
91 | |||
92 | if ($set_mime === TRUE) |
||
93 | { |
||
94 | if (count($x) === 1 OR $extension === '') |
||
95 | { |
||
96 | /* If we're going to detect the MIME type, |
||
97 | * we'll need a file extension. |
||
98 | */ |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | // Load the mime types |
||
103 | $mimes =& get_mimes(); |
||
104 | |||
105 | // Only change the default MIME if we can find one |
||
106 | View Code Duplication | if (isset($mimes[$extension])) |
|
107 | { |
||
108 | $mime = is_array($mimes[$extension]) ? $mimes[$extension][0] : $mimes[$extension]; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /* It was reported that browsers on Android 2.1 (and possibly older as well) |
||
113 | * need to have the filename extension upper-cased in order to be able to |
||
114 | * download it. |
||
115 | * |
||
116 | * Reference: http://digiblog.de/2011/04/19/android-and-the-download-file-headers/ |
||
117 | */ |
||
118 | if (count($x) !== 1 && isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/Android\s(1|2\.[01])/', $_SERVER['HTTP_USER_AGENT'])) |
||
119 | { |
||
120 | $x[count($x) - 1] = strtoupper($extension); |
||
121 | $filename = implode('.', $x); |
||
122 | } |
||
123 | |||
124 | if ($data === NULL && ($fp = @fopen($filepath, 'rb')) === FALSE) |
||
125 | { |
||
126 | return; |
||
127 | } |
||
128 | |||
129 | // Clean output buffer |
||
130 | if (ob_get_level() !== 0 && @ob_end_clean() === FALSE) |
||
131 | { |
||
132 | @ob_clean(); |
||
133 | } |
||
134 | |||
135 | // Generate the server headers |
||
136 | header('Content-Type: '.$mime); |
||
137 | header('Content-Disposition: attachment; filename="'.$filename.'"'); |
||
138 | header('Expires: 0'); |
||
139 | header('Content-Transfer-Encoding: binary'); |
||
140 | header('Content-Length: '.$filesize); |
||
141 | header('Cache-Control: private, no-transform, no-store, must-revalidate'); |
||
142 | |||
143 | // If we have raw data - just dump it |
||
144 | if ($data !== NULL) |
||
145 | { |
||
146 | exit($data); |
||
147 | } |
||
148 | |||
149 | // Flush 1MB chunks of data |
||
150 | while ( ! feof($fp) && ($data = fread($fp, 1048576)) !== FALSE) |
||
151 | { |
||
152 | echo $data; |
||
153 | } |
||
154 | |||
155 | fclose($fp); |
||
156 | exit; |
||
157 | } |
||
158 | } |
||
159 |
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.