Conditions | 23 |
Paths | 24 |
Total Lines | 111 |
Code Lines | 68 |
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 |
||
68 | public function fetchPhoto($file) |
||
69 | { |
||
70 | $jupart = Request::getInt('jupart', 0, 'POST'); |
||
71 | $jufinal = Request::getInt('jufinal', 1, 'POST'); |
||
72 | $md5sums = $_POST['md5sum'][0] ?? null; |
||
73 | |||
74 | if ('' == $this->uploadDir) { |
||
75 | $this->abort('upload dir not defined'); |
||
76 | |||
77 | return false; |
||
78 | } |
||
79 | |||
80 | if (!\is_dir($this->uploadDir)) { |
||
81 | $this->abort('fail to open upload dir'); |
||
82 | |||
83 | return false; |
||
84 | } |
||
85 | |||
86 | if (!\is_writable($this->uploadDir)) { |
||
87 | $this->abort('upload dir not writable'); |
||
88 | |||
89 | return false; |
||
90 | } |
||
91 | |||
92 | if ($this->checkMd5 && !isset($md5sums)) { |
||
93 | $this->abort('Expecting an MD5 checksum'); |
||
94 | |||
95 | return false; |
||
96 | } |
||
97 | |||
98 | $dstdir = $this->uploadDir; |
||
99 | $dstname = $dstdir . '/juvar.' . \session_id(); |
||
100 | $tmpname = $dstdir . '/juvar.tmp' . \session_id(); |
||
101 | |||
102 | if (!\move_uploaded_file($file['tmp_name'], $tmpname)) { |
||
103 | $this->abort('Unable to move uploaded file'); |
||
104 | |||
105 | return false; |
||
106 | } |
||
107 | |||
108 | if ($jupart) { |
||
109 | // got a chunk of a multi-part upload |
||
110 | $len = \filesize($tmpname); |
||
111 | $_SESSION['juvar.tmpsize'] += $len; |
||
112 | if ($len > 0) { |
||
113 | $src = \fopen($tmpname, 'rb'); |
||
114 | $dst = \fopen($dstname, (1 == $jupart) ? 'wb' : 'ab'); |
||
115 | while ($len > 0) { |
||
116 | $rlen = ($len > 8192) ? 8192 : $len; |
||
117 | $buf = \fread($src, $rlen); |
||
118 | if (!$buf) { |
||
119 | \fclose($src); |
||
120 | \fclose($dst); |
||
121 | \unlink($dstname); |
||
122 | $this->abort('read IO error'); |
||
123 | |||
124 | return false; |
||
125 | } |
||
126 | if (!\fwrite($dst, $buf, $rlen)) { |
||
127 | \fclose($src); |
||
128 | \fclose($dst); |
||
129 | \unlink($dstname); |
||
130 | $this->abort('write IO error'); |
||
131 | |||
132 | return false; |
||
133 | } |
||
134 | $len -= $rlen; |
||
135 | } |
||
136 | \fclose($src); |
||
137 | \fclose($dst); |
||
138 | \unlink($tmpname); |
||
139 | } |
||
140 | if ($jufinal) { |
||
141 | // This is the last chunk. Check total lenght and rename it to it's final name. |
||
142 | $dlen = \filesize($dstname); |
||
143 | if ($dlen != $_SESSION['juvar.tmpsize']) { |
||
144 | $this->abort('file size mismatch'); |
||
145 | |||
146 | return false; |
||
147 | } |
||
148 | if ($this->checkMd5 && ($md5sums != \md5_file($dstname))) { |
||
149 | $this->abort('MD5 checksum mismatch'); |
||
150 | |||
151 | return false; |
||
152 | } |
||
153 | // remove zero sized files |
||
154 | if ($dlen > 0) { |
||
155 | if (!$this->_saveFile($dstname, $file['name'])) { |
||
156 | return false; |
||
157 | } |
||
158 | } else { |
||
159 | $this->abort('0 file size'); |
||
160 | |||
161 | return false; |
||
162 | } |
||
163 | // reset session var |
||
164 | $_SESSION['juvar.tmpsize'] = 0; |
||
165 | } |
||
166 | } else { |
||
167 | // Got a single file upload. Trivial. |
||
168 | if ($this->checkMd5 && $md5sums != \md5_file($tmpname)) { |
||
169 | $this->abort('MD5 checksum mismatch'); |
||
170 | |||
171 | return false; |
||
172 | } |
||
173 | if (!$this->_saveFile($tmpname, $file['name'])) { |
||
174 | return false; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | return true; |
||
179 | } |
||
375 |