Skip to content

Commit

Permalink
FileUpload: Initialize properties (#235)(#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
finwe authored and dg committed Nov 4, 2024
1 parent b7439c6 commit c76b61e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class FileUpload
public const IMAGE_MIME_TYPES = ['image/gif', 'image/png', 'image/jpeg', 'image/webp'];

private readonly string $name;
private readonly string|null $fullPath;
private readonly ?string $fullPath;
private string|false|null $type = null;
private string|false|null $extension = null;
private readonly int $size;
Expand All @@ -46,16 +46,16 @@ public function __construct(?array $value)
{
foreach (['name', 'size', 'tmp_name', 'error'] as $key) {
if (!isset($value[$key]) || !is_scalar($value[$key])) {
$this->error = UPLOAD_ERR_NO_FILE;
return; // or throw exception?
$value = [];
break;
}
}

$this->name = $value['name'];
$this->name = $value['name'] ?? '';
$this->fullPath = $value['full_path'] ?? null;
$this->size = $value['size'];
$this->tmpName = $value['tmp_name'];
$this->error = $value['error'];
$this->size = $value['size'] ?? 0;
$this->tmpName = $value['tmp_name'] ?? '';
$this->error = $value['error'] ?? UPLOAD_ERR_NO_FILE;
}


Expand Down Expand Up @@ -174,7 +174,7 @@ public function __toString(): string


/**
* 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
Expand Down
13 changes: 13 additions & 0 deletions tests/Http/FileUpload.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,17 @@ test('', function () {
Assert::null($upload->getContentType());
Assert::false($upload->isImage());
Assert::null($upload->getSuggestedExtension());
Assert::same('', (string) $upload);
});


test('', function () {
$upload = new FileUpload([]);

Assert::false($upload->isOk());
Assert::false($upload->hasFile());
Assert::null($upload->getContentType());
Assert::false($upload->isImage());
Assert::null($upload->getSuggestedExtension());
Assert::same('', (string) $upload);
});

0 comments on commit c76b61e

Please sign in to comment.