Complex classes like MultipartFormDataParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MultipartFormDataParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class MultipartFormDataParser extends BaseObject implements RequestParserInterface |
||
63 | { |
||
64 | /** |
||
65 | * @var bool whether to parse raw body even for 'POST' request and `$_FILES` already populated. |
||
66 | * By default this option is disabled saving performance for 'POST' requests, which are already |
||
67 | * processed by PHP automatically. |
||
68 | * > Note: if this option is enabled, value of [[Request::$uploadedFiles]] will be reset on each parse. |
||
69 | * @since 2.0.13 |
||
70 | */ |
||
71 | public $force = false; |
||
72 | |||
73 | /** |
||
74 | * @var int upload file max size in bytes. |
||
75 | */ |
||
76 | private $_uploadFileMaxSize; |
||
77 | /** |
||
78 | * @var int maximum upload files count. |
||
79 | */ |
||
80 | private $_uploadFileMaxCount; |
||
81 | |||
82 | |||
83 | /** |
||
84 | * @return int upload file max size in bytes. |
||
85 | */ |
||
86 | 4 | public function getUploadFileMaxSize() |
|
87 | { |
||
88 | 4 | if ($this->_uploadFileMaxSize === null) { |
|
89 | 3 | $this->_uploadFileMaxSize = $this->getByteSize(ini_get('upload_max_filesize')); |
|
|
|||
90 | } |
||
91 | 4 | return $this->_uploadFileMaxSize; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param int $uploadFileMaxSize upload file max size in bytes. |
||
96 | */ |
||
97 | 1 | public function setUploadFileMaxSize($uploadFileMaxSize) |
|
98 | { |
||
99 | 1 | $this->_uploadFileMaxSize = $uploadFileMaxSize; |
|
100 | 1 | } |
|
101 | |||
102 | /** |
||
103 | * @return int maximum upload files count. |
||
104 | */ |
||
105 | 4 | public function getUploadFileMaxCount() |
|
106 | { |
||
107 | 4 | if ($this->_uploadFileMaxCount === null) { |
|
108 | 3 | $this->_uploadFileMaxCount = ini_get('max_file_uploads'); |
|
109 | } |
||
110 | 4 | return $this->_uploadFileMaxCount; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param int $uploadFileMaxCount maximum upload files count. |
||
115 | */ |
||
116 | 1 | public function setUploadFileMaxCount($uploadFileMaxCount) |
|
117 | { |
||
118 | 1 | $this->_uploadFileMaxCount = $uploadFileMaxCount; |
|
119 | 1 | } |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 6 | public function parse($request) |
|
125 | { |
||
126 | 6 | if (!$this->force) { |
|
127 | 5 | if (!empty($_POST) || !empty($_FILES)) { |
|
128 | // normal POST request is parsed by PHP automatically |
||
129 | 2 | return $_POST; |
|
130 | } |
||
131 | } |
||
132 | |||
133 | 4 | $uploadedFiles = []; |
|
134 | |||
135 | 4 | $contentType = $request->getContentType(); |
|
136 | 4 | $rawBody = $request->getBody()->__toString(); |
|
137 | |||
138 | 4 | if (empty($rawBody)) { |
|
139 | return []; |
||
140 | } |
||
141 | |||
142 | 4 | if (!preg_match('/boundary=(.*)$/is', $contentType, $matches)) { |
|
143 | return []; |
||
144 | } |
||
145 | 4 | $boundary = $matches[1]; |
|
146 | |||
147 | 4 | $bodyParts = preg_split('/\\R?-+' . preg_quote($boundary, '/') . '/s', $rawBody); |
|
148 | 4 | array_pop($bodyParts); // last block always has no data, contains boundary ending like `--` |
|
149 | |||
150 | 4 | $bodyParams = []; |
|
151 | 4 | $filesCount = 0; |
|
152 | 4 | foreach ($bodyParts as $bodyPart) { |
|
153 | 4 | if (empty($bodyPart)) { |
|
154 | 4 | continue; |
|
155 | } |
||
156 | 4 | [$headers, $value] = preg_split('/\\R\\R/', $bodyPart, 2); |
|
157 | 4 | $headers = $this->parseHeaders($headers); |
|
158 | |||
159 | 4 | if (!isset($headers['content-disposition']['name'])) { |
|
160 | continue; |
||
161 | } |
||
162 | |||
163 | 4 | if (isset($headers['content-disposition']['filename'])) { |
|
164 | // file upload: |
||
165 | 4 | if ($filesCount >= $this->getUploadFileMaxCount()) { |
|
166 | 1 | continue; |
|
167 | } |
||
168 | |||
169 | $fileConfig = [ |
||
170 | 4 | 'class' => $request->uploadedFileClass, |
|
171 | 4 | 'clientFilename' => $headers['content-disposition']['filename'], |
|
172 | 4 | 'clientMediaType' => ArrayHelper::getValue($headers, 'content-type', 'application/octet-stream'), |
|
173 | 4 | 'size' => StringHelper::byteLength($value), |
|
174 | 4 | 'error' => UPLOAD_ERR_OK, |
|
175 | 'tempFilename' => null, |
||
176 | ]; |
||
177 | |||
178 | 4 | if ($fileConfig['size'] > $this->getUploadFileMaxSize()) { |
|
179 | 1 | $fileConfig['error'] = UPLOAD_ERR_INI_SIZE; |
|
180 | } else { |
||
181 | 4 | $tmpResource = tmpfile(); |
|
182 | 4 | if ($tmpResource === false) { |
|
183 | $fileConfig['error'] = UPLOAD_ERR_CANT_WRITE; |
||
184 | } else { |
||
185 | 4 | $tmpResourceMetaData = stream_get_meta_data($tmpResource); |
|
186 | 4 | $tmpFileName = $tmpResourceMetaData['uri']; |
|
187 | 4 | if (empty($tmpFileName)) { |
|
188 | $fileConfig['error'] = UPLOAD_ERR_CANT_WRITE; |
||
189 | @fclose($tmpResource); |
||
190 | } else { |
||
191 | 4 | fwrite($tmpResource, $value); |
|
192 | 4 | $fileConfig['tempFilename'] = $tmpFileName; |
|
193 | 4 | $fileConfig['stream'] = new ResourceStream(['resource' => $tmpResource]); // save file resource, otherwise it will be deleted |
|
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | 4 | $this->addValue($uploadedFiles, $headers['content-disposition']['name'], Yii::createObject($fileConfig)); |
|
199 | |||
200 | 4 | $filesCount++; |
|
201 | } else { |
||
202 | // regular parameter: |
||
203 | 4 | $this->addValue($bodyParams, $headers['content-disposition']['name'], $value); |
|
204 | } |
||
205 | } |
||
206 | |||
207 | 4 | $request->setUploadedFiles($uploadedFiles); |
|
208 | |||
209 | 4 | return $bodyParams; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Parses content part headers. |
||
214 | * @param string $headerContent headers source content |
||
215 | * @return array parsed headers. |
||
216 | */ |
||
217 | 4 | private function parseHeaders($headerContent) |
|
250 | |||
251 | /** |
||
252 | * Adds value to the array by input name, e.g. `Item[name]`. |
||
253 | * @param array $array array which should store value. |
||
254 | * @param string $name input name specification. |
||
255 | * @param mixed $value value to be added. |
||
256 | */ |
||
257 | 4 | private function addValue(&$array, $name, $value) |
|
276 | |||
277 | /** |
||
278 | * Gets the size in bytes from verbose size representation. |
||
279 | * For example: '5K' => 5*1024 |
||
280 | * @param string $verboseSize verbose size representation. |
||
281 | * @return int actual size in bytes. |
||
282 | */ |
||
283 | 3 | private function getByteSize($verboseSize) |
|
311 | } |
||
312 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.