From f1f5a5139dc8ac9ef706cbd699402800ec1bb1c2 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 17 Feb 2023 09:38:24 +0900 Subject: [PATCH 1/3] docs: update PHPDocs --- system/HTTP/URI.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/system/HTTP/URI.php b/system/HTTP/URI.php index 5615bb8d1287..3d673fab0928 100644 --- a/system/HTTP/URI.php +++ b/system/HTTP/URI.php @@ -532,12 +532,14 @@ public function getSegments(): array /** * Returns the value of a specific segment of the URI path. + * Allows to get only existing segments or the next one. * - * @param int $number Segment number + * @param int $number Segment number starting at 1 * @param string $default Default value * - * @return string The value of the segment. If no segment is found, - * throws InvalidArgumentError + * @return string The value of the segment. If you specify the last +1 + * segment, the $default value. If you specify the last +2 + * or more throws HTTPException. */ public function getSegment(int $number, string $default = ''): string { @@ -556,7 +558,8 @@ public function getSegment(int $number, string $default = ''): string * Set the value of a specific segment of the URI path. * Allows to set only existing segments or add new one. * - * @param mixed $value (string or int) + * @param int $number Segment number starting at 1 + * @param int|string $value * * @return $this */ From 65cd0452cb70f55db0a87a59157b004d14b61b3c Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 17 Feb 2023 09:40:21 +0900 Subject: [PATCH 2/3] fix: incorrect segment number in Exception message --- system/HTTP/URI.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/system/HTTP/URI.php b/system/HTTP/URI.php index 3d673fab0928..4a0c75ce245d 100644 --- a/system/HTTP/URI.php +++ b/system/HTTP/URI.php @@ -543,14 +543,15 @@ public function getSegments(): array */ public function getSegment(int $number, string $default = ''): string { - // The segment should treat the array as 1-based for the user - // but we still have to deal with a zero-based array. - $number--; - if ($number > count($this->segments) && ! $this->silent) { + if ($number > count($this->segments) + 1 && ! $this->silent) { throw HTTPException::forURISegmentOutOfRange($number); } + // The segment should treat the array as 1-based for the user + // but we still have to deal with a zero-based array. + $number--; + return $this->segments[$number] ?? $default; } From 391e2b9e4616ac1e42dfa578f6adb0d3dbaf2644 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 17 Feb 2023 09:40:56 +0900 Subject: [PATCH 3/3] fix: throw an exception for an impossible segment number --- system/HTTP/URI.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/system/HTTP/URI.php b/system/HTTP/URI.php index 4a0c75ce245d..7e57e4aa888c 100644 --- a/system/HTTP/URI.php +++ b/system/HTTP/URI.php @@ -543,6 +543,9 @@ public function getSegments(): array */ public function getSegment(int $number, string $default = ''): string { + if ($number < 1) { + throw HTTPException::forURISegmentOutOfRange($number); + } if ($number > count($this->segments) + 1 && ! $this->silent) { throw HTTPException::forURISegmentOutOfRange($number);