From 217364dd58797ef444d9856c8bd6a5d6e6cc1219 Mon Sep 17 00:00:00 2001 From: azjezz Date: Wed, 29 May 2024 05:58:32 +0100 Subject: [PATCH] chore: fix static analysis Signed-off-by: azjezz --- docs/component/str-byte.md | 2 +- docs/component/str-grapheme.md | 2 +- src/Psl/Iter/Iterator.php | 14 +++++++------- src/Psl/Str/Byte/compare.php | 4 ++-- src/Psl/Str/Byte/compare_ci.php | 4 ++-- src/Psl/Str/Byte/length.php | 2 ++ src/Psl/Str/Byte/strip_suffix.php | 6 ++++-- src/Psl/Str/Grapheme/length.php | 6 ++++-- src/Psl/Str/Grapheme/strip_suffix.php | 6 ++++-- src/Psl/Str/length.php | 4 ++-- 10 files changed, 29 insertions(+), 21 deletions(-) diff --git a/docs/component/str-byte.md b/docs/component/str-byte.md index 33656271..ec90a940 100644 --- a/docs/component/str-byte.md +++ b/docs/component/str-byte.md @@ -30,7 +30,7 @@ - [contains_ci](./../../src/Psl/Str/Byte/contains_ci.php#L21) - [ends_with](./../../src/Psl/Str/Byte/ends_with.php#L12) - [ends_with_ci](./../../src/Psl/Str/Byte/ends_with_ci.php#L14) -- [length](./../../src/Psl/Str/Byte/length.php#L14) +- [length](./../../src/Psl/Str/Byte/length.php#L16) - [lowercase](./../../src/Psl/Str/Byte/lowercase.php#L14) - [ord](./../../src/Psl/Str/Byte/ord.php#L12) - [pad_left](./../../src/Psl/Str/Byte/pad_left.php#L24) diff --git a/docs/component/str-grapheme.md b/docs/component/str-grapheme.md index 3ff113d0..fe457b29 100644 --- a/docs/component/str-grapheme.md +++ b/docs/component/str-grapheme.md @@ -24,7 +24,7 @@ - [contains_ci](./../../src/Psl/Str/Grapheme/contains_ci.php#L22) - [ends_with](./../../src/Psl/Str/Grapheme/ends_with.php#L16) - [ends_with_ci](./../../src/Psl/Str/Grapheme/ends_with_ci.php#L16) -- [length](./../../src/Psl/Str/Grapheme/length.php#L18) +- [length](./../../src/Psl/Str/Grapheme/length.php#L20) - [range](./../../src/Psl/Str/Grapheme/range.php#L43) - [reverse](./../../src/Psl/Str/Grapheme/reverse.php#L16) - [search](./../../src/Psl/Str/Grapheme/search.php#L27) diff --git a/src/Psl/Iter/Iterator.php b/src/Psl/Iter/Iterator.php index 58ac0a55..266a6a41 100644 --- a/src/Psl/Iter/Iterator.php +++ b/src/Psl/Iter/Iterator.php @@ -164,14 +164,14 @@ public function rewind(): void /** * Seek to the given position. * - * @param int<0, max> $offset + * @param int<0, max> $position * * @throws Exception\OutOfBoundsException If $position is out-of-bounds. */ - public function seek(int $offset): void + public function seek(int $position): void { - if ($offset <= $this->position) { - $this->position = $offset; + if ($position <= $this->position) { + $this->position = $position; return; } @@ -184,16 +184,16 @@ public function seek(int $offset): void $this->generator = null; throw new Exception\OutOfBoundsException('Position is out-of-bounds.'); } - } while ($this->position < $offset); + } while ($this->position < $position); return; } - if ($offset >= $this->count()) { + if ($position >= $this->count()) { throw new Exception\OutOfBoundsException('Position is out-of-bounds.'); } - $this->position = $offset; + $this->position = $position; } /** diff --git a/src/Psl/Str/Byte/compare.php b/src/Psl/Str/Byte/compare.php index 38c9061a..777c87ce 100644 --- a/src/Psl/Str/Byte/compare.php +++ b/src/Psl/Str/Byte/compare.php @@ -11,8 +11,8 @@ * Returns < 0 if `$string1` is less than `$string2`, > 0 if `$string1` is * greater than `$string2`, and 0 if they are equal. * - * @param int|null $length number of characters to use in the comparison, - * or null to compare the whole string + * @param int<0, max>|null $length number of characters to use in the comparison, + * or null to compare the whole string * * @pure */ diff --git a/src/Psl/Str/Byte/compare_ci.php b/src/Psl/Str/Byte/compare_ci.php index e8cae868..6eeeb529 100644 --- a/src/Psl/Str/Byte/compare_ci.php +++ b/src/Psl/Str/Byte/compare_ci.php @@ -13,8 +13,8 @@ * * @pure * - * @param int|null $length number of characters to use in the comparison, - * or null to compare the whole string + * @param int<0, max>|null $length number of characters to use in the comparison, + * or null to compare the whole string */ function compare_ci(string $string, string $other, ?int $length = null): int { diff --git a/src/Psl/Str/Byte/length.php b/src/Psl/Str/Byte/length.php index 23140c25..a1f64f26 100644 --- a/src/Psl/Str/Byte/length.php +++ b/src/Psl/Str/Byte/length.php @@ -9,6 +9,8 @@ /** * Returns the length of the given string, i.e. the number of bytes. * + * @return int<0, max> + * * @pure */ function length(string $string): int diff --git a/src/Psl/Str/Byte/strip_suffix.php b/src/Psl/Str/Byte/strip_suffix.php index bee86ae8..317062b6 100644 --- a/src/Psl/Str/Byte/strip_suffix.php +++ b/src/Psl/Str/Byte/strip_suffix.php @@ -35,7 +35,9 @@ function strip_suffix(string $string, string $suffix): string /** * $string_length is greater than $suffix_length, so the result is always int<0, max>. * - * @psalm-suppress ArgumentTypeCoercion + * @var int<0, max> $length */ - return slice($string, 0, $string_length - $suffix_length); + $length = $string_length - $suffix_length; + + return slice($string, 0, $length); } diff --git a/src/Psl/Str/Grapheme/length.php b/src/Psl/Str/Grapheme/length.php index 191354d6..34915deb 100644 --- a/src/Psl/Str/Grapheme/length.php +++ b/src/Psl/Str/Grapheme/length.php @@ -11,9 +11,11 @@ /** * Returns the length of the given string in grapheme units. * - * @pure - * * @throws Exception\InvalidArgumentException If $string is not made of grapheme clusters. + * + * @return int<0, max> + * + * @pure */ function length(string $string): int { diff --git a/src/Psl/Str/Grapheme/strip_suffix.php b/src/Psl/Str/Grapheme/strip_suffix.php index bdbb50fe..4f51c441 100644 --- a/src/Psl/Str/Grapheme/strip_suffix.php +++ b/src/Psl/Str/Grapheme/strip_suffix.php @@ -39,7 +39,9 @@ function strip_suffix(string $string, string $suffix): string /** * $string_length is greater than $suffix_length, so the result is always int<0, max>. * - * @psalm-suppress ArgumentTypeCoercion + * @var int<0, max> $length */ - return slice($string, 0, $string_length - $suffix_length); + $length = $string_length - $suffix_length; + + return slice($string, 0, $length); } diff --git a/src/Psl/Str/length.php b/src/Psl/Str/length.php index afc3f0a7..9491ba76 100644 --- a/src/Psl/Str/length.php +++ b/src/Psl/Str/length.php @@ -20,9 +20,9 @@ * Str\length('تونس') * => Int(4) * - * @pure - * * @return int<0, max> + * + * @pure */ function length(string $string, Encoding $encoding = Encoding::Utf8): int {