Skip to content

Commit 1585255

Browse files
committed
used native PHP 8 functions
1 parent f25cbbb commit 1585255

10 files changed

Lines changed: 22 additions & 30 deletions

File tree

src/Dibi/Connection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ public function getSubstitutes(): HashMap
516516
*/
517517
public function substitute(string $value): string
518518
{
519-
return strpos($value, ':') === false
520-
? $value
521-
: preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value);
519+
return str_contains($value, ':')
520+
? preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value)
521+
: $value;
522522
}
523523

524524

src/Dibi/Drivers/PostgreDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static function createException(string $message, $code = null, ?string $s
139139
$message = substr($message, strlen($m[0]));
140140
}
141141

142-
if ($code === '0A000' && strpos($message, 'truncate') !== false) {
142+
if ($code === '0A000' && str_contains($message, 'truncate')) {
143143
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);
144144

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

src/Dibi/Drivers/PostgreReflector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function getColumns(string $table): array
126126
'size' => $size > 0 ? $size : null,
127127
'nullable' => $row['is_nullable'] === 'YES' || $row['is_nullable'] === 't' || $row['is_nullable'] === true,
128128
'default' => $row['column_default'],
129-
'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'] ?? '', 0, 7) === 'nextval',
129+
'autoincrement' => (int) $row['ordinal_position'] === $primary && str_starts_with($row['column_default'] ?? '', 'nextval'),
130130
'vendor' => $row,
131131
];
132132
}

src/Dibi/Drivers/SqliteDriver.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ public static function createException(string $message, $code, string $sql): Dib
9999
if ($code !== 19) {
100100
return new Dibi\DriverException($message, $code, $sql);
101101

102-
} elseif (strpos($message, 'must be unique') !== false
103-
|| strpos($message, 'is not unique') !== false
104-
|| strpos($message, 'UNIQUE constraint failed') !== false
102+
} elseif (str_contains($message, 'must be unique')
103+
|| str_contains($message, 'is not unique')
104+
|| str_contains($message, 'UNIQUE constraint failed')
105105
) {
106106
return new Dibi\UniqueConstraintViolationException($message, $code, $sql);
107107

108-
} elseif (strpos($message, 'may not be null') !== false
109-
|| strpos($message, 'NOT NULL constraint failed') !== false
108+
} elseif (str_contains($message, 'may not be null')
109+
|| str_contains($message, 'NOT NULL constraint failed')
110110
) {
111111
return new Dibi\NotNullConstraintViolationException($message, $code, $sql);
112112

113-
} elseif (strpos($message, 'foreign key constraint failed') !== false
114-
|| strpos($message, 'FOREIGN KEY constraint failed') !== false
113+
} elseif (str_contains($message, 'foreign key constraint failed')
114+
|| str_contains($message, 'FOREIGN KEY constraint failed')
115115
) {
116116
return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql);
117117

src/Dibi/Event.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __construct(Connection $connection, int $type, ?string $sql = nu
6363

6464
$dibiDir = dirname((new \ReflectionClass('dibi'))->getFileName()) . DIRECTORY_SEPARATOR;
6565
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $row) {
66-
if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) {
66+
if (isset($row['file']) && is_file($row['file']) && !str_starts_with($row['file'], $dibiDir)) {
6767
$this->source = [$row['file'], (int) $row['line']];
6868
break;
6969
}

src/Dibi/Helpers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function dump(string|Result|null $sql = null, bool $return = false
2424
{
2525
ob_start();
2626
if ($sql instanceof Result && PHP_SAPI === 'cli') {
27-
$hasColors = (substr((string) getenv('TERM'), 0, 5) === 'xterm');
27+
$hasColors = (str_starts_with((string) getenv('TERM'), 'xterm'));
2828
$maxLen = 0;
2929
foreach ($sql as $i => $row) {
3030
if ($i === 0) {
@@ -89,7 +89,7 @@ public static function dump(string|Result|null $sql = null, bool $return = false
8989
// syntax highlight
9090
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
9191
if (PHP_SAPI === 'cli') {
92-
if (substr((string) getenv('TERM'), 0, 5) === 'xterm') {
92+
if (str_starts_with((string) getenv('TERM'), 'xterm')) {
9393
$sql = preg_replace_callback($highlighter, function (array $m) {
9494
if (!empty($m[1])) { // comment
9595
return "\033[1;30m" . $m[1] . "\033[0m";
@@ -257,7 +257,7 @@ public static function loadFromFile(Connection $connection, string $file, ?calla
257257
if (strtoupper(substr($s, 0, 10)) === 'DELIMITER ') {
258258
$delimiter = trim(substr($s, 10));
259259

260-
} elseif (substr($ts = rtrim($s), -strlen($delimiter)) === $delimiter) {
260+
} elseif (str_ends_with($ts = rtrim($s), $delimiter)) {
261261
$sql .= substr($ts, 0, -strlen($delimiter));
262262
$driver->query($sql);
263263
$sql = '';

src/Dibi/Result.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ final public function fetchAll(?int $offset = null, ?int $limit = null): array
234234
*/
235235
final public function fetchAssoc(string $assoc): array
236236
{
237-
if (strpos($assoc, ',') !== false) {
237+
if (str_contains($assoc, ',')) {
238238
return $this->oldFetchAssoc($assoc);
239239
}
240240

@@ -491,7 +491,7 @@ private function normalize(array &$row): void
491491
$row[$key] = ((bool) $value) && $value !== 'f' && $value !== 'F';
492492

493493
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
494-
if ($value && substr((string) $value, 0, 7) !== '0000-00') { // '', null, false, '0000-00-00', ...
494+
if ($value && !str_starts_with((string) $value, '0000-00')) { // '', null, false, '0000-00-00', ...
495495
$value = new DateTime($value);
496496
$row[$key] = $format ? $value->format($format) : $value;
497497
} else {

src/Dibi/Row.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function asDateTime(string $key, ?string $format = null): DateTime|string
3737
{
3838
$time = $this[$key];
3939
if (!$time instanceof DateTime) {
40-
if (!$time || substr((string) $time, 0, 7) === '0000-00') { // '', null, false, '0000-00-00', ...
40+
if (!$time || str_starts_with((string) $time, '0000-00')) { // '', null, false, '0000-00-00', ...
4141
return null;
4242
}
4343

src/Dibi/Translator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public function formatValue(mixed $value, ?string $modifier): string
198198
$v = $this->formatValue($v, $pair[1]);
199199
if ($pair[1] === 'l' || $pair[1] === 'in') {
200200
$op = 'IN ';
201-
} elseif (strpos($pair[1], 'like') !== false) {
201+
} elseif (str_contains($pair[1], 'like')) {
202202
$op = 'LIKE ';
203203
} elseif ($v === 'NULL') {
204204
$op = 'IS ';

src/Dibi/exceptions.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,9 @@ class DriverException extends Exception
5656
*/
5757
class PcreException extends Exception
5858
{
59-
public function __construct(string $message = '%msg.')
59+
public function __construct()
6060
{
61-
static $messages = [
62-
PREG_INTERNAL_ERROR => 'Internal error',
63-
PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
64-
PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
65-
PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
66-
5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', // PREG_BAD_UTF8_OFFSET_ERROR
67-
];
68-
$code = preg_last_error();
69-
parent::__construct(str_replace('%msg', $messages[$code] ?? 'Unknown error', $message), $code);
61+
parent::__construct(preg_last_error_msg(), preg_last_error());
7062
}
7163
}
7264

0 commit comments

Comments
 (0)