Skip to content
Open
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
18 changes: 11 additions & 7 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ public function runRequired(string $position = 'before')
*
* @param 'after'|'before' $position
*
* @return array{list<string>, array<string, class-string|list<class-string>>}
*
* @internal
*/
public function getRequiredFilters(string $position = 'before'): array
Expand Down Expand Up @@ -410,7 +412,9 @@ public function getRequiredFilters(string $position = 'before'): array
* Set the toolbar filter to the last position to be executed.
*
* @param list<string> $filters `after` filter array
* @param bool $remove if true, remove `toolbar` filter
* @param bool $remove If true, remove `toolbar` filter
*
* @return list<string>
*/
private function setToolbarToLast(array $filters, bool $remove = false): array
{
Expand Down Expand Up @@ -853,8 +857,8 @@ protected function processAliasesToClass(string $position)
/**
* Check paths for match for URI
*
* @param string $uri URI to test against
* @param array|string $paths The path patterns to test
* @param string $uri URI to test against
* @param array<array-key, string>|string $paths The path patterns to test
*
* @return bool True if any of the paths apply to the URI
*/
Expand All @@ -876,8 +880,8 @@ private function pathApplies(string $uri, $paths)
/**
* Check except paths
*
* @param string $uri URI path relative to baseURL (all lowercase)
* @param array|string $paths The except path patterns
* @param string $uri URI path relative to baseURL (all lowercase)
* @param array<array-key, string>|string $paths The except path patterns
*
* @return bool True if the URI matches except paths.
*/
Expand All @@ -899,8 +903,8 @@ private function checkExcept(string $uri, $paths): bool
/**
* Check the URI path as pseudo-regex
*
* @param string $uri URI path relative to baseURL (all lowercase, URL-decoded)
* @param array $paths The except path patterns
* @param string $uri URI path relative to baseURL (all lowercase, URL-decoded)
* @param array<array-key, string> $paths The except path patterns
*/
private function checkPseudoRegex(string $uri, array $paths): bool
{
Expand Down
4 changes: 2 additions & 2 deletions system/Filters/ForceHTTPS.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ForceHTTPS implements FilterInterface
* as set the HTTP Strict Transport Security (HSTS) header for those browsers
* that support it.
*
* @param array|null $arguments
* @param list<string>|null $arguments
*
* @return ResponseInterface|null
*/
Expand All @@ -56,7 +56,7 @@ public function before(RequestInterface $request, $arguments = null)
/**
* We don't have anything to do here.
*
* @param array|null $arguments
* @param list<string>|null $arguments
*/
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
Expand Down
8 changes: 4 additions & 4 deletions system/Filters/InvalidChars.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
/**
* Check the character encoding is valid UTF-8.
*
* @param array|string $value
* @param array<array-key, mixed>|string $value
*
* @return array|string
* @return array<array-key, mixed>|string
*
* @throws SecurityException
*/
Expand All @@ -113,9 +113,9 @@ protected function checkEncoding($value)
/**
* Check for the presence of control characters except line breaks and tabs.
*
* @param array|string $value
* @param array<array-key, mixed>|string $value
*
* @return array|string
* @return array<array-key, mixed>|string
*/
protected function checkControl($value)
{
Expand Down
4 changes: 2 additions & 2 deletions system/Filters/PageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(?Cache $config = null)
/**
* Checks page cache and return if found.
*
* @param array|null $arguments
* @param list<string>|null $arguments
*
* @return ResponseInterface|null
*/
Expand All @@ -61,7 +61,7 @@ public function before(RequestInterface $request, $arguments = null)
/**
* Cache the page.
*
* @param array|null $arguments
* @param list<string>|null $arguments
*/
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
Expand Down
4 changes: 2 additions & 2 deletions system/Filters/PerformanceMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PerformanceMetrics implements FilterInterface
/**
* We don't need to do anything here.
*
* @param array|null $arguments
* @param list<string>|null $arguments
*/
public function before(RequestInterface $request, $arguments = null)
{
Expand All @@ -34,7 +34,7 @@ public function before(RequestInterface $request, $arguments = null)
/**
* Replaces the performance metrics.
*
* @param array|null $arguments
* @param list<string>|null $arguments
*/
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
Expand Down
14 changes: 10 additions & 4 deletions tests/system/Filters/CSRFTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ public function testDoNotCheckCliRequest(): void
'after' => [],
];

$this->request = Services::clirequest(null, false);
$this->response = service('response');
$this->request = Services::clirequest(null, false);

$response = service('response');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;

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

$this->request = service('incomingrequest', null, false);
$this->response = service('response');
$this->request = service('incomingrequest', null, false);

$response = service('response');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';
Expand Down
7 changes: 5 additions & 2 deletions tests/system/Filters/DebugToolbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ protected function setUp(): void

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

$this->request = service('request');
$this->response = service('response');
$this->request = service('request');

$response = service('response');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;
}

public function testDebugToolbarFilter(): void
Expand Down
22 changes: 16 additions & 6 deletions tests/system/Filters/FiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use CodeIgniter\Filters\fixtures\Multiple2;
use CodeIgniter\Filters\fixtures\Role;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Superglobals;
Expand Down Expand Up @@ -70,10 +71,12 @@ protected function setUp(): void

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

$this->response = service('response');
$response = service('response');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;
}

private function createFilters(FiltersConfig $config, $request = null): Filters
private function createFilters(FiltersConfig $config, ?RequestInterface $request = null): Filters
{
$request ??= service('request');

Expand Down Expand Up @@ -213,10 +216,10 @@ public function testProcessMethodProcessGlobals(): void
}

/**
* @param array|string $except
* @param list<string>|string $except
*/
#[DataProvider('provideProcessMethodProcessGlobalsWithExcept')]
public function testProcessMethodProcessGlobalsWithExcept($except): void
public function testProcessMethodProcessGlobalsWithExcept(array|string $except): void
{
service('superglobals')->setServer('REQUEST_METHOD', 'GET');

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

/**
* @return iterable<array{list<string>|string}>
*/
public static function provideProcessMethodProcessGlobalsWithExcept(): iterable
{
return [
Expand Down Expand Up @@ -590,10 +596,11 @@ public function testOtherResult(): void
}

/**
* @param array|string $except
* @param list<string>|string $except
* @param array{before: list<string>, after: list<string>} $expected
*/
#[DataProvider('provideBeforeExcept')]
public function testBeforeExcept(string $uri, $except, array $expected): void
public function testBeforeExcept(string $uri, array|string $except, array $expected): void
{
service('superglobals')->setServer('REQUEST_METHOD', 'GET');

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

/**
* @return iterable<string, array{string, list<string>|string, array{before: list<string>, after: list<string>}}>
*/
public static function provideBeforeExcept(): iterable
{
return [
Expand Down
36 changes: 26 additions & 10 deletions tests/system/Filters/HoneypotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ public function testBeforeTriggered(): void
'after' => [],
];

$this->request = service('request', null, false);
$this->response = service('response');
$this->request = service('request', null, false);

$response = service('response');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;

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

service('superglobals')->unsetPost($this->honey->name);
$this->request = service('request', null, false);
$this->response = service('response');
$this->request = service('request', null, false);

$response = service('response');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;

$expected = $this->request;

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

$this->request = service('request', null, false);
$this->response = service('response');
$this->request = service('request', null, false);

$response = service('response');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;

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

$this->response->setBody('<form></form>');
$this->response = $filters->run($uri, 'after');
$response = $filters->run($uri, 'after');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;
$this->assertStringContainsString($this->honey->name, (string) $this->response->getBody());
}

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

$this->request = service('request', null, false);
$this->response = service('response');
$this->request = service('request', null, false);

$response = service('response');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;

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

$this->response->setBody('<div></div>');
$this->response = $filters->run($uri, 'after');
$response = $filters->run($uri, 'after');
$this->assertInstanceOf(Response::class, $response);
$this->response = $response;
$this->assertStringNotContainsString($this->honey->name, (string) $this->response->getBody());
}
}
6 changes: 6 additions & 0 deletions tests/system/Filters/InvalidCharsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public function testCheckControlStringWithLineBreakAndTabReturnsTheString(string
$this->invalidChars->before($this->request);
}

/**
* @return iterable<array{string}>
*/
public static function provideCheckControlStringWithLineBreakAndTabReturnsTheString(): iterable
{
yield from [
Expand All @@ -183,6 +186,9 @@ public function testCheckControlStringWithControlCharsCausesException(string $in
$this->invalidChars->before($this->request);
}

/**
* @return iterable<array{string}>
*/
public static function provideCheckControlStringWithControlCharsCausesException(): iterable
{
yield from [
Expand Down
27 changes: 1 addition & 26 deletions utils/phpstan-baseline/assign.propertyType.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 20 errors
# total 10 errors

parameters:
ignoreErrors:
Expand All @@ -17,31 +17,6 @@ parameters:
count: 1
path: ../../tests/system/Commands/Utilities/Routes/FilterFinderTest.php

-
message: '#^Property CodeIgniter\\Filters\\CSRFTest\:\:\$response \(CodeIgniter\\HTTP\\Response\|null\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#'
count: 2
path: ../../tests/system/Filters/CSRFTest.php

-
message: '#^Property CodeIgniter\\Filters\\DebugToolbarTest\:\:\$response \(CodeIgniter\\HTTP\\Response\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#'
count: 1
path: ../../tests/system/Filters/DebugToolbarTest.php

-
message: '#^Property CodeIgniter\\Filters\\FiltersTest\:\:\$response \(CodeIgniter\\HTTP\\Response\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#'
count: 1
path: ../../tests/system/Filters/FiltersTest.php

-
message: '#^Property CodeIgniter\\Filters\\HoneypotTest\:\:\$response \(CodeIgniter\\HTTP\\Response\|null\) does not accept CodeIgniter\\HTTP\\RequestInterface\|CodeIgniter\\HTTP\\ResponseInterface\|string\|null\.$#'
count: 2
path: ../../tests/system/Filters/HoneypotTest.php

-
message: '#^Property CodeIgniter\\Filters\\HoneypotTest\:\:\$response \(CodeIgniter\\HTTP\\Response\|null\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#'
count: 4
path: ../../tests/system/Filters/HoneypotTest.php

-
message: '#^Property CodeIgniter\\Helpers\\CookieHelperTest\:\:\$response \(CodeIgniter\\HTTP\\Response\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#'
count: 1
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 419 errors
# total 386 errors

includes:
- argument.type.neon
Expand Down
Loading
Loading