From 18f7e8cf535c74b66e223d9a2cdd754e3529226c Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 19 Jun 2022 17:17:13 +0900 Subject: [PATCH] fix: is_image causes PHP 8.1 deprecated error ErrorException : mb_strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated .../CodeIgniter4/system/Validation/FileRules.php:138 --- system/Validation/FileRules.php | 2 +- tests/_support/Validation/uploads/abc77tz | 0 tests/system/Validation/FileRulesTest.php | 3 +++ 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 tests/_support/Validation/uploads/abc77tz diff --git a/system/Validation/FileRules.php b/system/Validation/FileRules.php index ea1a6f678379..2becb34b1950 100644 --- a/system/Validation/FileRules.php +++ b/system/Validation/FileRules.php @@ -133,7 +133,7 @@ public function is_image(?string $blank, string $params): bool // We know that our mimes list always has the first mime // start with `image` even when then are multiple accepted types. - $type = Mimes::guessTypeFromExtension($file->getExtension()); + $type = Mimes::guessTypeFromExtension($file->getExtension()) ?? ''; if (mb_strpos($type, 'image') !== 0) { return false; diff --git a/tests/_support/Validation/uploads/abc77tz b/tests/_support/Validation/uploads/abc77tz new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/system/Validation/FileRulesTest.php b/tests/system/Validation/FileRulesTest.php index 0a2f5356efd8..8c5ce6e142ac 100644 --- a/tests/system/Validation/FileRulesTest.php +++ b/tests/system/Validation/FileRulesTest.php @@ -41,7 +41,9 @@ final class FileRulesTest extends CIUnitTestCase protected function setUp(): void { + $this->resetServices(); parent::setUp(); + $this->validation = new Validation((object) $this->config, Services::renderer()); $this->validation->reset(); @@ -229,6 +231,7 @@ public function testIsntImage(): void 'type' => 'application/address', 'error' => UPLOAD_ERR_OK, ]; + $this->validation->setRules(['avatar' => 'is_image[stuff]']); $this->assertFalse($this->validation->run([])); }