Skip to content

Commit

Permalink
used native PHP 8 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 25, 2021
1 parent 5b03385 commit f0b966a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Database/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
14 changes: 7 additions & 7 deletions src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Table/SqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down

0 comments on commit f0b966a

Please sign in to comment.