Skip to content

Commit 83b6fdf

Browse files
committed
refactor: Clean up code formatting and improve import statements in Input and InputQuery classes
1 parent 5c4f56b commit 83b6fdf

File tree

4 files changed

+59
-58
lines changed

4 files changed

+59
-58
lines changed

src/Attribute/Input.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
#[Attribute(Attribute::TARGET_PARAMETER)]
1010
final class Input
1111
{
12-
/**
13-
* @param string|null $item Class name for array items
14-
*/
12+
/** @param string|null $item Class name for array items */
1513
public function __construct(
1614
public readonly string|null $item = null,
1715
) {

src/InputQuery.php

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use ArrayObject;
88
use InvalidArgumentException;
9+
use Koriym\FileUpload\FileUpload;
910
use Override;
1011
use Ray\Di\Di\Named;
1112
use Ray\Di\Di\Qualifier;
@@ -22,6 +23,7 @@
2223
use function array_key_exists;
2324
use function assert;
2425
use function class_exists;
26+
use function count;
2527
use function gettype;
2628
use function is_array;
2729
use function is_bool;
@@ -40,6 +42,8 @@
4042
use function substr;
4143
use function ucwords;
4244

45+
use const UPLOAD_ERR_NO_FILE;
46+
4347
/**
4448
* @template T of object
4549
* @implements InputQueryInterface<T>
@@ -66,7 +70,6 @@ public function getArguments(ReflectionMethod $method, array $query): array
6670
return $args;
6771
}
6872

69-
7073
/**
7174
* @param class-string<T> $class
7275
* @param array<string, mixed> $query
@@ -88,7 +91,6 @@ public function create(string $class, array $query): object
8891
return $reflection->newInstanceArgs($args);
8992
}
9093

91-
9294
/** @param array<string, mixed> $query */
9395
private function resolveParameter(ReflectionParameter $param, array $query): mixed
9496
{
@@ -112,7 +114,6 @@ private function resolveInputParameter(ReflectionParameter $param, array $query,
112114
$type = $param->getType();
113115
$paramName = $param->getName();
114116

115-
116117
// Handle union types (e.g., FileUpload|ErrorFileUpload)
117118
if ($type instanceof ReflectionUnionType) {
118119
return $this->resolveUnionType($param, $query, $inputAttributes, $type);
@@ -397,49 +398,49 @@ private function isFileUploadType(string $className): bool
397398
if (! class_exists('Koriym\FileUpload\FileUpload')) {
398399
return false;
399400
}
400-
401-
return $className === 'Koriym\FileUpload\FileUpload'
401+
402+
return $className === 'Koriym\FileUpload\FileUpload'
402403
|| $className === 'Koriym\FileUpload\ErrorFileUpload'
403-
|| is_subclass_of($className, \Koriym\FileUpload\FileUpload::class);
404+
|| is_subclass_of($className, FileUpload::class);
404405
}
405406

406-
/**
407-
* @param array<string, mixed> $query
408-
*/
407+
/** @param array<string, mixed> $query */
409408
private function resolveFileUpload(ReflectionParameter $param, array $query): mixed
410409
{
411410
$paramName = $param->getName();
412-
411+
413412
// Check if FileUpload is provided in query (for testing)
414413
if (array_key_exists($paramName, $query)) {
415414
return $query[$paramName];
416415
}
417-
416+
418417
// Try to create from $_FILES
419418
if (isset($_FILES[$paramName])) {
420419
$fileData = $_FILES[$paramName];
421-
420+
422421
// Check if no file was uploaded (UPLOAD_ERR_NO_FILE)
423422
if (isset($fileData['error']) && $fileData['error'] === UPLOAD_ERR_NO_FILE) {
424423
if ($param->allowsNull() || $param->isDefaultValueAvailable()) {
425424
return $param->getDefaultValue();
426425
}
426+
427427
throw new InvalidArgumentException("Required file parameter '{$paramName}' is missing");
428428
}
429-
430-
return \Koriym\FileUpload\FileUpload::create($fileData);
429+
430+
return FileUpload::create($fileData);
431431
}
432-
432+
433433
// No file found
434434
if ($param->allowsNull() || $param->isDefaultValueAvailable()) {
435435
return $param->getDefaultValue();
436436
}
437-
437+
438438
throw new InvalidArgumentException("Required file parameter '{$paramName}' is missing");
439439
}
440440

441441
/**
442442
* @param array<string, mixed> $query
443+
*
443444
* @return array<array-key, mixed>
444445
*/
445446
private function createArrayOfFileUploads(string $paramName, array $query): array
@@ -448,15 +449,15 @@ private function createArrayOfFileUploads(string $paramName, array $query): arra
448449
if (array_key_exists($paramName, $query) && is_array($query[$paramName])) {
449450
return $query[$paramName];
450451
}
451-
452+
452453
// Try to create from $_FILES
453-
if (!isset($_FILES[$paramName])) {
454+
if (! isset($_FILES[$paramName])) {
454455
return [];
455456
}
456457

457458
$arrayData = $_FILES[$paramName];
458-
459-
if (!is_array($arrayData)) {
459+
460+
if (! is_array($arrayData)) {
460461
return [];
461462
}
462463

@@ -467,9 +468,9 @@ private function createArrayOfFileUploads(string $paramName, array $query): arra
467468

468469
// Handle regular array format (each element is a complete file array)
469470
$result = [];
470-
471+
471472
foreach ($arrayData as $key => $fileData) {
472-
if (!is_array($fileData)) {
473+
if (! is_array($fileData)) {
473474
throw new InvalidArgumentException(
474475
sprintf(
475476
'Expected array for file upload at key "%s", got %s.',
@@ -484,26 +485,28 @@ private function createArrayOfFileUploads(string $paramName, array $query): arra
484485
continue;
485486
}
486487

487-
$result[$key] = \Koriym\FileUpload\FileUpload::create($fileData);
488+
$result[$key] = FileUpload::create($fileData);
488489
}
489490

490491
return $result;
491492
}
492493

493494
/**
494495
* Convert HTML multiple file upload format to individual file arrays
496+
*
495497
* @param array<string, mixed> $multipleFileData
498+
*
496499
* @return array<array-key, mixed>
497500
*/
498501
private function convertMultipleFileFormat(array $multipleFileData): array
499502
{
500-
if (!isset($multipleFileData['name']) || !is_array($multipleFileData['name'])) {
503+
if (! isset($multipleFileData['name']) || ! is_array($multipleFileData['name'])) {
501504
return [];
502505
}
503-
506+
504507
$result = [];
505508
$fileCount = count($multipleFileData['name']);
506-
509+
507510
for ($i = 0; $i < $fileCount; $i++) {
508511
$fileData = [
509512
'name' => $multipleFileData['name'][$i] ?? '',
@@ -512,15 +515,15 @@ private function convertMultipleFileFormat(array $multipleFileData): array
512515
'tmp_name' => isset($multipleFileData['tmp_name']) && is_array($multipleFileData['tmp_name']) ? ($multipleFileData['tmp_name'][$i] ?? '') : '',
513516
'error' => isset($multipleFileData['error']) && is_array($multipleFileData['error']) ? ($multipleFileData['error'][$i] ?? UPLOAD_ERR_NO_FILE) : UPLOAD_ERR_NO_FILE,
514517
];
515-
518+
516519
// Skip files that weren't uploaded
517520
if ($fileData['error'] === UPLOAD_ERR_NO_FILE) {
518521
continue;
519522
}
520-
521-
$result[$i] = \Koriym\FileUpload\FileUpload::create($fileData);
523+
524+
$result[$i] = FileUpload::create($fileData);
522525
}
523-
526+
524527
return $result;
525528
}
526529

@@ -540,6 +543,7 @@ private function resolveUnionType(ReflectionParameter $param, array $query, arra
540543

541544
// Not a FileUpload union type, handle as regular parameter
542545
$paramName = $param->getName();
546+
543547
return $query[$paramName] ?? $this->getDefaultValue($param);
544548
}
545549
}

src/InputQueryInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ interface InputQueryInterface
1818
*/
1919
public function getArguments(ReflectionMethod $method, array $query): array;
2020

21-
2221
/**
2322
* Create object from query data
2423
*
@@ -28,5 +27,4 @@ public function getArguments(ReflectionMethod $method, array $query): array;
2827
* @return T
2928
*/
3029
public function create(string $class, array $query): object;
31-
3230
}

tests/FileUploadTest.php

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
use Ray\InputQuery\Fake\FileUploadWithOptionsInput;
1313
use Ray\InputQuery\Fake\OptionalFileUploadInput;
1414

15+
use const UPLOAD_ERR_OK;
16+
1517
class FileUploadTest extends TestCase
1618
{
1719
private InputQuery $inputQuery;
18-
20+
1921
protected function setUp(): void
2022
{
2123
$this->inputQuery = new InputQuery(new Injector());
2224
}
23-
25+
2426
public function testFileUploadIntegration(): void
2527
{
2628
// Create mock FileUpload
@@ -31,22 +33,22 @@ public function testFileUploadIntegration(): void
3133
'tmp_name' => '/tmp/php_upload_test',
3234
'error' => UPLOAD_ERR_OK,
3335
]);
34-
36+
3537
// Pass FileUpload directly in query
3638
$query = [
3739
'name' => 'Jingu',
38-
'avatar' => $mockAvatar
40+
'avatar' => $mockAvatar,
3941
];
40-
42+
4143
$result = $this->inputQuery->create(FileUploadInput::class, $query);
42-
44+
4345
$this->assertSame('Jingu', $result->name);
4446
$this->assertSame($mockAvatar, $result->avatar);
4547
$this->assertSame('test-avatar.jpg', $result->avatar->name);
4648
$this->assertSame('image/jpeg', $result->avatar->type);
4749
$this->assertSame(1024, $result->avatar->size);
4850
}
49-
51+
5052
public function testFileUploadWithValidationOptions(): void
5153
{
5254
$mockAvatar = FileUpload::create([
@@ -56,32 +58,32 @@ public function testFileUploadWithValidationOptions(): void
5658
'tmp_name' => '/tmp/php_upload_test2',
5759
'error' => UPLOAD_ERR_OK,
5860
]);
59-
61+
6062
$query = [
6163
'name' => 'Horikawa',
62-
'avatar' => $mockAvatar
64+
'avatar' => $mockAvatar,
6365
];
64-
66+
6567
$result = $this->inputQuery->create(FileUploadWithOptionsInput::class, $query);
66-
68+
6769
$this->assertSame('Horikawa', $result->name);
6870
$this->assertSame($mockAvatar, $result->avatar);
6971
$this->assertSame('test-image.png', $result->avatar->name);
7072
}
71-
73+
7274
public function testOptionalFileUpload(): void
7375
{
7476
$query = [
7577
'name' => 'Test User',
76-
'banner' => null
78+
'banner' => null,
7779
];
78-
80+
7981
$result = $this->inputQuery->create(OptionalFileUploadInput::class, $query);
80-
82+
8183
$this->assertSame('Test User', $result->name);
8284
$this->assertNull($result->banner);
8385
}
84-
86+
8587
public function testFileUploadArray(): void
8688
{
8789
$mockImage1 = FileUpload::create([
@@ -91,28 +93,27 @@ public function testFileUploadArray(): void
9193
'tmp_name' => '/tmp/php_upload_1',
9294
'error' => UPLOAD_ERR_OK,
9395
]);
94-
96+
9597
$mockImage2 = FileUpload::create([
9698
'name' => 'image2.png',
9799
'type' => 'image/png',
98100
'size' => 2048,
99101
'tmp_name' => '/tmp/php_upload_2',
100102
'error' => UPLOAD_ERR_OK,
101103
]);
102-
104+
103105
$query = [
104106
'title' => 'Gallery',
105-
'images' => [$mockImage1, $mockImage2]
107+
'images' => [$mockImage1, $mockImage2],
106108
];
107-
109+
108110
$result = $this->inputQuery->create(FileUploadArrayInput::class, $query);
109-
111+
110112
$this->assertSame('Gallery', $result->title);
111113
$this->assertCount(2, $result->images);
112114
$this->assertSame($mockImage1, $result->images[0]);
113115
$this->assertSame($mockImage2, $result->images[1]);
114116
$this->assertSame('image1.jpg', $result->images[0]->name);
115117
$this->assertSame('image2.png', $result->images[1]->name);
116118
}
117-
118-
}
119+
}

0 commit comments

Comments
 (0)