| Conditions | 9 |
| Paths | 6 |
| Total Lines | 98 |
| Code Lines | 47 |
| 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 |
||
| 73 | private function inputVars() |
||
| 74 | {
|
||
| 75 | global $_PUT; |
||
| 76 | |||
| 77 | /* PUT data comes in on the stdin stream */ |
||
| 78 | $putdata = fopen("php://input", "r");
|
||
| 79 | |||
| 80 | /* Open a file for writing */ |
||
| 81 | // $fp = fopen("myputfile.ext", "w");
|
||
| 82 | |||
| 83 | $raw_data = ''; |
||
| 84 | |||
| 85 | /* Read the data 1 KB at a time |
||
| 86 | and write to the file */ |
||
| 87 | while ($chunk = fread($putdata, 1024)) |
||
|
|
|||
| 88 | $raw_data .= $chunk; |
||
| 89 | |||
| 90 | /* Close the streams */ |
||
| 91 | fclose($putdata); |
||
| 92 | |||
| 93 | // Fetch content and determine boundary |
||
| 94 | $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n")); |
||
| 95 | |||
| 96 | if(empty($boundary)){
|
||
| 97 | parse_str($raw_data,$data); |
||
| 98 | $GLOBALS[ '_PUT' ] = $data; |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | |||
| 102 | // Fetch each part |
||
| 103 | $parts = array_slice(explode($boundary, $raw_data), 1); |
||
| 104 | $data = array(); |
||
| 105 | |||
| 106 | foreach ($parts as $part) {
|
||
| 107 | // If this is the last part, break |
||
| 108 | if ($part == "--\r\n") break; |
||
| 109 | |||
| 110 | // Separate content from headers |
||
| 111 | $part = ltrim($part, "\r\n"); |
||
| 112 | list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);
|
||
| 113 | |||
| 114 | // Parse the headers list |
||
| 115 | $raw_headers = explode("\r\n", $raw_headers);
|
||
| 116 | $headers = array(); |
||
| 117 | foreach ($raw_headers as $header) {
|
||
| 118 | list($name, $value) = explode(':', $header);
|
||
| 119 | $headers[strtolower($name)] = ltrim($value, ' '); |
||
| 120 | } |
||
| 121 | |||
| 122 | // Parse the Content-Disposition to get the field name, etc. |
||
| 123 | if (isset($headers['content-disposition'])) {
|
||
| 124 | $filename = null; |
||
| 125 | $tmp_name = null; |
||
| 126 | preg_match( |
||
| 127 | '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', |
||
| 128 | $headers['content-disposition'], |
||
| 129 | $matches |
||
| 130 | ); |
||
| 131 | list(, $type, $name) = $matches; |
||
| 132 | |||
| 133 | //Parse File |
||
| 134 | if( isset($matches[4]) ) |
||
| 135 | {
|
||
| 136 | //if labeled the same as previous, skip |
||
| 137 | if( isset( $_FILES[ $matches[ 2 ] ] ) ) |
||
| 138 | {
|
||
| 139 | continue; |
||
| 140 | } |
||
| 141 | |||
| 142 | //get filename |
||
| 143 | $filename = $matches[4]; |
||
| 144 | |||
| 145 | //get tmp name |
||
| 146 | $filename_parts = pathinfo( $filename ); |
||
| 147 | $tmp_name = tempnam( ini_get('upload_tmp_dir'), $filename_parts['filename']);
|
||
| 148 | |||
| 149 | //populate $_FILES with information, size may be off in multibyte situation |
||
| 150 | $_FILES[ $matches[ 2 ] ] = array( |
||
| 151 | 'error'=>0, |
||
| 152 | 'name'=>$filename, |
||
| 153 | 'tmp_name'=>$tmp_name, |
||
| 154 | 'size'=>strlen( $body ), |
||
| 155 | 'type'=>$value |
||
| 156 | ); |
||
| 157 | |||
| 158 | //place in temporary directory |
||
| 159 | file_put_contents($tmp_name, $body); |
||
| 160 | } |
||
| 161 | //Parse Field |
||
| 162 | else |
||
| 163 | {
|
||
| 164 | $data[$name] = substr($body, 0, strlen($body) - 2); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | } |
||
| 169 | $GLOBALS[ '_PUT' ] = $data; |
||
| 170 | return; |
||
| 171 | } |
||
| 183 | } |