diff --git a/src/Dibi/Bridges/Tracy/Panel.php b/src/Dibi/Bridges/Tracy/Panel.php index 268e18e8..76bda79c 100644 --- a/src/Dibi/Bridges/Tracy/Panel.php +++ b/src/Dibi/Bridges/Tracy/Panel.php @@ -165,7 +165,7 @@ class Panel implements Tracy\IBarPanel private function getConnectionName(Dibi\Connection $connection): string { $driver = $connection->getConfig('driver'); - return (is_object($driver) ? $driver::class : $driver) + return get_debug_type($driver) . ($connection->getConfig('name') ? '/' . $connection->getConfig('name') : '') . ($connection->getConfig('host') ? "\u{202f}@\u{202f}" . $connection->getConfig('host') : ''); } diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 2042ae35..ee230806 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -508,9 +508,9 @@ class Connection implements IConnection */ 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/PostgreReflector.php b/src/Dibi/Drivers/PostgreReflector.php index 6d08a17d..c97ff437 100644 --- a/src/Dibi/Drivers/PostgreReflector.php +++ b/src/Dibi/Drivers/PostgreReflector.php @@ -123,7 +123,7 @@ class PostgreReflector implements Dibi\Reflector '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/Event.php b/src/Dibi/Event.php index 9777b82b..92f4a162 100644 --- a/src/Dibi/Event.php +++ b/src/Dibi/Event.php @@ -58,7 +58,7 @@ class Event if ( isset($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']]; break; diff --git a/src/Dibi/Helpers.php b/src/Dibi/Helpers.php index cf67a861..acfaff13 100644 --- a/src/Dibi/Helpers.php +++ b/src/Dibi/Helpers.php @@ -22,7 +22,7 @@ class Helpers { 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) { @@ -87,7 +87,7 @@ class Helpers // 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"; @@ -255,7 +255,7 @@ class Helpers 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/Loggers/FileLogger.php b/src/Dibi/Loggers/FileLogger.php index 3a71fc88..24239e65 100644 --- a/src/Dibi/Loggers/FileLogger.php +++ b/src/Dibi/Loggers/FileLogger.php @@ -70,7 +70,7 @@ class FileLogger { $driver = $event->connection->getConfig('driver'); $message .= - "\n-- driver: " . (is_object($driver) ? $driver::class : $driver) . '/' . $event->connection->getConfig('name') + "\n-- driver: " . get_debug_type($driver) . '/' . $event->connection->getConfig('name') . "\n-- " . date('Y-m-d H:i:s') . "\n\n"; file_put_contents($this->file, $message, FILE_APPEND | LOCK_EX); diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index d089b19b..328f05fc 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -230,7 +230,7 @@ class Result implements IDataSource */ final public function fetchAssoc(string $assoc): array { - if (strpos($assoc, ',') !== false) { + if (str_contains($assoc, ',')) { return $this->oldFetchAssoc($assoc); } @@ -487,7 +487,7 @@ class Result implements IDataSource $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 ad4a17d6..8a99d4e8 100644 --- a/src/Dibi/Row.php +++ b/src/Dibi/Row.php @@ -37,7 +37,7 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable { $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; } diff --git a/src/Dibi/Translator.php b/src/Dibi/Translator.php index 98299d4a..b037edeb 100644 --- a/src/Dibi/Translator.php +++ b/src/Dibi/Translator.php @@ -187,7 +187,7 @@ final class Translator $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 '; @@ -257,7 +257,7 @@ final class Translator $proto = array_keys($v); } } else { - return $this->errors[] = '**Unexpected type ' . (is_object($v) ? $v::class : gettype($v)) . '**'; + return $this->errors[] = '**Unexpected type ' . get_debug_type($v) . '**'; } $pair = explode('%', $k, 2); // split into identifier & modifier @@ -326,7 +326,7 @@ final class Translator ) { // continue } else { - $type = is_object($value) ? $value::class : gettype($value); + $type = get_debug_type($value); return $this->errors[] = "**Invalid combination of type $type and modifier %$modifier**"; } } @@ -492,7 +492,7 @@ final class Translator return $this->connection->translate(...$value->getValues()); } else { - $type = is_object($value) ? $value::class : gettype($value); + $type = get_debug_type($value); return $this->errors[] = "**Unexpected $type**"; } } diff --git a/src/Dibi/exceptions.php b/src/Dibi/exceptions.php index 8711cb65..612ca7a1 100644 --- a/src/Dibi/exceptions.php +++ b/src/Dibi/exceptions.php @@ -56,17 +56,9 @@ class DriverException extends Exception */ class PcreException extends Exception { - public function __construct(string $message = '%msg.') + public function __construct() { - static $messages = [ - 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); + parent::__construct(preg_last_error_msg(), preg_last_error()); } }