Skip to content

Commit

Permalink
Initialize properties of FileUpload class
Browse files Browse the repository at this point in the history
  • Loading branch information
finwe committed Mar 6, 2024
1 parent c779293 commit 4f52684
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}


Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/Http/FileUpload.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
});

0 comments on commit 4f52684

Please sign in to comment.