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. 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 UploadedFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | final class UploadedFile implements UploadedFileInterface |
||
17 | { |
||
18 | /** @var int[] */ |
||
19 | private static $errors = [ |
||
20 | UPLOAD_ERR_OK, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, |
||
21 | UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION, |
||
22 | ]; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $clientFilename; |
||
26 | |||
27 | /** @var string */ |
||
28 | private $clientMediaType; |
||
29 | |||
30 | /** @var int */ |
||
31 | private $error; |
||
32 | |||
33 | /** @var null|string */ |
||
34 | private $file; |
||
35 | |||
36 | /** @var bool */ |
||
37 | private $moved = false; |
||
38 | |||
39 | /** @var null|int */ |
||
40 | private $size; |
||
41 | |||
42 | /** @var null|StreamInterface */ |
||
43 | private $stream; |
||
44 | |||
45 | /** |
||
46 | * @param StreamInterface|string|resource $streamOrFile |
||
47 | * @param int $size |
||
48 | * @param int $errorStatus |
||
49 | * @param string|null $clientFilename |
||
50 | * @param string|null $clientMediaType |
||
51 | */ |
||
52 | 75 | public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null) |
|
63 | |||
64 | /** |
||
65 | * Depending on the value set file or stream variable. |
||
66 | * |
||
67 | * @param string|resource|StreamInterface $streamOrFile |
||
68 | * |
||
69 | * @throws InvalidArgumentException |
||
70 | */ |
||
71 | 32 | private function setStreamOrFile($streamOrFile): void |
|
83 | |||
84 | 75 | private function setError($error): void |
|
96 | |||
97 | 66 | private function setSize($size): void |
|
105 | |||
106 | 66 | private function isStringOrNull($param): bool |
|
110 | |||
111 | 16 | private function isStringNotEmpty($param): bool |
|
115 | |||
116 | 66 | private function setClientFilename($clientFilename): void |
|
124 | |||
125 | 60 | private function setClientMediaType($clientMediaType): void |
|
133 | |||
134 | /** |
||
135 | * @return bool Return true if there is no upload error. |
||
136 | */ |
||
137 | 54 | private function isOk(): bool |
|
141 | |||
142 | /** |
||
143 | * @throws RuntimeException if is moved or not ok |
||
144 | */ |
||
145 | 34 | private function validateActive(): void |
|
155 | |||
156 | 18 | public function getStream(): StreamInterface |
|
168 | |||
169 | 23 | public function moveTo($targetPath): void |
|
194 | |||
195 | 3 | public function getSize(): ?int |
|
199 | |||
200 | 10 | public function getError(): int |
|
204 | |||
205 | 3 | public function getClientFilename(): ?string |
|
209 | |||
210 | 3 | public function getClientMediaType(): ?string |
|
214 | |||
215 | /** |
||
216 | * Copy the contents of a stream into another stream until the given number |
||
217 | * of bytes have been read. |
||
218 | * |
||
219 | * @author Michael Dowling and contributors to guzzlehttp/psr7 |
||
220 | * |
||
221 | * @param StreamInterface $source Stream to read from |
||
222 | * @param StreamInterface $dest Stream to write to |
||
223 | * @param int $maxLen Maximum number of bytes to read. Pass -1 |
||
224 | * to read the entire stream |
||
225 | * |
||
226 | * @throws \RuntimeException on error |
||
227 | */ |
||
228 | 7 | private function copyToStream(StreamInterface $source, StreamInterface $dest, $maxLen = -1) |
|
253 | } |
||
254 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..