diff --git a/src/Result.php b/src/Result.php index 01271f7..ee2128a 100644 --- a/src/Result.php +++ b/src/Result.php @@ -164,9 +164,9 @@ 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 | null + public function fetchRow(int $offset, string $class = null, mixed ...$constructor) : object { $this->checkIsFree(); $this->moveCursor($offset); @@ -176,12 +176,12 @@ 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 + public function fetchArray() : array | null { $this->checkIsFree(); - return $this->result->fetch_assoc(); + return $this->result->fetch_assoc(); // @phpstan-ignore-line } /** @@ -200,13 +200,13 @@ public function fetchArrayAll() : array * * @param int $offset * - * @return array + * @return array */ public function fetchArrayRow(int $offset) : array { $this->checkIsFree(); $this->moveCursor($offset); - return $this->result->fetch_assoc(); + return $this->result->fetch_assoc(); // @phpstan-ignore-line } /** @@ -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; } } 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