Skip to content

Commit 0230c8b

Browse files
authored
Merge pull request #7251 from kenjis/fix-URI-setSegment
fix: URI::setSegment() accepts the last +2 segment without Exception
2 parents 51f095e + d4e7426 commit 0230c8b

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

system/HTTP/URI.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ public function getSegment(int $number, string $default = ''): string
562562
*/
563563
public function setSegment(int $number, $value)
564564
{
565-
// The segment should treat the array as 1-based for the user
566-
// but we still have to deal with a zero-based array.
567-
$number--;
565+
if ($number < 1) {
566+
throw HTTPException::forURISegmentOutOfRange($number);
567+
}
568568

569569
if ($number > count($this->segments) + 1) {
570570
if ($this->silent) {
@@ -574,6 +574,10 @@ public function setSegment(int $number, $value)
574574
throw HTTPException::forURISegmentOutOfRange($number);
575575
}
576576

577+
// The segment should treat the array as 1-based for the user
578+
// but we still have to deal with a zero-based array.
579+
$number--;
580+
577581
$this->segments[$number] = $value;
578582
$this->refreshPath();
579583

tests/system/HTTP/URITest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -808,32 +808,37 @@ public function testSetSegment()
808808
$this->assertSame('foo/banana/baz', $uri->getPath());
809809
}
810810

811-
public function testSetSegmentFallback()
811+
public function testSetSegmentNewOne()
812812
{
813813
$base = 'http://example.com';
814+
$uri = new URI($base);
814815

815-
$uri = new URI($base);
816+
// Can set the next segment.
816817
$uri->setSegment(1, 'first');
817-
$uri->setSegment(3, 'third');
818+
// Can set the next segment.
819+
$uri->setSegment(2, 'third');
818820

819821
$this->assertSame('first/third', $uri->getPath());
820822

823+
// Can replace the existing segment.
821824
$uri->setSegment(2, 'second');
822825

823826
$this->assertSame('first/second', $uri->getPath());
824827

828+
// Can set the next segment.
825829
$uri->setSegment(3, 'third');
826830

827831
$this->assertSame('first/second/third', $uri->getPath());
828832

829-
$uri->setSegment(5, 'fifth');
833+
// Can set the next segment.
834+
$uri->setSegment(4, 'fourth');
830835

831-
$this->assertSame('first/second/third/fifth', $uri->getPath());
836+
$this->assertSame('first/second/third/fourth', $uri->getPath());
832837

833-
// sixth or seventh was not set
838+
// Cannot set the next next segment.
834839
$this->expectException(HTTPException::class);
835840

836-
$uri->setSegment(8, 'eighth');
841+
$uri->setSegment(6, 'six');
837842
}
838843

839844
public function testSetBadSegment()

user_guide_src/source/changelogs/v4.4.0.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ BREAKING
2020
Behavior Changes
2121
================
2222

23+
URI::setSegment() and Non-Existent Segment
24+
------------------------------------------
25+
26+
An exception is now thrown when you set the last ``+2`` segment.
27+
In previous versions, an exception was thrown only if the last segment ``+3``
28+
or more was specified. See :ref:`upgrade-440-uri-setsegment`.
29+
30+
The next segment (``+1``) of the current last segment can be set as before.
31+
2332
Interface Changes
2433
=================
2534

user_guide_src/source/installation/upgrade_440.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ Please refer to the upgrade instructions corresponding to your installation meth
1515
Breaking Changes
1616
****************
1717

18+
.. _upgrade-440-uri-setsegment:
19+
20+
URI::setSegment() Change
21+
========================
22+
23+
Dut to a bug, in previous versions an exception was not thrown if the last segment
24+
``+2`` was specified. This bug has been fixed.
25+
26+
If your code depends on this bug, fix the segment number.
27+
28+
.. literalinclude:: upgrade_440/002.php
29+
:lines: 2-
30+
1831
Mandatory File Changes
1932
**********************
2033

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
// URI: http://example.com/one/two
4+
5+
// Before:
6+
$uri->setSegment(4, 'three');
7+
// The URI will be http://example.com/one/two/three
8+
9+
// After:
10+
$uri->setSegment(4, 'three'); // Will throw Exception
11+
$uri->setSegment(3, 'three');
12+
// The URI will be http://example.com/one/two/three

0 commit comments

Comments
 (0)