Conditions | 9 |
Paths | 6 |
Total Lines | 98 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
10 | public function getInputData() |
||
11 | { |
||
12 | global /** @noinspection PhpUnusedLocalVariableInspection */ |
||
13 | $_PUT; |
||
14 | |||
15 | /* PUT data comes in on the stdin stream */ |
||
16 | $putdata = fopen("php://input", "r"); |
||
17 | |||
18 | $raw_data = ''; |
||
19 | |||
20 | /* Read the data 1 KB at a time |
||
21 | and write to the file */ |
||
22 | while ($chunk = fread($putdata, 1024)) |
||
|
|||
23 | $raw_data .= $chunk; |
||
24 | |||
25 | /* Close the streams */ |
||
26 | fclose($putdata); |
||
27 | |||
28 | // Fetch content and determine boundary |
||
29 | $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n")); |
||
30 | |||
31 | if(empty($boundary)){ |
||
32 | parse_str($raw_data,$data); |
||
33 | $GLOBALS[ '_PUT' ] = $data; |
||
34 | return; |
||
35 | } |
||
36 | |||
37 | // Fetch each part |
||
38 | $parts = array_slice(explode($boundary, $raw_data), 1); |
||
39 | $data = array(); |
||
40 | |||
41 | foreach ($parts as $part) { |
||
42 | // If this is the last part, break |
||
43 | if ($part == "--\r\n") break; |
||
44 | |||
45 | // Separate content from headers |
||
46 | $part = ltrim($part, "\r\n"); |
||
47 | list($raw_headers, $body) = explode("\r\n\r\n", $part, 2); |
||
48 | |||
49 | // Parse the headers list |
||
50 | $raw_headers = explode("\r\n", $raw_headers); |
||
51 | $headers = array(); |
||
52 | foreach ($raw_headers as $header) { |
||
53 | list($name, $value) = explode(':', $header); |
||
54 | $headers[strtolower($name)] = ltrim($value, ' '); |
||
55 | } |
||
56 | |||
57 | // Parse the Content-Disposition to get the field name, etc. |
||
58 | if (isset($headers['content-disposition'])) { |
||
59 | $filename = null; |
||
60 | $tmp_name = null; |
||
61 | preg_match( |
||
62 | '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', |
||
63 | $headers['content-disposition'], |
||
64 | $matches |
||
65 | ); |
||
66 | /** @noinspection PhpUnusedLocalVariableInspection */ |
||
67 | list(, $type, $name) = $matches; |
||
68 | |||
69 | //Parse File |
||
70 | if( isset($matches[4]) ) |
||
71 | { |
||
72 | //if labeled the same as previous, skip |
||
73 | if( isset( $_FILES[ $matches[ 2 ] ] ) ) |
||
74 | { |
||
75 | continue; |
||
76 | } |
||
77 | |||
78 | //get filename |
||
79 | $filename = $matches[4]; |
||
80 | |||
81 | //get tmp name |
||
82 | $filename_parts = pathinfo( $filename ); |
||
83 | $tmp_name = tempnam( ini_get('upload_tmp_dir'), $filename_parts['filename']); |
||
84 | |||
85 | //populate $_FILES with information, size may be off in multibyte situation |
||
86 | /** @noinspection PhpUndefinedVariableInspection */ |
||
87 | $_FILES[ $matches[ 2 ] ] = array( |
||
88 | 'error'=>0, |
||
89 | 'name'=>$filename, |
||
90 | 'tmp_name'=>$tmp_name, |
||
91 | 'size'=>strlen( $body ), |
||
92 | 'type'=>$value |
||
93 | ); |
||
94 | |||
95 | //place in temporary directory |
||
96 | file_put_contents($tmp_name, $body); |
||
97 | } |
||
98 | //Parse Field |
||
99 | else |
||
100 | { |
||
101 | $data[$name] = substr($body, 0, strlen($body) - 2); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | } |
||
106 | $GLOBALS[ '_PUT' ] = $data; |
||
107 | return $data; |
||
108 | } |
||
140 | } |