Skip to content

Commit 2c298b0

Browse files
finwedg
authored andcommitted
FileUpload: Initialize properties (#235)(#195)
1 parent 64b51a5 commit 2c298b0

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/Http/FileUpload.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class FileUpload
3434
public const IMAGE_MIME_TYPES = ['image/gif', 'image/png', 'image/jpeg', 'image/webp'];
3535

3636
private readonly string $name;
37-
private readonly string|null $fullPath;
37+
private readonly ?string $fullPath;
3838
private string|false|null $type = null;
3939
private string|false|null $extension = null;
4040
private readonly int $size;
@@ -46,16 +46,16 @@ public function __construct(?array $value)
4646
{
4747
foreach (['name', 'size', 'tmp_name', 'error'] as $key) {
4848
if (!isset($value[$key]) || !is_scalar($value[$key])) {
49-
$this->error = UPLOAD_ERR_NO_FILE;
50-
return; // or throw exception?
49+
$value = [];
50+
break;
5151
}
5252
}
5353

54-
$this->name = $value['name'];
54+
$this->name = $value['name'] ?? '';
5555
$this->fullPath = $value['full_path'] ?? null;
56-
$this->size = $value['size'];
57-
$this->tmpName = $value['tmp_name'];
58-
$this->error = $value['error'];
56+
$this->size = $value['size'] ?? 0;
57+
$this->tmpName = $value['tmp_name'] ?? '';
58+
$this->error = $value['error'] ?? UPLOAD_ERR_NO_FILE;
5959
}
6060

6161

@@ -174,7 +174,7 @@ public function __toString(): string
174174

175175

176176
/**
177-
* Returns the error code. It is be one of UPLOAD_ERR_XXX constants.
177+
* Returns the error code. It has to be one of UPLOAD_ERR_XXX constants.
178178
* @see http://php.net/manual/en/features.file-upload.errors.php
179179
*/
180180
public function getError(): int

tests/Http/FileUpload.basic.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,17 @@ test('', function () {
7171
Assert::null($upload->getContentType());
7272
Assert::false($upload->isImage());
7373
Assert::null($upload->getSuggestedExtension());
74+
Assert::same('', (string) $upload);
75+
});
76+
77+
78+
test('', function () {
79+
$upload = new FileUpload([]);
80+
81+
Assert::false($upload->isOk());
82+
Assert::false($upload->hasFile());
83+
Assert::null($upload->getContentType());
84+
Assert::false($upload->isImage());
85+
Assert::null($upload->getSuggestedExtension());
86+
Assert::same('', (string) $upload);
7487
});

0 commit comments

Comments
 (0)