From cb08cc73f2ee79211ecbd16835c1fc130749cc9d Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Tue, 13 Aug 2024 18:39:42 -0300 Subject: [PATCH 1/5] Update method signatures --- src/Result.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Result.php b/src/Result.php index 01271f7..260fdf0 100644 --- a/src/Result.php +++ b/src/Result.php @@ -166,7 +166,7 @@ public function fetchAll(string $class = null, mixed ...$constructor) : array * * @return object|null */ - public function fetchRow(int $offset, string $class = null, mixed ...$constructor) : object | null + public function fetchRow(int $offset, string $class = null, mixed ...$constructor) : object { $this->checkIsFree(); $this->moveCursor($offset); @@ -178,7 +178,7 @@ public function fetchRow(int $offset, string $class = null, mixed ...$constructo * * @return array|null */ - public function fetchArray() : ?array + public function fetchArray() : array | null { $this->checkIsFree(); return $this->result->fetch_assoc(); From 95ba9776c3592eba8fdcdda31952b4eddd2a7150 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Tue, 13 Aug 2024 18:40:41 -0300 Subject: [PATCH 2/5] Update Result::fetchFields method --- src/Result.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Result.php b/src/Result.php index 260fdf0..53f54cc 100644 --- a/src/Result.php +++ b/src/Result.php @@ -229,10 +229,10 @@ public function numRows() : int | string public function fetchFields() : array { $this->checkIsFree(); - $fields = $this->result->fetch_fields(); - foreach ($fields as &$field) { - $field = new Field($field); + $fields = []; + foreach ($this->result->fetch_fields() as $field) { + $fields[] = new Field($field); } - return $fields; // @phpstan-ignore-line + return $fields; } } From 067dcf4b375c6dd795804104aa18f56438d164e7 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Tue, 13 Aug 2024 18:41:50 -0300 Subject: [PATCH 3/5] Ignore phpstan errors --- src/Result.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Result.php b/src/Result.php index 53f54cc..5f781b4 100644 --- a/src/Result.php +++ b/src/Result.php @@ -181,7 +181,7 @@ public function fetchRow(int $offset, string $class = null, mixed ...$constructo public function fetchArray() : array | null { $this->checkIsFree(); - return $this->result->fetch_assoc(); + return $this->result->fetch_assoc(); // @phpstan-ignore-line } /** @@ -206,7 +206,7 @@ public function fetchArrayRow(int $offset) : array { $this->checkIsFree(); $this->moveCursor($offset); - return $this->result->fetch_assoc(); + return $this->result->fetch_assoc(); // @phpstan-ignore-line } /** From 229ea27f5ce428619e5ed1bb1ac4c7180e5ef614 Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Tue, 13 Aug 2024 18:42:19 -0300 Subject: [PATCH 4/5] Update phpdocs --- src/Result.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Result.php b/src/Result.php index 5f781b4..ee2128a 100644 --- a/src/Result.php +++ b/src/Result.php @@ -164,7 +164,7 @@ public function fetchAll(string $class = null, mixed ...$constructor) : array * @param string|null $class * @param mixed ...$constructor * - * @return object|null + * @return object */ public function fetchRow(int $offset, string $class = null, mixed ...$constructor) : object { @@ -176,7 +176,7 @@ public function fetchRow(int $offset, string $class = null, mixed ...$constructo /** * Fetches the current row as array and move the cursor to the next. * - * @return array|null + * @return array|null */ public function fetchArray() : array | null { @@ -200,7 +200,7 @@ public function fetchArrayAll() : array * * @param int $offset * - * @return array + * @return array */ public function fetchArrayRow(int $offset) : array { From 94d99b1fc651835939d5eaa0ee9911b25bce498c Mon Sep 17 00:00:00 2001 From: Natan Felles Date: Tue, 13 Aug 2024 18:47:03 -0300 Subject: [PATCH 5/5] Test Result::fetch{Row,ArrayRow} methods --- tests/ResultTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/ResultTest.php b/tests/ResultTest.php index 83860a5..fa910e1 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -94,6 +94,9 @@ public function testFetchRow() : void $result = static::$database->query('SELECT * FROM `t1`'); self::assertSame(1, $result->fetchRow(0)->c1); self::assertSame(4, $result->fetchRow(3)->c1); + $this->expectException(\OutOfRangeException::class); + $this->expectExceptionMessage('Invalid cursor offset: 5'); + $result->fetchRow(5); } public function testFetchRowFree() : void @@ -106,6 +109,9 @@ public function testFetchArrayRow() : void $result = static::$database->query('SELECT * FROM `t1`'); self::assertSame(1, $result->fetchArrayRow(0)['c1']); self::assertSame(4, $result->fetchArrayRow(3)['c1']); + $this->expectException(\OutOfRangeException::class); + $this->expectExceptionMessage('Invalid cursor offset: 5'); + $result->fetchArrayRow(5); } public function testFetchArrayRowFree() : void