From 45c0839d1cc6ba3f6911ed62d931e9fd58f6b5af Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 28 Jul 2021 13:34:19 +0200 Subject: [PATCH] used native PHP 8 functions --- src/Dibi/Connection.php | 6 +++--- src/Dibi/Drivers/PostgreDriver.php | 2 +- src/Dibi/Drivers/PostgreReflector.php | 2 +- src/Dibi/Drivers/SqliteDriver.php | 14 +++++++------- src/Dibi/Event.php | 2 +- src/Dibi/Helpers.php | 6 +++--- src/Dibi/Result.php | 4 ++-- src/Dibi/Row.php | 2 +- src/Dibi/Translator.php | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 070688ce..7a4e55f8 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -501,9 +501,9 @@ public function getSubstitutes(): HashMap */ public function substitute(string $value): string { - return strpos($value, ':') === false - ? $value - : preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value); + return str_contains($value, ':') + ? preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value) + : $value; } diff --git a/src/Dibi/Drivers/PostgreDriver.php b/src/Dibi/Drivers/PostgreDriver.php index fd9aa4b3..d6701798 100644 --- a/src/Dibi/Drivers/PostgreDriver.php +++ b/src/Dibi/Drivers/PostgreDriver.php @@ -137,7 +137,7 @@ public static function createException(string $message, $code = null, string $sq $message = substr($message, strlen($m[0])); } - if ($code === '0A000' && strpos($message, 'truncate') !== false) { + if ($code === '0A000' && str_contains($message, 'truncate')) { return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); } elseif ($code === '23502') { diff --git a/src/Dibi/Drivers/PostgreReflector.php b/src/Dibi/Drivers/PostgreReflector.php index 4b4dad64..066e1394 100644 --- a/src/Dibi/Drivers/PostgreReflector.php +++ b/src/Dibi/Drivers/PostgreReflector.php @@ -125,7 +125,7 @@ public function getColumns(string $table): array 'size' => $size > 0 ? $size : null, 'nullable' => $row['is_nullable'] === 'YES' || $row['is_nullable'] === 't' || $row['is_nullable'] === true, 'default' => $row['column_default'], - 'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'] ?? '', 0, 7) === 'nextval', + 'autoincrement' => (int) $row['ordinal_position'] === $primary && str_starts_with($row['column_default'] ?? '', 'nextval'), 'vendor' => $row, ]; } diff --git a/src/Dibi/Drivers/SqliteDriver.php b/src/Dibi/Drivers/SqliteDriver.php index 46c6b88b..1a52a0a7 100644 --- a/src/Dibi/Drivers/SqliteDriver.php +++ b/src/Dibi/Drivers/SqliteDriver.php @@ -98,19 +98,19 @@ public static function createException(string $message, $code, string $sql): Dib if ($code !== 19) { return new Dibi\DriverException($message, $code, $sql); - } elseif (strpos($message, 'must be unique') !== false - || strpos($message, 'is not unique') !== false - || strpos($message, 'UNIQUE constraint failed') !== false + } elseif (str_contains($message, 'must be unique') + || str_contains($message, 'is not unique') + || str_contains($message, 'UNIQUE constraint failed') ) { return new Dibi\UniqueConstraintViolationException($message, $code, $sql); - } elseif (strpos($message, 'may not be null') !== false - || strpos($message, 'NOT NULL constraint failed') !== false + } elseif (str_contains($message, 'may not be null') + || str_contains($message, 'NOT NULL constraint failed') ) { return new Dibi\NotNullConstraintViolationException($message, $code, $sql); - } elseif (strpos($message, 'foreign key constraint failed') !== false - || strpos($message, 'FOREIGN KEY constraint failed') !== false + } elseif (str_contains($message, 'foreign key constraint failed') + || str_contains($message, 'FOREIGN KEY constraint failed') ) { return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); diff --git a/src/Dibi/Event.php b/src/Dibi/Event.php index 5e4bdf1f..225068b7 100644 --- a/src/Dibi/Event.php +++ b/src/Dibi/Event.php @@ -63,7 +63,7 @@ public function __construct(Connection $connection, int $type, string $sql = nul $dibiDir = dirname((new \ReflectionClass('dibi'))->getFileName()) . DIRECTORY_SEPARATOR; foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $row) { - if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) { + if (isset($row['file']) && is_file($row['file']) && !str_starts_with($row['file'], $dibiDir)) { $this->source = [$row['file'], (int) $row['line']]; break; } diff --git a/src/Dibi/Helpers.php b/src/Dibi/Helpers.php index b9ee46e1..6622e360 100644 --- a/src/Dibi/Helpers.php +++ b/src/Dibi/Helpers.php @@ -24,7 +24,7 @@ public static function dump(string|Result $sql = null, bool $return = false): ?s { ob_start(); if ($sql instanceof Result && PHP_SAPI === 'cli') { - $hasColors = (substr((string) getenv('TERM'), 0, 5) === 'xterm'); + $hasColors = (str_starts_with((string) getenv('TERM'), 'xterm')); $maxLen = 0; foreach ($sql as $i => $row) { if ($i === 0) { @@ -86,7 +86,7 @@ public static function dump(string|Result $sql = null, bool $return = false): ?s // syntax highlight $highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is"; if (PHP_SAPI === 'cli') { - if (substr((string) getenv('TERM'), 0, 5) === 'xterm') { + if (str_starts_with((string) getenv('TERM'), 'xterm')) { $sql = preg_replace_callback($highlighter, function (array $m) { if (!empty($m[1])) { // comment return "\033[1;30m" . $m[1] . "\033[0m"; @@ -250,7 +250,7 @@ public static function loadFromFile(Connection $connection, string $file, callab if (strtoupper(substr($s, 0, 10)) === 'DELIMITER ') { $delimiter = trim(substr($s, 10)); - } elseif (substr($ts = rtrim($s), -strlen($delimiter)) === $delimiter) { + } elseif (str_ends_with($ts = rtrim($s), $delimiter)) { $sql .= substr($ts, 0, -strlen($delimiter)); $driver->query($sql); $sql = ''; diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index e7a9b578..d1f27de5 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -230,7 +230,7 @@ final public function fetchAll(int $offset = null, int $limit = null): array */ final public function fetchAssoc(string $assoc): array { - if (strpos($assoc, ',') !== false) { + if (str_contains($assoc, ',')) { return $this->oldFetchAssoc($assoc); } @@ -485,7 +485,7 @@ private function normalize(array &$row): void $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F'; } elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) { - if ($value && substr((string) $value, 0, 7) !== '0000-00') { // '', null, false, '0000-00-00', ... + if ($value && !str_starts_with((string) $value, '0000-00')) { // '', null, false, '0000-00-00', ... $value = new DateTime($value); $row[$key] = $format ? $value->format($format) : $value; } else { diff --git a/src/Dibi/Row.php b/src/Dibi/Row.php index 770f354f..b5b6c737 100644 --- a/src/Dibi/Row.php +++ b/src/Dibi/Row.php @@ -36,7 +36,7 @@ public function asDateTime(string $key, string $format = null): DateTime|string| { $time = $this[$key]; if (!$time instanceof DateTime) { - if (!$time || substr((string) $time, 0, 7) === '0000-00') { // '', null, false, '0000-00-00', ... + if (!$time || str_starts_with((string) $time, '0000-00')) { // '', null, false, '0000-00-00', ... return null; } $time = new DateTime($time); diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index b2822988..b86dbcab 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -195,7 +195,7 @@ public function formatValue(mixed $value, ?string $modifier): string $v = $this->formatValue($v, $pair[1]); if ($pair[1] === 'l' || $pair[1] === 'in') { $op = 'IN '; - } elseif (strpos($pair[1], 'like') !== false) { + } elseif (str_contains($pair[1], 'like')) { $op = 'LIKE '; } elseif ($v === 'NULL') { $op = 'IS ';