Skip to content

Commit a7aaa31

Browse files
committed
used native PHP 8 features
1 parent 76c0dd6 commit a7aaa31

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

src/Database/Drivers/PgSqlDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function initialize(Nette\Database\Connection $connection, array $options
3131
public function convertException(\PDOException $e): Nette\Database\DriverException
3232
{
3333
$code = $e->errorInfo[0] ?? null;
34-
if ($code === '0A000' && strpos($e->getMessage(), 'truncate') !== false) {
34+
if ($code === '0A000' && str_contains($e->getMessage(), 'truncate')) {
3535
return Nette\Database\ForeignKeyConstraintViolationException::from($e);
3636

3737
} elseif ($code === '23502') {

src/Database/Drivers/SqliteDriver.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ public function convertException(\PDOException $e): Nette\Database\DriverExcepti
4040
return Nette\Database\DriverException::from($e);
4141

4242
} elseif (
43-
strpos($msg, 'must be unique') !== false
44-
|| strpos($msg, 'is not unique') !== false
45-
|| strpos($msg, 'UNIQUE constraint failed') !== false
43+
str_contains($msg, 'must be unique')
44+
|| str_contains($msg, 'is not unique')
45+
|| str_contains($msg, 'UNIQUE constraint failed')
4646
) {
4747
return Nette\Database\UniqueConstraintViolationException::from($e);
4848

4949
} elseif (
50-
strpos($msg, 'may not be null') !== false
51-
|| strpos($msg, 'NOT NULL constraint failed') !== false
50+
str_contains($msg, 'may not be null')
51+
|| str_contains($msg, 'NOT NULL constraint failed')
5252
) {
5353
return Nette\Database\NotNullConstraintViolationException::from($e);
5454

5555
} elseif (
56-
strpos($msg, 'foreign key constraint failed') !== false
57-
|| strpos($msg, 'FOREIGN KEY constraint failed') !== false
56+
str_contains($msg, 'foreign key constraint failed')
57+
|| str_contains($msg, 'FOREIGN KEY constraint failed')
5858
) {
5959
return Nette\Database\ForeignKeyConstraintViolationException::from($e);
6060

src/Database/ResultSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(Connection $connection, string $queryString, array $
5252
$this->normalizer = $normalizer;
5353

5454
try {
55-
if (substr($queryString, 0, 2) === '::') {
55+
if (str_starts_with($queryString, '::')) {
5656
$connection->getPdo()->{substr($queryString, 2)}();
5757
} elseif ($queryString !== null) {
5858
static $types = ['boolean' => PDO::PARAM_BOOL, 'integer' => PDO::PARAM_INT,

src/Database/SqlPreprocessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private function formatValue($value, string $mode = null): string
188188
} elseif ($value instanceof \DateInterval) {
189189
return $this->driver->formatDateInterval($value);
190190

191-
} elseif (is_object($value) && method_exists($value, '__toString')) {
191+
} elseif ($value instanceof \Stringable) {
192192
$this->remaining[] = (string) $value;
193193
return '?';
194194
}
@@ -244,7 +244,7 @@ private function formatValue($value, string $mode = null): string
244244
foreach ($value as $k => $v) {
245245
if (is_int($k)) { // value, value, ...
246246
$vx[] = $this->formatValue($v);
247-
} elseif (substr($k, -1) === '=') { // key+=value, key-=value, ...
247+
} elseif (str_ends_with($k, '=')) { // key+=value, key-=value, ...
248248
$k2 = $this->delimite(substr($k, 0, -2));
249249
$vx[] = $k2 . '=' . $k2 . ' ' . substr($k, -2, 1) . ' ' . $this->formatValue($v);
250250
} else { // key=value, key=value, ...

src/Database/Table/Selection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function whereOr(array $parameters): static
351351
foreach ($parameters as $key => $val) {
352352
if (is_int($key)) { // whereOr(['full condition'])
353353
$columns[] = $val;
354-
} elseif (strpos($key, '?') === false) { // whereOr(['column1' => 1])
354+
} elseif (!str_contains($key, '?')) { // whereOr(['column1' => 1])
355355
$columns[] = $key . ' ?';
356356
$values[] = $val;
357357
} else { // whereOr(['column1 > ?' => 1])
@@ -931,7 +931,7 @@ public function getReferencingTable(
931931
string $column = null,
932932
int|string $active = null,
933933
): ?GroupedSelection {
934-
if (strpos($table, '.') !== false) {
934+
if (str_contains($table, '.')) {
935935
[$table, $column] = explode('.', $table);
936936
} elseif (!$column) {
937937
$hasMany = $this->conventions->getHasManyReference($this->name, $table);

src/Database/Table/SqlBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,10 @@ protected function addCondition($condition, array $params, array &$conditions, a
370370

371371
if ($arg !== null) {
372372
if (!$arg) {
373-
$hasBrackets = strpos($condition, '(') !== false;
373+
$hasBrackets = str_contains($condition, '(');
374374
$hasOperators = preg_match('#AND|OR#', $condition);
375-
$hasNot = strpos($condition, 'NOT') !== false;
376-
$hasPrefixNot = strpos($match[2][0], 'NOT') !== false;
375+
$hasNot = str_contains($condition, 'NOT');
376+
$hasPrefixNot = str_contains($match[2][0], 'NOT');
377377
if (!$hasBrackets && ($hasOperators || ($hasNot && !$hasPrefixNot))) {
378378
throw new Nette\InvalidArgumentException('Possible SQL query corruption. Add parentheses around operators.');
379379
}
@@ -794,7 +794,7 @@ private function getConditionHash($condition, array $parameters): string
794794
$parameter = $this->getConditionHash($parameter->getSql(), $parameter->getSqlBuilder()->getParameters());
795795
} elseif ($parameter instanceof SqlLiteral) {
796796
$parameter = $this->getConditionHash($parameter->__toString(), $parameter->getParameters());
797-
} elseif (is_object($parameter) && method_exists($parameter, '__toString')) {
797+
} elseif ($parameter instanceof \Stringable) {
798798
$parameter = $parameter->__toString();
799799
} elseif (is_array($parameter) || $parameter instanceof \ArrayAccess) {
800800
$parameter = $this->getConditionHash($key, $parameter);

0 commit comments

Comments
 (0)