From 72f76a6a3930255c5a9531e40f90fa73b7ae6b3d Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 17 Feb 2023 10:01:03 +0900 Subject: [PATCH] fix: remove unneeded methods getSegment() accepts n+1. --- system/HTTP/SiteURI.php | 64 +++++-------------------------- tests/system/HTTP/SiteURITest.php | 2 +- 2 files changed, 10 insertions(+), 56 deletions(-) diff --git a/system/HTTP/SiteURI.php b/system/HTTP/SiteURI.php index 6b07bca62aa1..6a2d7f594af0 100644 --- a/system/HTTP/SiteURI.php +++ b/system/HTTP/SiteURI.php @@ -192,21 +192,17 @@ public function getRoutePath(): string } /** - * Returns the URI segments of the path as an array. - */ - public function getSegments(): array - { - return $this->segments; - } - - /** - * Returns the value of a specific segment of the URI path relative to baseURL. + * 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 HTTPException + * @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. + * + * @TODO remove this method after merging #7267 */ public function getSegment(int $number, string $default = ''): string { @@ -214,7 +210,7 @@ public function getSegment(int $number, string $default = ''): string throw HTTPException::forURISegmentOutOfRange($number); } - if ($number > count($this->segments) && ! $this->silent) { + if ($number > count($this->segments) + 1 && ! $this->silent) { throw HTTPException::forURISegmentOutOfRange($number); } @@ -225,48 +221,6 @@ public function getSegment(int $number, string $default = ''): string return $this->segments[$number] ?? $default; } - /** - * Set the value of a specific segment of the URI path relative to baseURL. - * Allows to set only existing segments or add new one. - * - * @param int $number The segment number. Starting with 1. - * @param string $value The segment value. - * - * @return $this - */ - public function setSegment(int $number, $value) - { - if ($number < 1) { - throw HTTPException::forURISegmentOutOfRange($number); - } - - if ($number > count($this->segments) + 1) { - if ($this->silent) { - return $this; - } - - 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--; - - $this->segments[$number] = $value; - - $this->refreshPath(); - - return $this; - } - - /** - * Returns the total number of segments. - */ - public function getTotalSegments(): int - { - return count($this->segments); - } - /** * Formats the URI as a string. */ diff --git a/tests/system/HTTP/SiteURITest.php b/tests/system/HTTP/SiteURITest.php index bdc6679bacc1..e670e023b9f4 100644 --- a/tests/system/HTTP/SiteURITest.php +++ b/tests/system/HTTP/SiteURITest.php @@ -285,7 +285,7 @@ public function testGetSegmentOutOfRange() $uri = new SiteURI($config); $uri->setPath('test/method'); - $uri->getSegment(3); + $uri->getSegment(4); } public function testGetTotalSegments()