Skip to content

Commit f106aaa

Browse files
committed
used native PHP 8 functions
1 parent 1ff5bd5 commit f106aaa

9 files changed

Lines changed: 20 additions & 20 deletions

File tree

src/Dibi/Connection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,9 @@ public function getSubstitutes(): HashMap
501501
*/
502502
public function substitute(string $value): string
503503
{
504-
return strpos($value, ':') === false
505-
? $value
506-
: preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value);
504+
return str_contains($value, ':')
505+
? preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value)
506+
: $value;
507507
}
508508

509509

src/Dibi/Drivers/PostgreDriver.php

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

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

142142
} 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
@@ -98,19 +98,19 @@ public static function createException(string $message, $code, string $sql): Dib
9898
if ($code !== 19) {
9999
return new Dibi\DriverException($message, $code, $sql);
100100

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

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

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

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 = nul
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 $sql = null, bool $return = false): ?s
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) {
@@ -86,7 +86,7 @@ public static function dump(string|Result $sql = null, bool $return = false): ?s
8686
// syntax highlight
8787
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
8888
if (PHP_SAPI === 'cli') {
89-
if (substr((string) getenv('TERM'), 0, 5) === 'xterm') {
89+
if (str_starts_with((string) getenv('TERM'), 'xterm')) {
9090
$sql = preg_replace_callback($highlighter, function (array $m) {
9191
if (!empty($m[1])) { // comment
9292
return "\033[1;30m" . $m[1] . "\033[0m";
@@ -249,7 +249,7 @@ public static function loadFromFile(Connection $connection, string $file, callab
249249
if (strtoupper(substr($s, 0, 10)) === 'DELIMITER ') {
250250
$delimiter = trim(substr($s, 10));
251251

252-
} elseif (substr($ts = rtrim($s), -strlen($delimiter)) === $delimiter) {
252+
} elseif (str_ends_with($ts = rtrim($s), $delimiter)) {
253253
$sql .= substr($ts, 0, -strlen($delimiter));
254254
$driver->query($sql);
255255
$sql = '';

src/Dibi/Result.php

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

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

487487
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
488-
if ($value && substr((string) $value, 0, 3) !== '000') { // '', null, false, '0000-00-00', ...
488+
if ($value && !str_starts_with((string) $value, '000')) { // '', null, false, '0000-00-00', ...
489489
$value = new DateTime($value);
490490
$row[$key] = $format ? $value->format($format) : $value;
491491
} else {

src/Dibi/Row.php

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

src/Dibi/Translator.php

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

0 commit comments

Comments
 (0)