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 | * @param StreamInterface|string|resource $streamOrFile |
||
| 51 | * @param int $size |
||
| 52 | * @param int $errorStatus |
||
| 53 | * @param string|null $clientFilename |
||
| 54 | * @param string|null $clientMediaType |
||
| 55 | */ |
||
| 56 | 75 | public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null) |
|
| 57 | { |
||
| 58 | 75 | $this->setError($errorStatus); |
|
| 59 | 66 | $this->setSize($size); |
|
| 60 | 66 | $this->setClientFilename($clientFilename); |
|
| 61 | 60 | $this->setClientMediaType($clientMediaType); |
|
| 62 | |||
| 63 | 54 | if ($this->isOk()) { |
|
| 64 | 32 | $this->setStreamOrFile($streamOrFile); |
|
| 65 | } |
||
| 66 | 47 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Depending on the value set file or stream variable. |
||
| 70 | * |
||
| 71 | * @param string|resource|StreamInterface $streamOrFile |
||
| 72 | * |
||
| 73 | * @throws \InvalidArgumentException |
||
| 74 | */ |
||
| 75 | 32 | private function setStreamOrFile($streamOrFile): void |
|
| 76 | { |
||
| 77 | 32 | if (\is_string($streamOrFile)) { |
|
| 78 | 1 | $this->file = $streamOrFile; |
|
| 79 | 31 | } elseif (\is_resource($streamOrFile)) { |
|
| 80 | 1 | $this->stream = Stream::create($streamOrFile); |
|
|
|
|||
| 81 | 30 | } elseif ($streamOrFile instanceof StreamInterface) { |
|
| 82 | 23 | $this->stream = $streamOrFile; |
|
| 83 | } else { |
||
| 84 | 7 | throw new \InvalidArgumentException('Invalid stream or file provided for UploadedFile'); |
|
| 85 | } |
||
| 86 | 25 | } |
|
| 87 | |||
| 88 | 75 | private function setError($error): void |
|
| 89 | { |
||
| 90 | 75 | if (false === \is_int($error)) { |
|
| 91 | 7 | throw new \InvalidArgumentException('Upload file error status must be an integer'); |
|
| 92 | } |
||
| 93 | |||
| 94 | 68 | if (!isset(self::ERRORS[$error])) { |
|
| 95 | 2 | throw new \InvalidArgumentException('Invalid error status for UploadedFile'); |
|
| 96 | } |
||
| 97 | |||
| 98 | 66 | $this->error = $error; |
|
| 99 | 66 | } |
|
| 100 | |||
| 101 | 66 | private function setSize($size): void |
|
| 102 | { |
||
| 103 | 66 | if (false === \is_int($size)) { |
|
| 104 | throw new \InvalidArgumentException('Upload file size must be an integer'); |
||
| 105 | } |
||
| 106 | |||
| 107 | 66 | $this->size = $size; |
|
| 108 | 66 | } |
|
| 109 | |||
| 110 | 66 | private function setClientFilename($clientFilename): void |
|
| 111 | { |
||
| 112 | 66 | if ($clientFilename !== null && !\is_string($clientFilename)) { |
|
| 113 | 6 | throw new \InvalidArgumentException('Upload file client filename must be a string or null'); |
|
| 114 | } |
||
| 115 | |||
| 116 | 60 | $this->clientFilename = $clientFilename; |
|
| 117 | 60 | } |
|
| 118 | |||
| 119 | 60 | private function setClientMediaType($clientMediaType): void |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @return bool return true if there is no upload error |
||
| 130 | */ |
||
| 131 | 54 | private function isOk(): bool |
|
| 132 | { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @throws \RuntimeException if is moved or not ok |
||
| 138 | */ |
||
| 139 | 34 | private function validateActive(): void |
|
| 149 | |||
| 150 | 18 | public function getStream(): StreamInterface |
|
| 162 | |||
| 163 | 23 | public function moveTo($targetPath): void |
|
| 186 | |||
| 187 | 3 | public function getSize(): ?int |
|
| 191 | |||
| 192 | 10 | public function getError(): int |
|
| 196 | |||
| 197 | 3 | public function getClientFilename(): ?string |
|
| 201 | |||
| 202 | 3 | public function getClientMediaType(): ?string |
|
| 206 | |||
| 207 | /** |
||
| 208 | * 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 | 7 | 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..