1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 22:26:43 +02:00

used native PHP 8 functions

This commit is contained in:
David Grudl
2022-11-18 04:46:00 +01:00
parent 94df6db03d
commit cab1c5b5e6
10 changed files with 22 additions and 30 deletions

View File

@@ -516,9 +516,9 @@ class Connection implements IConnection
*/ */
public function substitute(string $value): string public function substitute(string $value): string
{ {
return strpos($value, ':') === false return str_contains($value, ':')
? $value ? preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value)
: preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value); : $value;
} }

View File

@@ -139,7 +139,7 @@ class PostgreDriver implements Dibi\Driver
$message = substr($message, strlen($m[0])); $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); return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);
} elseif ($code === '23502') { } elseif ($code === '23502') {

View File

@@ -126,7 +126,7 @@ class PostgreReflector implements Dibi\Reflector
'size' => $size > 0 ? $size : null, 'size' => $size > 0 ? $size : null,
'nullable' => $row['is_nullable'] === 'YES' || $row['is_nullable'] === 't' || $row['is_nullable'] === true, 'nullable' => $row['is_nullable'] === 'YES' || $row['is_nullable'] === 't' || $row['is_nullable'] === true,
'default' => $row['column_default'], '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, 'vendor' => $row,
]; ];
} }

View File

@@ -99,19 +99,19 @@ class SqliteDriver implements Dibi\Driver
if ($code !== 19) { if ($code !== 19) {
return new Dibi\DriverException($message, $code, $sql); return new Dibi\DriverException($message, $code, $sql);
} elseif (strpos($message, 'must be unique') !== false } elseif (str_contains($message, 'must be unique')
|| strpos($message, 'is not unique') !== false || str_contains($message, 'is not unique')
|| strpos($message, 'UNIQUE constraint failed') !== false || str_contains($message, 'UNIQUE constraint failed')
) { ) {
return new Dibi\UniqueConstraintViolationException($message, $code, $sql); return new Dibi\UniqueConstraintViolationException($message, $code, $sql);
} elseif (strpos($message, 'may not be null') !== false } elseif (str_contains($message, 'may not be null')
|| strpos($message, 'NOT NULL constraint failed') !== false || str_contains($message, 'NOT NULL constraint failed')
) { ) {
return new Dibi\NotNullConstraintViolationException($message, $code, $sql); return new Dibi\NotNullConstraintViolationException($message, $code, $sql);
} elseif (strpos($message, 'foreign key constraint failed') !== false } elseif (str_contains($message, 'foreign key constraint failed')
|| strpos($message, 'FOREIGN KEY constraint failed') !== false || str_contains($message, 'FOREIGN KEY constraint failed')
) { ) {
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);

View File

@@ -66,7 +66,7 @@ class Event
if ( if (
isset($row['file']) isset($row['file'])
&& preg_match('~\.(php.?|phtml)$~', $row['file']) && preg_match('~\.(php.?|phtml)$~', $row['file'])
&& substr($row['file'], 0, strlen($dibiDir)) !== $dibiDir && !str_starts_with($row['file'], $dibiDir)
) { ) {
$this->source = [$row['file'], (int) $row['line']]; $this->source = [$row['file'], (int) $row['line']];
break; break;

View File

@@ -24,7 +24,7 @@ class Helpers
{ {
ob_start(); ob_start();
if ($sql instanceof Result && PHP_SAPI === 'cli') { 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; $maxLen = 0;
foreach ($sql as $i => $row) { foreach ($sql as $i => $row) {
if ($i === 0) { if ($i === 0) {
@@ -89,7 +89,7 @@ class Helpers
// syntax highlight // syntax highlight
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is"; $highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
if (PHP_SAPI === 'cli') { 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) { $sql = preg_replace_callback($highlighter, function (array $m) {
if (!empty($m[1])) { // comment if (!empty($m[1])) { // comment
return "\033[1;30m" . $m[1] . "\033[0m"; return "\033[1;30m" . $m[1] . "\033[0m";
@@ -257,7 +257,7 @@ class Helpers
if (strtoupper(substr($s, 0, 10)) === 'DELIMITER ') { if (strtoupper(substr($s, 0, 10)) === 'DELIMITER ') {
$delimiter = trim(substr($s, 10)); $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)); $sql .= substr($ts, 0, -strlen($delimiter));
$driver->query($sql); $driver->query($sql);
$sql = ''; $sql = '';

View File

@@ -234,7 +234,7 @@ class Result implements IDataSource
*/ */
final public function fetchAssoc(string $assoc): array final public function fetchAssoc(string $assoc): array
{ {
if (strpos($assoc, ',') !== false) { if (str_contains($assoc, ',')) {
return $this->oldFetchAssoc($assoc); return $this->oldFetchAssoc($assoc);
} }
@@ -491,7 +491,7 @@ class Result implements IDataSource
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F'; $row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) { } 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); $value = new DateTime($value);
$row[$key] = $format ? $value->format($format) : $value; $row[$key] = $format ? $value->format($format) : $value;
} else { } else {

View File

@@ -37,7 +37,7 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable
{ {
$time = $this[$key]; $time = $this[$key];
if (!$time instanceof DateTime) { 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; return null;
} }

View File

@@ -198,7 +198,7 @@ final class Translator
$v = $this->formatValue($v, $pair[1]); $v = $this->formatValue($v, $pair[1]);
if ($pair[1] === 'l' || $pair[1] === 'in') { if ($pair[1] === 'l' || $pair[1] === 'in') {
$op = 'IN '; $op = 'IN ';
} elseif (strpos($pair[1], 'like') !== false) { } elseif (str_contains($pair[1], 'like')) {
$op = 'LIKE '; $op = 'LIKE ';
} elseif ($v === 'NULL') { } elseif ($v === 'NULL') {
$op = 'IS '; $op = 'IS ';

View File

@@ -56,17 +56,9 @@ class DriverException extends Exception
*/ */
class PcreException extends Exception class PcreException extends Exception
{ {
public function __construct(string $message = '%msg.') public function __construct()
{ {
static $messages = [ parent::__construct(preg_last_error_msg(), preg_last_error());
PREG_INTERNAL_ERROR => 'Internal error',
PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', // PREG_BAD_UTF8_OFFSET_ERROR
];
$code = preg_last_error();
parent::__construct(str_replace('%msg', $messages[$code] ?? 'Unknown error', $message), $code);
} }
} }