Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions system/HTTP/Exceptions/RedirectException.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
class RedirectException extends RuntimeException implements ExceptionInterface, ResponsableInterface, HTTPExceptionInterface
{
/**
* HTTP status code for redirects
*
* @var int
* Status code applied to a Response whose status is outside the 301-308 range.
*/
protected $code = 302;
protected int $defaultStatusCode = 302;
Comment thread
paulbalandan marked this conversation as resolved.

protected ?ResponseInterface $response = null;

Expand Down Expand Up @@ -61,7 +59,7 @@ public function __construct($message = '', int $code = 0, ?Throwable $previous =
}

if ($this->response->getStatusCode() < 301 || $this->response->getStatusCode() > 308) {
$this->response->setStatusCode($this->code);
$this->response->setStatusCode($this->defaultStatusCode);
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/system/HTTP/RedirectExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ public function testResponseWithoutStatusCode(): void
$this->assertSame(302, $response->getStatusCode());
}

public function testResponseWithoutStatusCodeUsesSubclassDefault(): void
{
$exception = new class (service('response')->setHeader('Location', 'location')) extends RedirectException {
protected int $defaultStatusCode = 307;
};

$response = $exception->getResponse();

$this->assertSame('location', $response->getHeaderLine('location'));
$this->assertSame(307, $response->getStatusCode());
}

public function testLoggingLocationHeader(): void
{
Time::setTestNow('2023-11-25 12:00:00');
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.8.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Behavior Changes
- **Filters:** HTTP method matching for method-based filters is now case-sensitive. The keys in ``Config\Filters::$methods`` must exactly match the request method
(e.g., ``GET``, ``POST``). Lowercase method names (e.g., ``post``) will no longer match.
- **HTTP:** Routes defined with ``$routes->add()`` now also match HTTP ``QUERY`` requests. If the route has CSRF protection, remember that CSRF verification does not protect safe methods such as ``GET`` and ``QUERY``.
- **HTTP:** ``CodeIgniter\HTTP\Exceptions\RedirectException`` no longer redeclares the inherited ``$code`` property. The status applied to a ``Response`` passed to the constructor whose status is outside the 301-308 range now comes from the new ``protected int $defaultStatusCode = 302`` property. Subclasses that overrode ``$code`` to change that status must override ``$defaultStatusCode`` instead.
- **Testing:** Tests using the ``FeatureTestTrait`` must now use uppercase HTTP method names when performing a request when using the ``call()`` method directly
(e.g., ``$this->call('GET', '/path')`` instead of ``$this->call('get', '/path')``). Additionally, setting method-based routes using ``withRoutes()`` must
also use uppercase method names (e.g., ``$this->withRoutes([['GET', 'home', 'Home::index']])``).
Expand Down
21 changes: 21 additions & 0 deletions user_guide_src/source/installation/upgrade_480.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ the URI. Such calls must now pass a ``URI``:
Any other call that omitted ``$uri`` or passed ``null`` already failed with
``Call to a member function getHost() on null``, so it needs no migration.

RedirectException Default Status Code
======================================

``CodeIgniter\HTTP\Exceptions\RedirectException`` no longer redeclares the inherited ``$code`` property to store the status applied to a
``Response`` whose status is outside the 301-308 range. If you have a subclass that overrode ``$code`` for this purpose, override the new
``$defaultStatusCode`` property instead:

.. code-block:: php

// Before
class MyRedirectException extends RedirectException
{
protected $code = 307;
}

// After
class MyRedirectException extends RedirectException
{
protected int $defaultStatusCode = 307;
}

*********************
Breaking Enhancements
*********************
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 689 errors
# total 688 errors

includes:
- argument.type.neon
Expand Down
7 changes: 1 addition & 6 deletions utils/phpstan-baseline/property.phpDocType.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 21 errors
# total 20 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -72,11 +72,6 @@ parameters:
count: 1
path: ../../system/Exceptions/PageNotFoundException.php

-
message: '#^PHPDoc type int of property CodeIgniter\\HTTP\\Exceptions\\RedirectException\:\:\$code is not the same as PHPDoc type mixed of overridden property Exception\:\:\$code\.$#'
count: 1
path: ../../system/HTTP/Exceptions/RedirectException.php

-
message: '#^PHPDoc type string of property CodeIgniter\\Session\\Handlers\\FileHandler\:\:\$savePath is not the same as PHPDoc type array\<string, mixed\>\|string of overridden property CodeIgniter\\Session\\Handlers\\BaseHandler\:\:\$savePath\.$#'
count: 1
Expand Down
Loading