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 int[] */ |
||
| 17 | private static $errors = [ |
||
| 18 | UPLOAD_ERR_OK, UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, |
||
| 19 | UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION, |
||
| 20 | ]; |
||
| 21 | |||
| 22 | /** @var string */ |
||
| 23 | private $clientFilename; |
||
| 24 | |||
| 25 | /** @var string */ |
||
| 26 | private $clientMediaType; |
||
| 27 | |||
| 28 | /** @var int */ |
||
| 29 | private $error; |
||
| 30 | |||
| 31 | /** @var null|string */ |
||
| 32 | private $file; |
||
| 33 | |||
| 34 | /** @var bool */ |
||
| 35 | private $moved = false; |
||
| 36 | |||
| 37 | /** @var null|int */ |
||
| 38 | private $size; |
||
| 39 | |||
| 40 | /** @var null|StreamInterface */ |
||
| 41 | private $stream; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param StreamInterface|string|resource $streamOrFile |
||
| 45 | * @param int $size |
||
| 46 | * @param int $errorStatus |
||
| 47 | * @param string|null $clientFilename |
||
| 48 | * @param string|null $clientMediaType |
||
| 49 | */ |
||
| 50 | 75 | public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Depending on the value set file or stream variable. |
||
| 64 | * |
||
| 65 | * @param string|resource|StreamInterface $streamOrFile |
||
| 66 | * |
||
| 67 | * @throws \InvalidArgumentException |
||
| 68 | */ |
||
| 69 | 32 | private function setStreamOrFile($streamOrFile): void |
|
| 81 | |||
| 82 | 75 | private function setError($error): void |
|
| 94 | |||
| 95 | 66 | private function setSize($size): void |
|
| 103 | |||
| 104 | 66 | private function isStringOrNull($param): bool |
|
| 108 | |||
| 109 | 16 | private function isStringNotEmpty($param): bool |
|
| 113 | |||
| 114 | 66 | private function setClientFilename($clientFilename): void |
|
| 122 | |||
| 123 | 60 | private function setClientMediaType($clientMediaType): void |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @return bool Return true if there is no upload error. |
||
| 134 | */ |
||
| 135 | 54 | private function isOk(): bool |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @throws \RuntimeException if is moved or not ok |
||
| 142 | */ |
||
| 143 | 34 | private function validateActive(): void |
|
| 153 | |||
| 154 | 18 | public function getStream(): StreamInterface |
|
| 166 | |||
| 167 | 23 | public function moveTo($targetPath): void |
|
| 190 | |||
| 191 | 3 | public function getSize(): ?int |
|
| 195 | |||
| 196 | 10 | public function getError(): int |
|
| 200 | |||
| 201 | 3 | public function getClientFilename(): ?string |
|
| 205 | |||
| 206 | 3 | public function getClientMediaType(): ?string |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Copy the contents of a stream into another stream until the given number |
||
| 213 | * of bytes have been read. |
||
| 214 | * |
||
| 215 | * @author Michael Dowling and contributors to guzzlehttp/psr7 |
||
| 216 | * |
||
| 217 | * @param StreamInterface $source Stream to read from |
||
| 218 | * @param StreamInterface $dest Stream to write to |
||
| 219 | * @param int $maxLen Maximum number of bytes to read. Pass -1 |
||
| 220 | * to read the entire stream |
||
| 221 | * |
||
| 222 | * @throws \RuntimeException on error |
||
| 223 | */ |
||
| 224 | 7 | private function copyToStream(StreamInterface $source, StreamInterface $dest, $maxLen = -1) |
|
| 249 | } |
||
| 250 |
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..