Skip to content

Commit 3de11a0

Browse files
authored
fix: preserve subclass types in route group callbacks (#10644)
1 parent 4791433 commit 3de11a0

4 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎system/Router/RouteCollection.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,8 @@ public function getRedirectCode(string $routeKey): int
656656
* $route->resource('users');
657657
* });
658658
*
659-
* @param string $name The name to group/prefix the routes with.
660-
* @param array<string, mixed>|(callable(self): void) ...$params
659+
* @param string $name The name to group/prefix the routes with.
660+
* @param array<string, mixed>|(callable(static): void) ...$params
661661
*
662662
* @return void
663663
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Router;
15+
16+
use CodeIgniter\Router\RouteCollection;
17+
18+
final class RouteCollectionSubclass extends RouteCollection
19+
{
20+
}

‎tests/system/Router/RouteCollectionTest.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use PHPUnit\Framework\Attributes\DataProvider;
2828
use PHPUnit\Framework\Attributes\Group;
2929
use Tests\Support\Controllers\Hello;
30+
use Tests\Support\Router\RouteCollectionSubclass;
3031

3132
/**
3233
* @internal
@@ -314,6 +315,38 @@ static function ($routes): void {
314315
$this->assertSame($expected, $routes->getRoutes());
315316
}
316317

318+
public function testGroupingPassesSubclassToNestedCallbacks(): void
319+
{
320+
$modules = new Modules();
321+
$modules->enabled = false;
322+
$config = new Routing();
323+
$config->defaultNamespace = '\\';
324+
$routes = new RouteCollectionSubclass(service('locator'), $modules, $config);
325+
$routes->setHTTPVerb(Method::GET);
326+
327+
$routes->group('admin', ['namespace' => 'Admin', 'filter' => ['csrf']], function (RouteCollectionSubclass $group) use ($routes): void {
328+
$this->assertSame($routes, $group);
329+
330+
$group->group('profile', ['filter' => ['honeypot']], function (RouteCollectionSubclass $nested) use ($routes): void {
331+
$this->assertSame($routes, $nested);
332+
$nested->get('show', 'Profile::show');
333+
});
334+
335+
$group->get('dashboard', 'Dashboard::index');
336+
});
337+
338+
$routes->get('home', 'Home::index');
339+
340+
$this->assertSame([
341+
'admin/profile/show' => '\\Admin\\Profile::show',
342+
'admin/dashboard' => '\\Admin\\Dashboard::index',
343+
'home' => '\\Home::index',
344+
], $routes->getRoutes(Method::GET));
345+
$this->assertSame(['csrf', 'honeypot'], $routes->getFiltersForRoute('admin/profile/show'));
346+
$this->assertSame(['csrf'], $routes->getFiltersForRoute('admin/dashboard'));
347+
$this->assertFalse($routes->isFiltered('home'));
348+
}
349+
317350
public function testGroupGetsSanitized(): void
318351
{
319352
$routes = $this->getCollector();

‎user_guide_src/source/changelogs/v4.7.6.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Bugs Fixed
5252
- **HTTP:** Fixed a bug where ``Negotiate`` could select a value that the request header excluded with ``q=0`` through a wildcard or a broader range. A ``q=0`` range now excludes every value it matches, such as ``text/html`` and ``text/html;charset=utf-8`` for ``text/*;q=0``, or ``en-US`` for ``en;q=0``, unless a more specific range in the header accepts the value. For example, ``Accept-Encoding: gzip;q=0, *`` no longer selects ``gzip``.
5353
- **HTTP:** Fixed stale ``$_REQUEST`` values after ``SiteURIFactory`` normalizes GET parameters, so ``Validation::withRequest()`` reads the normalized values while unrelated REQUEST entries are preserved.
5454
- **I18n:** Fixed a regression in v4.7.1 where ``Time`` and ``TimeLegacy`` lost their timezone, locale and ``__toString()`` format after ``unserialize()``, so methods such as ``difference()`` and ``getTimezoneName()`` threw an ``Error``.
55+
- **Routing:** Corrected the PHPDoc callback type of ``RouteCollection::group()`` to preserve the concrete collection type, allowing callbacks typed to a ``RouteCollection`` subclass to pass static analysis.
5556
- **Security:** Fixed a bug where a valid JSON request body that is not an object (for example a JSON array) was corrupted after CSRF verification when the token was sent via the ``X-CSRF-TOKEN`` header.
5657

5758
See the repo's

0 commit comments

Comments
 (0)