From 72a6d5a177accc010a4f1258355f6ed34dacb398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Hump=C3=A1l?= Date: Mon, 4 Nov 2024 14:08:43 +0100 Subject: [PATCH] FileUpload: Initialize properties (#235) Co-authored-by: David Grudl --- src/Http/FileUpload.php | 22 +++++++++++----------- tests/Http/FileUpload.basic.phpt | 3 +++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Http/FileUpload.php b/src/Http/FileUpload.php index 5d4dadef..87080487 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 readonly string $name; - private readonly string|null $fullPath; + private ?string $name = null; + private string|null $fullPath; private string|false|null $type = null; private string|false|null $extension = null; - private readonly int $size; - private string $tmpName; - private readonly 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; } @@ -170,12 +170,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 c10a065e..9391b49f 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()); });