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 |
||
14 | final class UploadedFile implements UploadedFileInterface |
||
15 | { |
||
16 | /** @var array */ |
||
17 | private const ERRORS = [ |
||
18 | \UPLOAD_ERR_OK => 1, |
||
19 | \UPLOAD_ERR_INI_SIZE => 1, |
||
20 | \UPLOAD_ERR_FORM_SIZE => 1, |
||
21 | \UPLOAD_ERR_PARTIAL => 1, |
||
22 | \UPLOAD_ERR_NO_FILE => 1, |
||
23 | \UPLOAD_ERR_NO_TMP_DIR => 1, |
||
24 | \UPLOAD_ERR_CANT_WRITE => 1, |
||
25 | \UPLOAD_ERR_EXTENSION => 1, |
||
26 | ]; |
||
27 | |||
28 | /** @var string */ |
||
29 | private $clientFilename; |
||
30 | |||
31 | /** @var string */ |
||
32 | private $clientMediaType; |
||
33 | |||
34 | /** @var int */ |
||
35 | private $error; |
||
36 | |||
37 | /** @var null|string */ |
||
38 | private $file; |
||
39 | |||
40 | /** @var bool */ |
||
41 | private $moved = false; |
||
42 | |||
43 | /** @var null|int */ |
||
44 | private $size; |
||
45 | |||
46 | /** @var null|StreamInterface */ |
||
47 | private $stream; |
||
48 | |||
49 | /** |
||
50 | 75 | * @param StreamInterface|string|resource $streamOrFile |
|
51 | * @param int $size |
||
52 | 75 | * @param int $errorStatus |
|
53 | 66 | * @param string|null $clientFilename |
|
54 | 66 | * @param string|null $clientMediaType |
|
55 | 60 | */ |
|
56 | public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null) |
||
67 | |||
68 | /** |
||
69 | 32 | * Depending on the value set file or stream variable. |
|
70 | * |
||
71 | 32 | * @param string|resource|StreamInterface $streamOrFile |
|
72 | 1 | * |
|
73 | 31 | * @throws \InvalidArgumentException |
|
74 | 1 | */ |
|
75 | 30 | private function setStreamOrFile($streamOrFile): void |
|
87 | |||
88 | 68 | private function setError($error): void |
|
100 | |||
101 | 66 | private function setSize($size): void |
|
109 | 16 | ||
110 | private function setClientFilename($clientFilename): void |
||
118 | |||
119 | private function setClientMediaType($clientMediaType): void |
||
127 | |||
128 | /** |
||
129 | 54 | * @return bool return true if there is no upload error |
|
130 | 54 | */ |
|
131 | private function isOk(): bool |
||
135 | 54 | ||
136 | /** |
||
137 | 54 | * @throws \RuntimeException if is moved or not ok |
|
138 | */ |
||
139 | private function validateActive(): void |
||
149 | 20 | ||
150 | 4 | public function getStream(): StreamInterface |
|
162 | |||
163 | public function moveTo($targetPath): void |
||
186 | 8 | ||
187 | public function getSize(): ?int |
||
191 | 3 | ||
192 | public function getError(): int |
||
196 | 10 | ||
197 | public function getClientFilename(): ?string |
||
201 | 3 | ||
202 | public function getClientMediaType(): ?string |
||
206 | 3 | ||
207 | /** |
||
208 | 3 | * Copy the contents of a stream into another stream until the given number |
|
209 | * of bytes have been read. |
||
210 | * |
||
211 | * @author Michael Dowling and contributors to guzzlehttp/psr7 |
||
212 | * |
||
213 | * @param StreamInterface $source Stream to read from |
||
214 | * @param StreamInterface $dest Stream to write to |
||
215 | * @param int $maxLen Maximum number of bytes to read. Pass -1 |
||
216 | * to read the entire stream |
||
217 | * |
||
218 | * @throws \RuntimeException on error |
||
219 | */ |
||
220 | private function copyToStream(StreamInterface $source, StreamInterface $dest, $maxLen = -1): void |
||
245 | } |
||
246 |
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..