From 4f52684bc0f503d85aa1114bebb4a2e5f88ea37d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Hump=C3=A1l?= Date: Wed, 6 Mar 2024 13:58:46 +0100 Subject: [PATCH] Initialize properties of FileUpload class --- src/Http/FileUpload.php | 20 ++++++++++---------- tests/Http/FileUpload.basic.phpt | 3 +++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Http/FileUpload.php b/src/Http/FileUpload.php index 9f5fa92c..7042a518 100644 --- a/src/Http/FileUpload.php +++ b/src/Http/FileUpload.php @@ -33,13 +33,13 @@ final class FileUpload /** @deprecated */ public const IMAGE_MIME_TYPES = ['image/gif', 'image/png', 'image/jpeg', 'image/webp']; - private string $name; + private ?string $name = null; private string|null $fullPath; private string|false|null $type = null; private string|false|null $extension = null; - private int $size; - private string $tmpName; - private int $error; + private int $size = 0; + private ?string $tmpName = null; + private ?int $error; public function __construct(?array $value) @@ -51,11 +51,11 @@ public function __construct(?array $value) } } - $this->name = $value['name']; + $this->name = $value['name'] ?? null; $this->fullPath = $value['full_path'] ?? null; - $this->size = $value['size']; - $this->tmpName = $value['tmp_name']; - $this->error = $value['error']; + $this->size = isset($value['size']) ? (int) $value['size'] : 0; + $this->tmpName = $value['tmp_name'] ?? null; + $this->error = $value['error'] ?? null; } @@ -169,12 +169,12 @@ public function getTemporaryFile(): string */ public function __toString(): string { - return $this->tmpName; + return $this->tmpName ?? ''; } /** - * Returns the error code. It is be one of UPLOAD_ERR_XXX constants. + * Returns the error code. It has to be one of UPLOAD_ERR_XXX constants. * @see http://php.net/manual/en/features.file-upload.errors.php */ public function getError(): int diff --git a/tests/Http/FileUpload.basic.phpt b/tests/Http/FileUpload.basic.phpt index 505774dc..dd9f2513 100644 --- a/tests/Http/FileUpload.basic.phpt +++ b/tests/Http/FileUpload.basic.phpt @@ -29,6 +29,7 @@ test('', function () { Assert::same(209, $upload->getSize()); Assert::same(__DIR__ . '/files/file.txt', $upload->getTemporaryFile()); Assert::same(__DIR__ . '/files/file.txt', (string) $upload); + Assert::same(__DIR__ . '/files/file.txt', $upload->__toString()); Assert::same(0, $upload->getError()); Assert::true($upload->isOk()); Assert::true($upload->hasFile()); @@ -71,4 +72,6 @@ test('', function () { Assert::null($upload->getContentType()); Assert::false($upload->isImage()); Assert::null($upload->getSuggestedExtension()); + Assert::same('', (string) $upload); + Assert::same('', $upload->__toString()); });