Skip to content

Commit 259abaa

Browse files
committed
refactor: fix phpstan errors in Filters
1 parent 590d8eb commit 259abaa

14 files changed

Lines changed: 89 additions & 183 deletions

system/Filters/Filters.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ public function runRequired(string $position = 'before')
369369
*
370370
* @param 'after'|'before' $position
371371
*
372+
* @return array{list<string>, array<string, class-string|list<class-string>>}
373+
*
372374
* @internal
373375
*/
374376
public function getRequiredFilters(string $position = 'before'): array
@@ -410,7 +412,9 @@ public function getRequiredFilters(string $position = 'before'): array
410412
* Set the toolbar filter to the last position to be executed.
411413
*
412414
* @param list<string> $filters `after` filter array
413-
* @param bool $remove if true, remove `toolbar` filter
415+
* @param bool $remove If true, remove `toolbar` filter
416+
*
417+
* @return list<string>
414418
*/
415419
private function setToolbarToLast(array $filters, bool $remove = false): array
416420
{
@@ -853,8 +857,8 @@ protected function processAliasesToClass(string $position)
853857
/**
854858
* Check paths for match for URI
855859
*
856-
* @param string $uri URI to test against
857-
* @param array|string $paths The path patterns to test
860+
* @param string $uri URI to test against
861+
* @param array<array-key, string>|string $paths The path patterns to test
858862
*
859863
* @return bool True if any of the paths apply to the URI
860864
*/
@@ -876,8 +880,8 @@ private function pathApplies(string $uri, $paths)
876880
/**
877881
* Check except paths
878882
*
879-
* @param string $uri URI path relative to baseURL (all lowercase)
880-
* @param array|string $paths The except path patterns
883+
* @param string $uri URI path relative to baseURL (all lowercase)
884+
* @param array<array-key, string>|string $paths The except path patterns
881885
*
882886
* @return bool True if the URI matches except paths.
883887
*/
@@ -899,8 +903,8 @@ private function checkExcept(string $uri, $paths): bool
899903
/**
900904
* Check the URI path as pseudo-regex
901905
*
902-
* @param string $uri URI path relative to baseURL (all lowercase, URL-decoded)
903-
* @param array $paths The except path patterns
906+
* @param string $uri URI path relative to baseURL (all lowercase, URL-decoded)
907+
* @param array<array-key, string> $paths The except path patterns
904908
*/
905909
private function checkPseudoRegex(string $uri, array $paths): bool
906910
{

system/Filters/ForceHTTPS.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ForceHTTPS implements FilterInterface
3030
* as set the HTTP Strict Transport Security (HSTS) header for those browsers
3131
* that support it.
3232
*
33-
* @param array|null $arguments
33+
* @param list<string>|null $arguments
3434
*
3535
* @return ResponseInterface|null
3636
*/
@@ -56,7 +56,7 @@ public function before(RequestInterface $request, $arguments = null)
5656
/**
5757
* We don't have anything to do here.
5858
*
59-
* @param array|null $arguments
59+
* @param list<string>|null $arguments
6060
*/
6161
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
6262
{

system/Filters/InvalidChars.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
8484
/**
8585
* Check the character encoding is valid UTF-8.
8686
*
87-
* @param array|string $value
87+
* @param array<array-key, mixed>|string $value
8888
*
89-
* @return array|string
89+
* @return array<array-key, mixed>|string
9090
*
9191
* @throws SecurityException
9292
*/
@@ -113,9 +113,9 @@ protected function checkEncoding($value)
113113
/**
114114
* Check for the presence of control characters except line breaks and tabs.
115115
*
116-
* @param array|string $value
116+
* @param array<array-key, mixed>|string $value
117117
*
118-
* @return array|string
118+
* @return array<array-key, mixed>|string
119119
*/
120120
protected function checkControl($value)
121121
{

system/Filters/PageCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(?Cache $config = null)
4545
/**
4646
* Checks page cache and return if found.
4747
*
48-
* @param array|null $arguments
48+
* @param list<string>|null $arguments
4949
*
5050
* @return ResponseInterface|null
5151
*/
@@ -61,7 +61,7 @@ public function before(RequestInterface $request, $arguments = null)
6161
/**
6262
* Cache the page.
6363
*
64-
* @param array|null $arguments
64+
* @param list<string>|null $arguments
6565
*/
6666
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
6767
{

system/Filters/PerformanceMetrics.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class PerformanceMetrics implements FilterInterface
2424
/**
2525
* We don't need to do anything here.
2626
*
27-
* @param array|null $arguments
27+
* @param list<string>|null $arguments
2828
*/
2929
public function before(RequestInterface $request, $arguments = null)
3030
{
@@ -34,7 +34,7 @@ public function before(RequestInterface $request, $arguments = null)
3434
/**
3535
* Replaces the performance metrics.
3636
*
37-
* @param array|null $arguments
37+
* @param list<string>|null $arguments
3838
*/
3939
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
4040
{

tests/system/Filters/CSRFTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ public function testDoNotCheckCliRequest(): void
5353
'after' => [],
5454
];
5555

56-
$this->request = Services::clirequest(null, false);
57-
$this->response = service('response');
56+
$this->request = Services::clirequest(null, false);
57+
58+
$response = service('response');
59+
$this->assertInstanceOf(Response::class, $response);
60+
$this->response = $response;
5861

5962
$filters = new Filters($this->config, $this->request, $this->response);
6063
$uri = 'admin/foo/bar';
@@ -71,8 +74,11 @@ public function testPassGetRequest(): void
7174
'after' => [],
7275
];
7376

74-
$this->request = service('incomingrequest', null, false);
75-
$this->response = service('response');
77+
$this->request = service('incomingrequest', null, false);
78+
79+
$response = service('response');
80+
$this->assertInstanceOf(Response::class, $response);
81+
$this->response = $response;
7682

7783
$filters = new Filters($this->config, $this->request, $this->response);
7884
$uri = 'admin/foo/bar';

tests/system/Filters/DebugToolbarTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ protected function setUp(): void
4545

4646
Services::injectMock('superglobals', new Superglobals());
4747

48-
$this->request = service('request');
49-
$this->response = service('response');
48+
$this->request = service('request');
49+
50+
$response = service('response');
51+
$this->assertInstanceOf(Response::class, $response);
52+
$this->response = $response;
5053
}
5154

5255
public function testDebugToolbarFilter(): void

tests/system/Filters/FiltersTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use CodeIgniter\Filters\fixtures\Multiple2;
2525
use CodeIgniter\Filters\fixtures\Role;
2626
use CodeIgniter\HTTP\CLIRequest;
27+
use CodeIgniter\HTTP\RequestInterface;
2728
use CodeIgniter\HTTP\Response;
2829
use CodeIgniter\HTTP\ResponseInterface;
2930
use CodeIgniter\Superglobals;
@@ -70,10 +71,12 @@ protected function setUp(): void
7071

7172
Services::injectMock('superglobals', new Superglobals());
7273

73-
$this->response = service('response');
74+
$response = service('response');
75+
$this->assertInstanceOf(Response::class, $response);
76+
$this->response = $response;
7477
}
7578

76-
private function createFilters(FiltersConfig $config, $request = null): Filters
79+
private function createFilters(FiltersConfig $config, ?RequestInterface $request = null): Filters
7780
{
7881
$request ??= service('request');
7982

@@ -213,10 +216,10 @@ public function testProcessMethodProcessGlobals(): void
213216
}
214217

215218
/**
216-
* @param array|string $except
219+
* @param list<string>|string $except
217220
*/
218221
#[DataProvider('provideProcessMethodProcessGlobalsWithExcept')]
219-
public function testProcessMethodProcessGlobalsWithExcept($except): void
222+
public function testProcessMethodProcessGlobalsWithExcept(array|string $except): void
220223
{
221224
service('superglobals')->setServer('REQUEST_METHOD', 'GET');
222225

@@ -249,6 +252,9 @@ public function testProcessMethodProcessGlobalsWithExcept($except): void
249252
$this->assertSame($expected, $filters->initialize($uri)->getFilters());
250253
}
251254

255+
/**
256+
* @return iterable<array{list<string>|string}>
257+
*/
252258
public static function provideProcessMethodProcessGlobalsWithExcept(): iterable
253259
{
254260
return [
@@ -589,11 +595,13 @@ public function testOtherResult(): void
589595
$this->assertSame('This is curious', $response);
590596
}
591597

598+
/** @param array|string $except */
592599
/**
593-
* @param array|string $except
600+
* @param list<string>|string $except
601+
* @param array{before: list<string>, after: list<string>} $expected
594602
*/
595603
#[DataProvider('provideBeforeExcept')]
596-
public function testBeforeExcept(string $uri, $except, array $expected): void
604+
public function testBeforeExcept(string $uri, array|string $except, array $expected): void
597605
{
598606
service('superglobals')->setServer('REQUEST_METHOD', 'GET');
599607

@@ -619,6 +627,9 @@ public function testBeforeExcept(string $uri, $except, array $expected): void
619627
$this->assertSame($expected, $filters->initialize($uri)->getFilters());
620628
}
621629

630+
/**
631+
* @return iterable<string, array{string, list<string>|string, array{before: list<string>, after: list<string>}}>
632+
*/
622633
public static function provideBeforeExcept(): iterable
623634
{
624635
return [

tests/system/Filters/HoneypotTest.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ public function testBeforeTriggered(): void
6565
'after' => [],
6666
];
6767

68-
$this->request = service('request', null, false);
69-
$this->response = service('response');
68+
$this->request = service('request', null, false);
69+
70+
$response = service('response');
71+
$this->assertInstanceOf(Response::class, $response);
72+
$this->response = $response;
7073

7174
$filters = new Filters($this->config, $this->request, $this->response);
7275
$uri = 'admin/foo/bar';
@@ -83,8 +86,11 @@ public function testBeforeClean(): void
8386
];
8487

8588
service('superglobals')->unsetPost($this->honey->name);
86-
$this->request = service('request', null, false);
87-
$this->response = service('response');
89+
$this->request = service('request', null, false);
90+
91+
$response = service('response');
92+
$this->assertInstanceOf(Response::class, $response);
93+
$this->response = $response;
8894

8995
$expected = $this->request;
9096

@@ -104,14 +110,19 @@ public function testAfter(): void
104110
'after' => ['honeypot'],
105111
];
106112

107-
$this->request = service('request', null, false);
108-
$this->response = service('response');
113+
$this->request = service('request', null, false);
114+
115+
$response = service('response');
116+
$this->assertInstanceOf(Response::class, $response);
117+
$this->response = $response;
109118

110119
$filters = new Filters($this->config, $this->request, $this->response);
111120
$uri = 'admin/foo/bar';
112121

113122
$this->response->setBody('<form></form>');
114-
$this->response = $filters->run($uri, 'after');
123+
$response = $filters->run($uri, 'after');
124+
$this->assertInstanceOf(Response::class, $response);
125+
$this->response = $response;
115126
$this->assertStringContainsString($this->honey->name, (string) $this->response->getBody());
116127
}
117128

@@ -124,14 +135,19 @@ public function testAfterNotApplicable(): void
124135
'after' => ['honeypot'],
125136
];
126137

127-
$this->request = service('request', null, false);
128-
$this->response = service('response');
138+
$this->request = service('request', null, false);
139+
140+
$response = service('response');
141+
$this->assertInstanceOf(Response::class, $response);
142+
$this->response = $response;
129143

130144
$filters = new Filters($this->config, $this->request, $this->response);
131145
$uri = 'admin/foo/bar';
132146

133147
$this->response->setBody('<div></div>');
134-
$this->response = $filters->run($uri, 'after');
148+
$response = $filters->run($uri, 'after');
149+
$this->assertInstanceOf(Response::class, $response);
150+
$this->response = $response;
135151
$this->assertStringNotContainsString($this->honey->name, (string) $this->response->getBody());
136152
}
137153
}

tests/system/Filters/InvalidCharsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ public function testCheckControlStringWithLineBreakAndTabReturnsTheString(string
161161
$this->invalidChars->before($this->request);
162162
}
163163

164+
/**
165+
* @return iterable<array{string}>
166+
*/
164167
public static function provideCheckControlStringWithLineBreakAndTabReturnsTheString(): iterable
165168
{
166169
yield from [
@@ -183,6 +186,9 @@ public function testCheckControlStringWithControlCharsCausesException(string $in
183186
$this->invalidChars->before($this->request);
184187
}
185188

189+
/**
190+
* @return iterable<array{string}>
191+
*/
186192
public static function provideCheckControlStringWithControlCharsCausesException(): iterable
187193
{
188194
yield from [

0 commit comments

Comments
 (0)