Skip to content

Commit 85434df

Browse files
authored
Merge pull request #8994 from kenjis/normalizer-filters-filter-args
fix: [Filters] normalize `$filters` arguments
2 parents adc6598 + f3e5504 commit 85434df

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5257,12 +5257,6 @@
52575257
'count' => 1,
52585258
'path' => __DIR__ . '/system/Filters/Filters.php',
52595259
];
5260-
$ignoreErrors[] = [
5261-
// identifier: missingType.iterableValue
5262-
'message' => '#^Method CodeIgniter\\\\Filters\\\\Filters\\:\\:enableFilters\\(\\) has parameter \\$names with no value type specified in iterable type array\\.$#',
5263-
'count' => 1,
5264-
'path' => __DIR__ . '/system/Filters/Filters.php',
5265-
];
52665260
$ignoreErrors[] = [
52675261
// identifier: missingType.iterableValue
52685262
'message' => '#^Method CodeIgniter\\\\Filters\\\\Filters\\:\\:getRequiredFilters\\(\\) return type has no value type specified in iterable type array\\.$#',

system/Filters/Filters.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,14 @@ public function addFilter(string $class, ?string $alias = null, string $position
574574
* after the filter name, followed by a comma-separated list of arguments that
575575
* are passed to the filter when executed.
576576
*
577-
* @param string $name filter_name or filter_name:arguments like 'role:admin,manager'
577+
* @param string $filter filter_name or filter_name:arguments like 'role:admin,manager'
578578
* or filter classname.
579579
* @phpstan-param 'before'|'after' $position
580580
*/
581-
private function enableFilter(string $name, string $position = 'before'): void
581+
private function enableFilter(string $filter, string $position = 'before'): void
582582
{
583583
// Normalize the arguments.
584-
[$alias, $arguments] = $this->getCleanName($name);
584+
[$alias, $arguments] = $this->getCleanName($filter);
585585
$filter = ($arguments === []) ? $alias : $alias . ':' . implode(',', $arguments);
586586

587587
if (class_exists($alias)) {
@@ -602,24 +602,26 @@ private function enableFilter(string $name, string $position = 'before'): void
602602
/**
603603
* Get clean name and arguments
604604
*
605-
* @param string $name filter_name or filter_name:arguments like 'role:admin,manager'
605+
* @param string $filter filter_name or filter_name:arguments like 'role:admin,manager'
606606
*
607607
* @return array{0: string, 1: list<string>} [name, arguments]
608608
*/
609-
private function getCleanName(string $name): array
609+
private function getCleanName(string $filter): array
610610
{
611611
$arguments = [];
612612

613-
if (str_contains($name, ':')) {
614-
[$name, $arguments] = explode(':', $name);
615-
616-
$arguments = explode(',', $arguments);
617-
array_walk($arguments, static function (&$item) {
618-
$item = trim($item);
619-
});
613+
if (! str_contains($filter, ':')) {
614+
return [$filter, $arguments];
620615
}
621616

622-
return [$name, $arguments];
617+
[$alias, $arguments] = explode(':', $filter);
618+
619+
$arguments = explode(',', $arguments);
620+
array_walk($arguments, static function (&$item) {
621+
$item = trim($item);
622+
});
623+
624+
return [$alias, $arguments];
623625
}
624626

625627
/**
@@ -629,13 +631,13 @@ private function getCleanName(string $name): array
629631
* after the filter name, followed by a comma-separated list of arguments that
630632
* are passed to the filter when executed.
631633
*
632-
* @params array<string> $names filter_name or filter_name:arguments like 'role:admin,manager'
634+
* @param list<string> $filters filter_name or filter_name:arguments like 'role:admin,manager'
633635
*
634636
* @return Filters
635637
*/
636-
public function enableFilters(array $names, string $when = 'before')
638+
public function enableFilters(array $filters, string $when = 'before')
637639
{
638-
foreach ($names as $filter) {
640+
foreach ($filters as $filter) {
639641
$this->enableFilter($filter, $when);
640642
}
641643

@@ -777,21 +779,25 @@ protected function processFilters(?string $uri = null)
777779
// Add any filters that apply to this URI
778780
$filters = [];
779781

780-
foreach ($this->config->filters as $alias => $settings) {
782+
foreach ($this->config->filters as $filter => $settings) {
783+
// Normalize the arguments.
784+
[$alias, $arguments] = $this->getCleanName($filter);
785+
$filter = ($arguments === []) ? $alias : $alias . ':' . implode(',', $arguments);
786+
781787
// Look for inclusion rules
782788
if (isset($settings['before'])) {
783789
$path = $settings['before'];
784790

785791
if ($this->pathApplies($uri, $path)) {
786-
$filters['before'][] = $alias;
792+
$filters['before'][] = $filter;
787793
}
788794
}
789795

790796
if (isset($settings['after'])) {
791797
$path = $settings['after'];
792798

793799
if ($this->pathApplies($uri, $path)) {
794-
$filters['after'][] = $alias;
800+
$filters['after'][] = $filter;
795801
}
796802
}
797803
}

tests/system/Filters/FiltersTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,18 @@ public function testProcessMethodProcessesFiltersBefore(): void
282282
'before' => ['admin/*'],
283283
'after' => ['/users/*'],
284284
],
285+
'bar: arg1, arg2' => [
286+
'before' => ['admin/*'],
287+
'after' => ['/users/*'],
288+
],
285289
],
286290
];
287291
$filtersConfig = $this->createConfigFromArray(FiltersConfig::class, $config);
288292
$filters = $this->createFilters($filtersConfig);
289293

290294
$uri = 'admin/foo/bar';
291295
$expected = [
292-
'before' => ['foo'],
296+
'before' => ['foo', 'bar:arg1,arg2'],
293297
'after' => [],
294298
];
295299
$this->assertSame($expected, $filters->initialize($uri)->getFilters());
@@ -311,6 +315,10 @@ public function testProcessMethodProcessesFiltersAfter(): void
311315
'before' => ['admin/*'],
312316
'after' => ['/users/*'],
313317
],
318+
'bar: arg1, arg2' => [
319+
'before' => ['admin/*'],
320+
'after' => ['/users/*'],
321+
],
314322
],
315323
];
316324
$filtersConfig = $this->createConfigFromArray(FiltersConfig::class, $config);
@@ -319,9 +327,7 @@ public function testProcessMethodProcessesFiltersAfter(): void
319327
$uri = 'users/foo/bar';
320328
$expected = [
321329
'before' => [],
322-
'after' => [
323-
'foo',
324-
],
330+
'after' => ['bar:arg1,arg2', 'foo'],
325331
];
326332
$this->assertSame($expected, $filters->initialize($uri)->getFilters());
327333
}

0 commit comments

Comments
 (0)