fix:更新已知bug,优化代码

This commit is contained in:
Ying
2022-11-28 19:11:12 +08:00
parent f6aee95cfc
commit 9445b206a2
1378 changed files with 53759 additions and 20789 deletions

View File

@@ -92,7 +92,7 @@ class ByteString extends AbstractString
return '' === $str ? [] : [\ord($str)];
}
public function append(string ...$suffix): parent
public function append(string ...$suffix): static
{
$str = clone $this;
$str->string .= 1 >= \count($suffix) ? ($suffix[0] ?? '') : implode('', $suffix);
@@ -100,10 +100,13 @@ class ByteString extends AbstractString
return $str;
}
public function camel(): parent
public function camel(): static
{
$str = clone $this;
$str->string = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $this->string))));
$parts = explode(' ', trim(ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $this->string))));
$parts[0] = 1 !== \strlen($parts[0]) && ctype_upper($parts[0]) ? $parts[0] : lcfirst($parts[0]);
$str->string = implode('', $parts);
return $str;
}
@@ -129,27 +132,23 @@ class ByteString extends AbstractString
return $chunks;
}
public function endsWith($suffix): bool
public function endsWith(string|iterable|AbstractString $suffix): bool
{
if ($suffix instanceof parent) {
if ($suffix instanceof AbstractString) {
$suffix = $suffix->string;
} elseif (\is_array($suffix) || $suffix instanceof \Traversable) {
} elseif (!\is_string($suffix)) {
return parent::endsWith($suffix);
} else {
$suffix = (string) $suffix;
}
return '' !== $suffix && \strlen($this->string) >= \strlen($suffix) && 0 === substr_compare($this->string, $suffix, -\strlen($suffix), null, $this->ignoreCase);
}
public function equalsTo($string): bool
public function equalsTo(string|iterable|AbstractString $string): bool
{
if ($string instanceof parent) {
if ($string instanceof AbstractString) {
$string = $string->string;
} elseif (\is_array($string) || $string instanceof \Traversable) {
} elseif (!\is_string($string)) {
return parent::equalsTo($string);
} else {
$string = (string) $string;
}
if ('' !== $string && $this->ignoreCase) {
@@ -159,7 +158,7 @@ class ByteString extends AbstractString
return $string === $this->string;
}
public function folded(): parent
public function folded(): static
{
$str = clone $this;
$str->string = strtolower($str->string);
@@ -167,14 +166,12 @@ class ByteString extends AbstractString
return $str;
}
public function indexOf($needle, int $offset = 0): ?int
public function indexOf(string|iterable|AbstractString $needle, int $offset = 0): ?int
{
if ($needle instanceof parent) {
if ($needle instanceof AbstractString) {
$needle = $needle->string;
} elseif (\is_array($needle) || $needle instanceof \Traversable) {
} elseif (!\is_string($needle)) {
return parent::indexOf($needle, $offset);
} else {
$needle = (string) $needle;
}
if ('' === $needle) {
@@ -186,14 +183,12 @@ class ByteString extends AbstractString
return false === $i ? null : $i;
}
public function indexOfLast($needle, int $offset = 0): ?int
public function indexOfLast(string|iterable|AbstractString $needle, int $offset = 0): ?int
{
if ($needle instanceof parent) {
if ($needle instanceof AbstractString) {
$needle = $needle->string;
} elseif (\is_array($needle) || $needle instanceof \Traversable) {
} elseif (!\is_string($needle)) {
return parent::indexOfLast($needle, $offset);
} else {
$needle = (string) $needle;
}
if ('' === $needle) {
@@ -210,7 +205,7 @@ class ByteString extends AbstractString
return '' === $this->string || preg_match('//u', $this->string);
}
public function join(array $strings, string $lastGlue = null): parent
public function join(array $strings, string $lastGlue = null): static
{
$str = clone $this;
@@ -225,7 +220,7 @@ class ByteString extends AbstractString
return \strlen($this->string);
}
public function lower(): parent
public function lower(): static
{
$str = clone $this;
$str->string = strtolower($str->string);
@@ -262,7 +257,7 @@ class ByteString extends AbstractString
return $matches;
}
public function padBoth(int $length, string $padStr = ' '): parent
public function padBoth(int $length, string $padStr = ' '): static
{
$str = clone $this;
$str->string = str_pad($this->string, $length, $padStr, \STR_PAD_BOTH);
@@ -270,7 +265,7 @@ class ByteString extends AbstractString
return $str;
}
public function padEnd(int $length, string $padStr = ' '): parent
public function padEnd(int $length, string $padStr = ' '): static
{
$str = clone $this;
$str->string = str_pad($this->string, $length, $padStr, \STR_PAD_RIGHT);
@@ -278,7 +273,7 @@ class ByteString extends AbstractString
return $str;
}
public function padStart(int $length, string $padStr = ' '): parent
public function padStart(int $length, string $padStr = ' '): static
{
$str = clone $this;
$str->string = str_pad($this->string, $length, $padStr, \STR_PAD_LEFT);
@@ -286,7 +281,7 @@ class ByteString extends AbstractString
return $str;
}
public function prepend(string ...$prefix): parent
public function prepend(string ...$prefix): static
{
$str = clone $this;
$str->string = (1 >= \count($prefix) ? ($prefix[0] ?? '') : implode('', $prefix)).$str->string;
@@ -294,7 +289,7 @@ class ByteString extends AbstractString
return $str;
}
public function replace(string $from, string $to): parent
public function replace(string $from, string $to): static
{
$str = clone $this;
@@ -305,21 +300,13 @@ class ByteString extends AbstractString
return $str;
}
public function replaceMatches(string $fromRegexp, $to): parent
public function replaceMatches(string $fromRegexp, string|callable $to): static
{
if ($this->ignoreCase) {
$fromRegexp .= 'i';
}
if (\is_array($to)) {
if (!\is_callable($to)) {
throw new \TypeError(sprintf('Argument 2 passed to "%s::replaceMatches()" must be callable, array given.', static::class));
}
$replace = 'preg_replace_callback';
} else {
$replace = $to instanceof \Closure ? 'preg_replace_callback' : 'preg_replace';
}
$replace = \is_array($to) || $to instanceof \Closure ? 'preg_replace_callback' : 'preg_replace';
set_error_handler(static function ($t, $m) { throw new InvalidArgumentException($m); });
@@ -345,7 +332,7 @@ class ByteString extends AbstractString
return $str;
}
public function reverse(): parent
public function reverse(): static
{
$str = clone $this;
$str->string = strrev($str->string);
@@ -353,7 +340,7 @@ class ByteString extends AbstractString
return $str;
}
public function slice(int $start = 0, int $length = null): parent
public function slice(int $start = 0, int $length = null): static
{
$str = clone $this;
$str->string = (string) substr($this->string, $start, $length ?? \PHP_INT_MAX);
@@ -361,15 +348,15 @@ class ByteString extends AbstractString
return $str;
}
public function snake(): parent
public function snake(): static
{
$str = $this->camel()->title();
$str = $this->camel();
$str->string = strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1_\2', $str->string));
return $str;
}
public function splice(string $replacement, int $start = 0, int $length = null): parent
public function splice(string $replacement, int $start = 0, int $length = null): static
{
$str = clone $this;
$str->string = substr_replace($this->string, $replacement, $start, $length ?? \PHP_INT_MAX);
@@ -404,9 +391,9 @@ class ByteString extends AbstractString
return $chunks;
}
public function startsWith($prefix): bool
public function startsWith(string|iterable|AbstractString $prefix): bool
{
if ($prefix instanceof parent) {
if ($prefix instanceof AbstractString) {
$prefix = $prefix->string;
} elseif (!\is_string($prefix)) {
return parent::startsWith($prefix);
@@ -415,7 +402,7 @@ class ByteString extends AbstractString
return '' !== $prefix && 0 === ($this->ignoreCase ? strncasecmp($this->string, $prefix, \strlen($prefix)) : strncmp($this->string, $prefix, \strlen($prefix)));
}
public function title(bool $allWords = false): parent
public function title(bool $allWords = false): static
{
$str = clone $this;
$str->string = $allWords ? ucwords($str->string) : ucfirst($str->string);
@@ -465,7 +452,7 @@ class ByteString extends AbstractString
return $u;
}
public function trim(string $chars = " \t\n\r\0\x0B\x0C"): parent
public function trim(string $chars = " \t\n\r\0\x0B\x0C"): static
{
$str = clone $this;
$str->string = trim($str->string, $chars);
@@ -473,7 +460,7 @@ class ByteString extends AbstractString
return $str;
}
public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C"): parent
public function trimEnd(string $chars = " \t\n\r\0\x0B\x0C"): static
{
$str = clone $this;
$str->string = rtrim($str->string, $chars);
@@ -481,7 +468,7 @@ class ByteString extends AbstractString
return $str;
}
public function trimStart(string $chars = " \t\n\r\0\x0B\x0C"): parent
public function trimStart(string $chars = " \t\n\r\0\x0B\x0C"): static
{
$str = clone $this;
$str->string = ltrim($str->string, $chars);
@@ -489,7 +476,7 @@ class ByteString extends AbstractString
return $str;
}
public function upper(): parent
public function upper(): static
{
$str = clone $this;
$str->string = strtoupper($str->string);