Skip to content

Commit 6b99dd7

Browse files
committed
used native PHP 8 functions
1 parent 7862efc commit 6b99dd7

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
@@ -510,9 +510,9 @@ public function getSubstitutes(): HashMap
510510
*/
511511
public function substitute(string $value): string
512512
{
513-
return strpos($value, ':') === false
514-
? $value
515-
: preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value);
513+
return str_contains($value, ':')
514+
? preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value)
515+
: $value;
516516
}
517517

518518

src/Dibi/Drivers/PostgreDriver.php

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

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

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

src/Dibi/Drivers/PostgreReflector.php

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

src/Dibi/Drivers/SqliteDriver.php

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

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

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

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

src/Dibi/Event.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function __construct(Connection $connection, int $type, ?string $sql = nu
6060
if (
6161
isset($row['file'])
6262
&& preg_match('~\.(php.?|phtml)$~', $row['file'])
63-
&& substr($row['file'], 0, strlen($dibiDir)) !== $dibiDir
63+
&& !str_starts_with($row['file'], $dibiDir)
6464
) {
6565
$this->source = [$row['file'], (int) $row['line']];
6666
break;

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
@@ -232,7 +232,7 @@ final public function fetchAll(?int $offset = null, ?int $limit = null): array
232232
*/
233233
final public function fetchAssoc(string $assoc): array
234234
{
235-
if (strpos($assoc, ',') !== false) {
235+
if (str_contains($assoc, ',')) {
236236
return $this->oldFetchAssoc($assoc);
237237
}
238238

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

491491
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
492-
if ($value && substr((string) $value, 0, 7) !== '0000-00') { // '', null, false, '0000-00-00', ...
492+
if ($value && !str_starts_with((string) $value, '0000-00')) { // '', null, false, '0000-00-00', ...
493493
$value = new DateTime($value);
494494
$row[$key] = $format ? $value->format($format) : $value;
495495
} 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
@@ -189,7 +189,7 @@ public function formatValue(mixed $value, ?string $modifier): string
189189
$v = $this->formatValue($v, $pair[1]);
190190
if ($pair[1] === 'l' || $pair[1] === 'in') {
191191
$op = 'IN ';
192-
} elseif (strpos($pair[1], 'like') !== false) {
192+
} elseif (str_contains($pair[1], 'like')) {
193193
$op = 'LIKE ';
194194
} elseif ($v === 'NULL') {
195195
$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)