From bfe7c39159e6b6ff7f5cc7e7af1a086f8fec6ef0 Mon Sep 17 00:00:00 2001 From: Roy Date: Sat, 8 Apr 2023 23:54:23 +0200 Subject: [PATCH] Prevent possible warnings on unset variables (#542) Co-authored-by: Roy --- src/ServerRequest.php | 8 ++++---- tests/ServerRequestTest.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ServerRequest.php b/src/ServerRequest.php index 43cbb502..b2aa382d 100644 --- a/src/ServerRequest.php +++ b/src/ServerRequest.php @@ -144,10 +144,10 @@ private static function normalizeNestedFileSpec(array $files = []): array foreach (array_keys($files['tmp_name']) as $key) { $spec = [ 'tmp_name' => $files['tmp_name'][$key], - 'size' => $files['size'][$key], - 'error' => $files['error'][$key], - 'name' => $files['name'][$key], - 'type' => $files['type'][$key], + 'size' => $files['size'][$key] ?? null, + 'error' => $files['error'][$key] ?? null, + 'name' => $files['name'][$key] ?? null, + 'type' => $files['type'][$key] ?? null, ]; $normalizedFiles[$key] = self::createUploadedFileFromSpec($spec); } diff --git a/tests/ServerRequestTest.php b/tests/ServerRequestTest.php index f7605120..15340f2a 100644 --- a/tests/ServerRequestTest.php +++ b/tests/ServerRequestTest.php @@ -163,6 +163,7 @@ public function dataNormalizeFiles(): iterable 'tmp_name' => [ 0 => '/tmp/php/hp9hskjhf', 1 => '/tmp/php/php1h4j1o', + 2 => '/tmp/php/w0ensl4ar', ], 'error' => [ 0 => '0', @@ -173,6 +174,11 @@ public function dataNormalizeFiles(): iterable 1 => '7349', ], ], + 'minimum_data' => [ + 'tmp_name' => [ + 0 => '/tmp/php/hp9hskjhf', + ], + ], 'nested' => [ 'name' => [ 'other' => 'Flag.txt', @@ -227,6 +233,18 @@ public function dataNormalizeFiles(): iterable 'Image.png', 'image/png' ), + 2 => new UploadedFile( + '/tmp/php/w0ensl4ar', + null, + UPLOAD_ERR_OK + ), + ], + 'minimum_data' => [ + 0 => new UploadedFile( + '/tmp/php/hp9hskjhf', + 0, + UPLOAD_ERR_OK + ), ], 'nested' => [ 'other' => new UploadedFile(