Skip to content

Commit

Permalink
Use modern string functions
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Jun 8, 2024
1 parent c0b60dd commit 6fdaa13
Show file tree
Hide file tree
Showing 25 changed files with 50 additions and 32 deletions.
2 changes: 1 addition & 1 deletion config/components.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@
// keep relative urls
if (
$path !== null &&
(substr($path, 0, 2) === './' || substr($path, 0, 3) === '../')
(str_starts_with($path, './') || str_starts_with($path, '../'))
) {
return $path;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cms/AppCaches.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function cacheOptionsKey(string $key): string

// plain keys without dots don't need further investigation
// since they can never be from a plugin.
if (strpos($key, '.') === false) {
if (str_contains($key, '.') === false) {
return $prefixedKey;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Cms/AppPlugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,10 @@ protected function pluginsLoader(): array
$loaded = [];

foreach (Dir::read($root) as $dirname) {
if (in_array(substr($dirname, 0, 1), ['.', '_']) === true) {
if (
str_starts_with($dirname, '.') ||
str_starts_with($dirname, '_')
) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Cms/HasFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function file(
return Uuid::for($filename, $this->$in())->model();
}

if (strpos($filename, '/') !== false) {
if (str_contains($filename, '/') === true) {
$path = dirname($filename);
$filename = basename($filename);

Expand Down
2 changes: 1 addition & 1 deletion src/Cms/Pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function findByKey(string|null $key = null): Page|null
$key = trim($key, '/');

// strip extensions from the id
if (strpos($key, '.') !== false) {
if (str_contains($key, '.') === true) {
$info = pathinfo($key);

if ($info['dirname'] !== '.') {
Expand Down
2 changes: 1 addition & 1 deletion src/Cms/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static function load(string|null $root = null, array $inject = []): stati

// load roles from plugins
foreach ($kirby->extensions('blueprints') as $name => $blueprint) {
if (substr($name, 0, 6) !== 'users/') {
if (str_starts_with($name, 'users/') === false) {
continue;
}

Expand Down
10 changes: 8 additions & 2 deletions src/Database/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,17 @@ public function tableName(string $table): string
public function unquoteIdentifier(string $identifier): string
{
// remove quotes around the identifier
if (in_array(Str::substr($identifier, 0, 1), ['"', '`']) === true) {
if (
str_starts_with($identifier, '"') ||
str_starts_with($identifier, '`')
) {
$identifier = Str::substr($identifier, 1);
}

if (in_array(Str::substr($identifier, -1), ['"', '`']) === true) {
if (
str_ends_with($identifier, '"') ||
str_ends_with($identifier, '`')
) {
$identifier = Str::substr($identifier, 0, -1);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Filesystem/Dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ public static function inventory(
// loop through all directory items and collect all relevant information
foreach ($items as $item) {
// ignore all items with a leading dot or underscore
if (in_array(substr($item, 0, 1), ['.', '_']) === true) {
if (
str_starts_with($item, '.') ||
str_starts_with($item, '_')
) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Filesystem/F.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public static function is(string $file, string $value): bool
}

// check for the mime type
if (strpos($value, '/') !== false) {
if (str_contains($value, '/') === true) {
return static::mime($file) === $value;
}

Expand Down Expand Up @@ -653,7 +653,7 @@ public static function realpath(string $file, string|null $in = null): string
throw new Exception(sprintf('The parent directory does not exist: "%s"', $in));
}

if (substr($realpath, 0, strlen($parent)) !== $parent) {
if (str_starts_with($realpath, $parent) === false) {
throw new Exception('The file is not within the parent directory');
}
}
Expand Down Expand Up @@ -711,7 +711,7 @@ public static function relativepath(string $file, string|null $in = null): strin
*/
public static function remove(string $file): bool
{
if (strpos($file, '*') !== false) {
if (str_contains($file, '*') === true) {
foreach (glob($file) as $f) {
static::remove($f);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected static function hmac(string $value): string
protected static function parse(string $string): string|null
{
// if no hash-value separator is present, we can't parse the value
if (strpos($string, '+') === false) {
if (str_contains($string, '+') === false) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ protected function detectCli(bool|null $override = null): bool
$term = getenv('TERM');

if (
substr($sapi, 0, 3) === 'cgi' &&
str_starts_with($sapi, 'cgi') === true &&
$term &&
$term !== 'unknown'
) {
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function extract(string|array|null $path = null): array
$slash = false;

if (is_string($path) === true) {
$slash = substr($path, -1, 1) === '/';
$slash = str_ends_with($path, '/') === true;
$path = Str::split($path, '/');
}

Expand All @@ -58,7 +58,7 @@ public static function extract(string|array|null $path = null): array
$separator = static::separator();

foreach ($path as $index => $p) {
if (strpos($p, $separator) === false) {
if (str_contains($p, $separator) === false) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ public function headers(): array

foreach (Environment::getGlobally() as $key => $value) {
if (
substr($key, 0, 5) !== 'HTTP_' &&
substr($key, 0, 14) !== 'REDIRECT_HTTP_'
str_starts_with($key, 'HTTP_') === false &&
str_starts_with($key, 'REDIRECT_HTTP_') === false
) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Request/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function data(): array
return $this->data = $json;
}

if (strstr($contents, '=') !== false) {
if (str_contains($contents, '=') === true) {
// try to parse the body as query string
parse_str($contents, $parsed);

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct(
$this->charset = $charset ?? 'UTF-8';

// automatic mime type detection
if (strpos($this->type, '/') === false) {
if (str_contains($this->type, '/') === false) {
$this->type = F::extensionToMime($this->type) ?? 'text/html';
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function parse(string $pattern, string $path): array|false
// We only need to check routes with regular expression since all others
// would have been able to be matched by the search for literal matches
// we just did before we started searching.
if (strpos($pattern, '(') === false) {
if (str_contains($pattern, '(') === false) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ protected static function parsePath(array $props): array
// use the full path;
// automatically detect the trailing slash from it if possible
if (is_string($props['path']) === true) {
$props['slash'] = substr($props['path'], -1, 1) === '/';
$props['slash'] = str_ends_with($props['path'], '/') === true;
}

return $props;
Expand Down
7 changes: 5 additions & 2 deletions src/Http/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static function makeAbsolute(string|null $path = null, string|null $home
return $home ?? static::home();
}

if (substr($path, 0, 1) === '#') {
if (str_starts_with($path, '#') === true) {
return $path;
}

Expand Down Expand Up @@ -226,7 +226,10 @@ public static function to(
$path ??= '';

// keep relative urls
if (substr($path, 0, 2) === './' || substr($path, 0, 3) === '../') {
if (
str_starts_with($path, './') === true ||
str_starts_with($path, '../') === true
) {
return $path;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Query/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function factory(string $argument): static

// numeric
if (is_numeric($argument) === true) {
if (strpos($argument, '.') === false) {
if (str_contains($argument, '.') === false) {
return new static((int)$argument);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Text/KirbyTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static function parse(

// use substr instead of rtrim to keep non-tagged brackets
// (link: file.pdf text: Download (PDF))
if (substr($tag, -1) === ')') {
if (str_ends_with($tag, ')') === true) {
$tag = substr($tag, 0, -1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Toolkit/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public static function get(
}

// extract data from nested array structures using the dot notation
if (strpos($key, '.') !== false) {
if (str_contains($key, '.') === true) {
$keys = explode('.', $key);
$firstKey = array_shift($keys);

Expand Down
4 changes: 2 additions & 2 deletions src/Toolkit/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1184,15 +1184,15 @@ public function without(string ...$keys): static
* Contains Filter
*/
Collection::$filters['*='] = [
'validator' => fn ($value, $test) => strpos($value, $test) !== false,
'validator' => fn ($value, $test) => str_contains($value, $test) === true,
'strict' => false
];

/**
* Not Contains Filter
*/
Collection::$filters['!*='] = [
'validator' => fn ($value, $test) => strpos($value, $test) === false
'validator' => fn ($value, $test) => str_contains($value, $test) === false
];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Toolkit/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public static function float(string|int|float|null $value): string
$value = (string)$value;

// Convert exponential to decimal, 1e-8 as 0.00000001
if (strpos(strtolower($value), 'e') !== false) {
if (str_contains(strtolower($value), 'e') === true) {
$value = rtrim(sprintf('%.16f', (float)$value), '0');
}

Expand Down
5 changes: 4 additions & 1 deletion src/Toolkit/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,10 @@ public static function simplify(
// of the respective type to a simple string;
// don't do anything with special `@` metadata keys
foreach ($array as $name => $item) {
if (substr($name, 0, 1) !== '@' && count($item) === 1) {
if (
str_starts_with($name, '@') === false &&
count($item) === 1
) {
$array[$name] = $item[0];
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Panel/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public function testIcons(): void
$icons = $assets->icons();

$this->assertNotNull($icons);
$this->assertTrue(strpos($icons, '<svg', 0) !== false);
$this->assertTrue(str_contains($icons, '<svg'));
}

/**
Expand Down

0 comments on commit 6fdaa13

Please sign in to comment.