Total Complexity | 43 |
Total Lines | 258 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like UploadedFile 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.
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 UploadedFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class UploadedFile implements UploadedFileInterface |
||
54 | { |
||
55 | /** |
||
56 | * The uploaded file name |
||
57 | * @var string |
||
58 | */ |
||
59 | protected ?string $filename = null; |
||
60 | |||
61 | /** |
||
62 | * Whether the uploaded file is moved |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected bool $moved = false; |
||
66 | |||
67 | /** |
||
68 | * The uploaded file stream |
||
69 | * @var StreamInterface |
||
70 | */ |
||
71 | protected ?StreamInterface $stream = null; |
||
72 | |||
73 | /** |
||
74 | * The uploaded file size |
||
75 | * @var int|null |
||
76 | */ |
||
77 | protected ?int $size; |
||
78 | |||
79 | /** |
||
80 | * The uploaded file error |
||
81 | * @var int |
||
82 | */ |
||
83 | protected int $error = \UPLOAD_ERR_OK; |
||
84 | |||
85 | /** |
||
86 | * The uploaded file client name |
||
87 | * @var string|null |
||
88 | */ |
||
89 | protected ?string $clientFilename; |
||
90 | |||
91 | /** |
||
92 | * The uploaded file client media type |
||
93 | * @var string|null |
||
94 | */ |
||
95 | protected ?string $clientMediaType; |
||
96 | |||
97 | /** |
||
98 | * Create new uploaded file instance |
||
99 | * |
||
100 | * @param string|StreamInterface $filenameOrStream the filename or stream |
||
101 | * @param int|null $size the upload file size |
||
102 | * @param int $error the upload error code |
||
103 | * @param string|null $clientFilename |
||
104 | * @param string|null $clientMediaType |
||
105 | */ |
||
106 | public function __construct( |
||
107 | $filenameOrStream, |
||
108 | ?int $size = null, |
||
109 | int $error = \UPLOAD_ERR_OK, |
||
110 | ?string $clientFilename = null, |
||
111 | ?string $clientMediaType = null |
||
112 | ) { |
||
113 | if ($filenameOrStream instanceof StreamInterface) { |
||
114 | if (!$filenameOrStream->isReadable()) { |
||
115 | throw new InvalidArgumentException('Stream is not readable'); |
||
116 | } |
||
117 | $this->stream = $filenameOrStream; |
||
118 | $this->size = $size ? $size : $filenameOrStream->getSize(); |
||
119 | } else { |
||
120 | $this->filename = $filenameOrStream; |
||
121 | $this->size = $size; |
||
122 | } |
||
123 | |||
124 | $this->error = $this->filterError($error); |
||
125 | $this->clientFilename = $clientFilename; |
||
126 | $this->clientMediaType = $clientMediaType; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Create uploaded file from global variable $_FILES |
||
131 | * @return array<mixed> |
||
132 | */ |
||
133 | public static function createFromGlobals(): array |
||
134 | { |
||
135 | return static::normalize($_FILES); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | public function getStream(): StreamInterface |
||
142 | { |
||
143 | if ($this->moved) { |
||
144 | throw new RuntimeException('Stream is not avaliable! Uploaded file is moved'); |
||
145 | } |
||
146 | |||
147 | if ($this->stream === null && $this->filename !== null) { |
||
148 | $this->stream = new Stream($this->filename); |
||
149 | } |
||
150 | |||
151 | return $this->stream; |
||
|
|||
152 | } |
||
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function moveTo(string $targetPath): void |
||
158 | { |
||
159 | if ($this->moved) { |
||
160 | throw new RuntimeException('Uploaded file is already moved'); |
||
161 | } |
||
162 | |||
163 | $cleanTargetPath = $this->filterTargetPath($targetPath); |
||
164 | if ($this->filename !== null) { |
||
165 | if (php_sapi_name() === 'cli') { |
||
166 | if (rename($this->filename, $cleanTargetPath) === false) { |
||
167 | throw new RuntimeException('Unable to rename the uploaded file'); |
||
168 | } |
||
169 | } else { |
||
170 | if ( |
||
171 | is_uploaded_file($this->filename) === false || move_uploaded_file( |
||
172 | $this->filename, |
||
173 | $cleanTargetPath |
||
174 | ) === false |
||
175 | ) { |
||
176 | throw new RuntimeException('Unable to move the uploaded file'); |
||
177 | } |
||
178 | } |
||
179 | } else { |
||
180 | $stream = $this->getStream(); |
||
181 | if ($stream->isSeekable()) { |
||
182 | $stream->rewind(); |
||
183 | } |
||
184 | $dest = new Stream($cleanTargetPath); |
||
185 | $bufferSize = 8192; |
||
186 | while (!$stream->eof()) { |
||
187 | if (!$dest->write($stream->read($bufferSize))) { |
||
188 | break; |
||
189 | } |
||
190 | } |
||
191 | $stream->close(); |
||
192 | } |
||
193 | $this->moved = true; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function getSize(): ?int |
||
200 | { |
||
201 | return $this->size; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | public function getError(): int |
||
208 | { |
||
209 | return $this->error; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | public function getClientFilename(): ?string |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | public function getClientMediaType(): ?string |
||
224 | { |
||
225 | return $this->clientMediaType; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Normalize files according to standard |
||
230 | * @param array<string, array<string, mixed|UploadedFileInterface>> $files |
||
231 | * @return array<string, UploadedFileInterface|UploadedFileInterface[]> |
||
232 | */ |
||
233 | public static function normalize(array $files): array |
||
234 | { |
||
235 | $normalized = []; |
||
236 | foreach ($files as $name => $info) { |
||
237 | if ($info instanceof UploadedFileInterface) { |
||
238 | $normalized[$name] = $info; |
||
239 | continue; |
||
240 | } |
||
241 | |||
242 | if (!isset($info['error'])) { |
||
243 | if (is_array($info)) { |
||
244 | $normalized[$name] = static::normalize($info); |
||
245 | } |
||
246 | continue; |
||
247 | } |
||
248 | |||
249 | $normalized[$name] = []; |
||
250 | if (!is_array($info['error'])) { |
||
251 | $normalized[$name] = new static( |
||
252 | isset($info['tmp_name']) ? $info['tmp_name'] : '', |
||
253 | !empty($info['size']) ? $info['size'] : null, |
||
254 | $info['error'], |
||
255 | !empty($info['name']) ? $info['name'] : null, |
||
256 | !empty($info['type']) ? $info['type'] : null, |
||
257 | ); |
||
258 | } else { |
||
259 | $nestedInfo = []; |
||
260 | foreach (array_keys($info['error']) as $key) { |
||
261 | $nestedInfo[$key]['tmp_name'] = isset($info['tmp_name'][$key]) ? $info['tmp_name'][$key] : ''; |
||
262 | $nestedInfo[$key]['name'] = isset($info['name'][$key]) ? $info['name'][$key] : ''; |
||
263 | $nestedInfo[$key]['size'] = isset($info['size'][$key]) ? $info['size'][$key] : null; |
||
264 | $nestedInfo[$key]['error'] = isset($info['error'][$key]) ? $info['error'][$key] : 0; |
||
265 | $nestedInfo[$key]['type'] = isset($info['type'][$key]) ? $info['type'][$key] : ''; |
||
266 | |||
267 | $normalized[$name] = static::normalize($nestedInfo); |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | |||
272 | return $normalized; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Filter the uploded file error |
||
277 | * @param int $error |
||
278 | * @return int |
||
279 | */ |
||
280 | protected function filterError(int $error): int |
||
281 | { |
||
282 | $validErrors = [ |
||
283 | \UPLOAD_ERR_OK, |
||
284 | \UPLOAD_ERR_INI_SIZE, |
||
285 | \UPLOAD_ERR_FORM_SIZE, |
||
286 | \UPLOAD_ERR_PARTIAL, |
||
287 | \UPLOAD_ERR_NO_FILE, |
||
288 | \UPLOAD_ERR_NO_TMP_DIR, |
||
289 | \UPLOAD_ERR_CANT_WRITE, |
||
290 | \UPLOAD_ERR_EXTENSION |
||
291 | ]; |
||
292 | |||
293 | if (!in_array($error, $validErrors)) { |
||
294 | throw new InvalidArgumentException('Upload error code must be a PHP file upload error.'); |
||
295 | } |
||
296 | |||
297 | return $error; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Filter the uploded file target path |
||
302 | * @param string $targetPath |
||
303 | * @return string |
||
304 | */ |
||
305 | protected function filterTargetPath(string $targetPath): string |
||
311 | } |
||
312 | } |
||
313 |