From fa6a1203a937de0ced0b23f2bb4729360be0ce24 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 15 Oct 2020 16:50:37 +0200 Subject: [PATCH] fixed compatibility with PHP 8 [Closes #379] --- src/Dibi/Drivers/OdbcDriver.php | 6 +++--- src/Dibi/Translator.php | 10 ++++++---- tests/dibi/Result.normalize.phpt | 9 ++++++++- tests/dibi/Translator.DateTimeInterface.phpt | 4 ---- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Dibi/Drivers/OdbcDriver.php b/src/Dibi/Drivers/OdbcDriver.php index 6af92a73..6049966d 100644 --- a/src/Dibi/Drivers/OdbcDriver.php +++ b/src/Dibi/Drivers/OdbcDriver.php @@ -124,7 +124,7 @@ class OdbcDriver implements Dibi\Driver */ public function begin(string $savepoint = null): void { - if (!odbc_autocommit($this->connection, 0/*false*/)) { + if (!odbc_autocommit($this->connection, PHP_VERSION_ID < 80000 ? 0 : false)) { throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); } } @@ -139,7 +139,7 @@ class OdbcDriver implements Dibi\Driver if (!odbc_commit($this->connection)) { throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); } - odbc_autocommit($this->connection, 1/*true*/); + odbc_autocommit($this->connection, PHP_VERSION_ID < 80000 ? 1 : true); } @@ -152,7 +152,7 @@ class OdbcDriver implements Dibi\Driver if (!odbc_rollback($this->connection)) { throw new Dibi\DriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); } - odbc_autocommit($this->connection, 1/*true*/); + odbc_autocommit($this->connection, PHP_VERSION_ID < 80000 ? 1 : true); } diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index 9320a103..a68613b7 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -290,7 +290,7 @@ XX if (is_array($v)) { $vx[] = $this->formatValue($v, 'ex'); } elseif (is_string($k)) { - $v = (is_string($v) && strncasecmp($v, 'd', 1)) || $v > 0 ? 'ASC' : 'DESC'; + $v = (is_string($v) ? strncasecmp($v, 'd', 1) : $v > 0) ? 'ASC' : 'DESC'; $vx[] = $this->identifiers->$k . ' ' . $v; } else { $vx[] = $this->identifiers->$v; @@ -343,11 +343,13 @@ XX case 'sN': // string or null case 'sn': - return $value == '' ? 'NULL' : $this->driver->escapeText((string) $value); // notice two equal signs + return $value === '' || $value === 0 || $value === null + ? 'NULL' + : $this->driver->escapeText((string) $value); case 'iN': // signed int or null - if ($value == '') { - $value = null; + if ($value === '' || $value === 0 || $value === null) { + return 'NULL'; } // break omitted case 'i': // signed int diff --git a/tests/dibi/Result.normalize.phpt b/tests/dibi/Result.normalize.phpt index 3e00f040..5a193949 100644 --- a/tests/dibi/Result.normalize.phpt +++ b/tests/dibi/Result.normalize.phpt @@ -147,7 +147,14 @@ test('', function () { Assert::same(['col' => 1], $result->test(['col' => true])); Assert::same(['col' => 0], $result->test(['col' => false])); - Assert::same(['col' => 0], @$result->test(['col' => ''])); // triggers warning in PHP 7.1 + if (PHP_VERSION_ID < 80000) { + Assert::same(['col' => 0], @$result->test(['col' => ''])); // triggers warning since PHP 7.1 + } else { + Assert::exception(function () use ($result) { + Assert::same(['col' => 0], $result->test(['col' => ''])); + }, TypeError::class); + } + Assert::same(['col' => 0], $result->test(['col' => '0'])); Assert::same(['col' => 1], $result->test(['col' => '1'])); Assert::same(['col' => 10], $result->test(['col' => '10'])); diff --git a/tests/dibi/Translator.DateTimeInterface.phpt b/tests/dibi/Translator.DateTimeInterface.phpt index 99094214..8df5f781 100644 --- a/tests/dibi/Translator.DateTimeInterface.phpt +++ b/tests/dibi/Translator.DateTimeInterface.phpt @@ -1,9 +1,5 @@