Skip to content

Commit c3660cc

Browse files
author
Carlos Garcia
committed
Refactor UploadedFile class and add unit tests for improved functionality
1 parent 67d39e8 commit c3660cc

File tree

2 files changed

+380
-34
lines changed

2 files changed

+380
-34
lines changed

Core/UploadedFile.php

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2023-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2023-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -30,15 +30,15 @@ final class UploadedFile
3030
/** @var int */
3131
public $size;
3232

33+
/** @var bool */
34+
public $test = false;
35+
3336
/** @var string */
3437
public $tmp_name;
3538

3639
/** @var string */
3740
public $type;
3841

39-
/** @var bool */
40-
public $test = false;
41-
4242
public function __construct(array $data = [])
4343
{
4444
foreach ($data as $key => $value) {
@@ -68,39 +68,24 @@ public function getClientOriginalName(): string
6868

6969
public function getErrorMessage(): string
7070
{
71-
switch ($this->error) {
72-
case UPLOAD_ERR_INI_SIZE:
73-
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
74-
75-
case UPLOAD_ERR_FORM_SIZE:
76-
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
77-
78-
case UPLOAD_ERR_PARTIAL:
79-
return 'The uploaded file was only partially uploaded.';
80-
81-
case UPLOAD_ERR_NO_FILE:
82-
return 'No file was uploaded.';
83-
84-
case UPLOAD_ERR_NO_TMP_DIR:
85-
return 'Missing a temporary folder.';
86-
87-
case UPLOAD_ERR_CANT_WRITE:
88-
return 'Failed to write file to disk.';
89-
90-
case UPLOAD_ERR_EXTENSION:
91-
return 'A PHP extension stopped the file upload.';
92-
93-
default:
94-
return 'Unknown upload error.';
95-
}
71+
return match ($this->error) {
72+
UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
73+
UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
74+
UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
75+
UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
76+
UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
77+
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
78+
UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.',
79+
default => 'Unknown upload error.',
80+
};
9681
}
9782

9883
public static function getMaxFilesize(): int
9984
{
100-
$sizePostMax = self::parseFilesize(ini_get('post_max_size'));
101-
$sizeUploadMax = self::parseFilesize(ini_get('upload_max_filesize'));
85+
$postMax = self::parseFilesize(ini_get('post_max_size'));
86+
$uploadMax = self::parseFilesize(ini_get('upload_max_filesize'));
10287

103-
return min($sizePostMax ?: PHP_INT_MAX, $sizeUploadMax ?: PHP_INT_MAX);
88+
return min($postMax ?: PHP_INT_MAX, $uploadMax ?: PHP_INT_MAX);
10489
}
10590

10691
public function getMimeType(): string
@@ -143,7 +128,9 @@ public function move(string $destiny, string $destinyName): bool
143128
$destiny .= DIRECTORY_SEPARATOR;
144129
}
145130

146-
return $this->test ? rename($this->tmp_name, $destiny . $destinyName) : move_uploaded_file($this->tmp_name, $destiny . $destinyName);
131+
return $this->test ?
132+
rename($this->tmp_name, $destiny . $destinyName) :
133+
move_uploaded_file($this->tmp_name, $destiny . $destinyName);
147134
}
148135

149136
public function moveTo(string $targetPath): bool
@@ -152,7 +139,9 @@ public function moveTo(string $targetPath): bool
152139
return false;
153140
}
154141

155-
return $this->test ? rename($this->tmp_name, $targetPath) : move_uploaded_file($this->tmp_name, $targetPath);
142+
return $this->test ?
143+
rename($this->tmp_name, $targetPath) :
144+
move_uploaded_file($this->tmp_name, $targetPath);
156145
}
157146

158147
private static function parseFilesize(string $size): int

0 commit comments

Comments
 (0)