Skip to content

Commit

Permalink
FileUpload: Initialize properties (#235)
Browse files Browse the repository at this point in the history
Co-authored-by: David Grudl <david@grudl.com>
  • Loading branch information
finwe and dg authored Nov 4, 2024
1 parent afdd2e1 commit 72a6d5a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
22 changes: 11 additions & 11 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 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)
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 @@ -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
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 72a6d5a

Please sign in to comment.