From 568b7c98608c3883d78a349bf016c41fb8f10dfb Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 28 Jul 2021 12:48:44 +0200 Subject: [PATCH] used native PHP 8 functions --- src/Database/Drivers/PgSqlDriver.php | 2 +- src/Database/Drivers/SqliteDriver.php | 14 +++++++------- src/Database/ResultSet.php | 2 +- src/Database/SqlPreprocessor.php | 2 +- src/Database/Table/Selection.php | 4 ++-- src/Database/Table/SqlBuilder.php | 6 +++--- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Database/Drivers/PgSqlDriver.php b/src/Database/Drivers/PgSqlDriver.php index 7368a8b58..75d9aec28 100644 --- a/src/Database/Drivers/PgSqlDriver.php +++ b/src/Database/Drivers/PgSqlDriver.php @@ -31,7 +31,7 @@ public function initialize(Nette\Database\Connection $connection, array $options public function convertException(\PDOException $e): Nette\Database\DriverException { $code = $e->errorInfo[0] ?? null; - if ($code === '0A000' && strpos($e->getMessage(), 'truncate') !== false) { + if ($code === '0A000' && str_contains($e->getMessage(), 'truncate')) { return Nette\Database\ForeignKeyConstraintViolationException::from($e); } elseif ($code === '23502') { diff --git a/src/Database/Drivers/SqliteDriver.php b/src/Database/Drivers/SqliteDriver.php index 2c0830ce6..72814fc32 100644 --- a/src/Database/Drivers/SqliteDriver.php +++ b/src/Database/Drivers/SqliteDriver.php @@ -40,21 +40,21 @@ public function convertException(\PDOException $e): Nette\Database\DriverExcepti return Nette\Database\DriverException::from($e); } elseif ( - strpos($msg, 'must be unique') !== false - || strpos($msg, 'is not unique') !== false - || strpos($msg, 'UNIQUE constraint failed') !== false + str_contains($msg, 'must be unique') + || str_contains($msg, 'is not unique') + || str_contains($msg, 'UNIQUE constraint failed') ) { return Nette\Database\UniqueConstraintViolationException::from($e); } elseif ( - strpos($msg, 'may not be null') !== false - || strpos($msg, 'NOT NULL constraint failed') !== false + str_contains($msg, 'may not be null') + || str_contains($msg, 'NOT NULL constraint failed') ) { return Nette\Database\NotNullConstraintViolationException::from($e); } elseif ( - strpos($msg, 'foreign key constraint failed') !== false - || strpos($msg, 'FOREIGN KEY constraint failed') !== false + str_contains($msg, 'foreign key constraint failed') + || str_contains($msg, 'FOREIGN KEY constraint failed') ) { return Nette\Database\ForeignKeyConstraintViolationException::from($e); diff --git a/src/Database/ResultSet.php b/src/Database/ResultSet.php index 74a964e5d..1e4f2b83c 100644 --- a/src/Database/ResultSet.php +++ b/src/Database/ResultSet.php @@ -52,7 +52,7 @@ public function __construct(Connection $connection, string $queryString, array $ $this->normalizer = $normalizer; try { - if (substr($queryString, 0, 2) === '::') { + if (str_starts_with($queryString, '::')) { $connection->getPdo()->{substr($queryString, 2)}(); } elseif ($queryString !== null) { static $types = ['boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT, diff --git a/src/Database/SqlPreprocessor.php b/src/Database/SqlPreprocessor.php index 51b42f8e6..ea72ae5e9 100644 --- a/src/Database/SqlPreprocessor.php +++ b/src/Database/SqlPreprocessor.php @@ -244,7 +244,7 @@ private function formatValue($value, string $mode = null): string foreach ($value as $k => $v) { if (is_int($k)) { // value, value, ... $vx[] = $this->formatValue($v); - } elseif (substr($k, -1) === '=') { // key+=value, key-=value, ... + } elseif (str_ends_with($k, '=')) { // key+=value, key-=value, ... $k2 = $this->delimite(substr($k, 0, -2)); $vx[] = $k2 . '=' . $k2 . ' ' . substr($k, -2, 1) . ' ' . $this->formatValue($v); } else { // key=value, key=value, ... diff --git a/src/Database/Table/Selection.php b/src/Database/Table/Selection.php index 1c2320269..5a9d9c64e 100644 --- a/src/Database/Table/Selection.php +++ b/src/Database/Table/Selection.php @@ -351,7 +351,7 @@ public function whereOr(array $parameters): static foreach ($parameters as $key => $val) { if (is_int($key)) { // whereOr(['full condition']) $columns[] = $val; - } elseif (strpos($key, '?') === false) { // whereOr(['column1' => 1]) + } elseif (!str_contains($key, '?')) { // whereOr(['column1' => 1]) $columns[] = $key . ' ?'; $values[] = $val; } else { // whereOr(['column1 > ?' => 1]) @@ -931,7 +931,7 @@ public function getReferencingTable( string $column = null, int|string $active = null, ): ?GroupedSelection { - if (strpos($table, '.') !== false) { + if (str_contains($table, '.')) { [$table, $column] = explode('.', $table); } elseif (!$column) { $hasMany = $this->conventions->getHasManyReference($this->name, $table); diff --git a/src/Database/Table/SqlBuilder.php b/src/Database/Table/SqlBuilder.php index 46001b5f2..7c6cfd632 100644 --- a/src/Database/Table/SqlBuilder.php +++ b/src/Database/Table/SqlBuilder.php @@ -370,10 +370,10 @@ protected function addCondition($condition, array $params, array &$conditions, a if ($arg !== null) { if (!$arg) { - $hasBrackets = strpos($condition, '(') !== false; + $hasBrackets = str_contains($condition, '('); $hasOperators = preg_match('#AND|OR#', $condition); - $hasNot = strpos($condition, 'NOT') !== false; - $hasPrefixNot = strpos($match[2][0], 'NOT') !== false; + $hasNot = str_contains($condition, 'NOT'); + $hasPrefixNot = str_contains($match[2][0], 'NOT'); if (!$hasBrackets && ($hasOperators || ($hasNot && !$hasPrefixNot))) { throw new Nette\InvalidArgumentException('Possible SQL query corruption. Add parentheses around operators.'); }