Skip to content

Commit

Permalink
add exception safeguards for failure states
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Oct 11, 2024
1 parent 8a912f2 commit 1bd5c40
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Drivers/Mysqli/MysqliResultAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public function seek(int $index): void

public function fetch(): ?array
{
return $this->result->fetch_assoc();
$fetched = $this->result->fetch_assoc();
if ($fetched === false) throw new InvalidStateException();
return $fetched;
}


Expand Down
6 changes: 5 additions & 1 deletion src/Drivers/Mysqli/MysqliResultNormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Closure;
use DateInterval;
use DateTimeZone;
use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Utils\DateTimeImmutable;
use Nextras\Dbal\Utils\StrictObjectTrait;
use function date_default_timezone_get;
Expand Down Expand Up @@ -42,7 +43,10 @@ public function __construct(MysqliDriver $driver)

$this->timeNormalizer = static function($value): ?DateInterval {
if ($value === null) return null;
preg_match('#^(-?)(\d+):(\d+):(\d+)#', $value, $m);
$matched = preg_match('#^(-?)(\d+):(\d+):(\d+)#', $value, $m);
if ($matched !== 1) {
throw new InvalidArgumentException("Unsupported value format for TIME column: $value. Unable to parse to DateInterval");
}
$value = new DateInterval("PT{$m[2]}H{$m[3]}M{$m[4]}S");
$value->invert = $m[1] === '-' ? 1 : 0;
return $value;
Expand Down
6 changes: 5 additions & 1 deletion src/Drivers/PdoMysql/PdoMysqlResultNormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Closure;
use DateInterval;
use DateTimeZone;
use Nextras\Dbal\Exception\InvalidArgumentException;
use Nextras\Dbal\Utils\DateTimeImmutable;
use Nextras\Dbal\Utils\StrictObjectTrait;
use function date_default_timezone_get;
Expand Down Expand Up @@ -43,7 +44,10 @@ public function __construct(PdoMysqlDriver $driver)

$this->timeNormalizer = static function ($value): ?DateInterval {
if ($value === null) return null;
preg_match('#^(-?)(\d+):(\d+):(\d+)#', $value, $m);
$matched = preg_match('#^(-?)(\d+):(\d+):(\d+)#', $value, $m);
if ($matched !== 1) {
throw new InvalidArgumentException("Unsupported value format for TIME column: $value. Unable to parse to DateInterval");
}
$value = new DateInterval("PT{$m[2]}H{$m[3]}M{$m[4]}S");
$value->invert = $m[1] === '-' ? 1 : 0;
return $value;
Expand Down

0 comments on commit 1bd5c40

Please sign in to comment.