Skip to content

Commit

Permalink
Use str_starts_with()/str_ends_with() instead of substr()
Browse files Browse the repository at this point in the history
  • Loading branch information
zonuexe committed Apr 16, 2024
1 parent ad7551c commit 01b1c59
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
14 changes: 6 additions & 8 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function ltrim;
use function preg_match;
use function rtrim;
use function str_ends_with;
use function str_replace;
use function str_starts_with;
use function strlen;
Expand Down Expand Up @@ -36,14 +37,13 @@ public function getWorkingDirectory(): string
public function absolutizePath(string $path): string
{
if (DIRECTORY_SEPARATOR === '/') {
if (substr($path, 0, 1) === '/') {
return $path;
}
} else {
if (substr($path, 1, 1) === ':') {
if (str_starts_with($path, '/')) {
return $path;
}
} elseif (substr($path, 1, 1) === ':') {
return $path;
}

if (preg_match('~^[a-z0-9+\-.]+://~i', $path) === 1) {
return $path;
}
Expand Down Expand Up @@ -87,12 +87,10 @@ public function normalizePath(string $originalPath, string $directorySeparator =
continue;
}
if ($pathPart === '..') {
/** @var string $removedPart */
$removedPart = array_pop($normalizedPathParts);
if ($scheme === 'phar' && substr($removedPart, -5) === '.phar') {
if ($scheme === 'phar' && $removedPart !== null && str_ends_with($removedPart, '.phar')) {
$scheme = null;
}

} else {
$normalizedPathParts[] = $pathPart;
}
Expand Down
2 changes: 1 addition & 1 deletion src/File/FuzzyRelativePathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(
$pathBeginning = null;
$pathToTrimArray = null;
$trimBeginning = static function (string $path): array {
if (substr($path, 0, 1) === '/') {
if (str_starts_with($path, '/')) {
return [
'/',
substr($path, 1),
Expand Down
3 changes: 2 additions & 1 deletion src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use function array_intersect_key;
use function array_map;
use function array_shift;
use function str_starts_with;
use function array_unique;
use function array_values;
use function count;
Expand Down Expand Up @@ -388,7 +389,7 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
}

if (
substr($typeDescription, 0, strlen('array<')) === 'array<'
str_starts_with($typeDescription, 'array<')
&& in_array('array', $skipTypeNames, true)
) {
$nonEmpty = false;
Expand Down

0 comments on commit 01b1c59

Please sign in to comment.