Skip to content

Commit 30625ce

Browse files
committed
refactor: fix phpstan errors in IncomingRequestTest
1 parent c42ffa8 commit 30625ce

6 files changed

Lines changed: 56 additions & 172 deletions

File tree

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use PHPUnit\Framework\Attributes\PreserveGlobalState;
3030
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
3131
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
32-
use TypeError;
3332

3433
/**
3534
* @internal
@@ -38,7 +37,7 @@
3837
#[Group('SeparateProcess')]
3938
final class IncomingRequestTest extends CIUnitTestCase
4039
{
41-
private Request $request;
40+
private IncomingRequest $request;
4241

4342
#[WithoutErrorHandler]
4443
protected function setUp(): void
@@ -59,7 +58,7 @@ protected function setUp(): void
5958
$this->request = $this->createRequest($config);
6059
}
6160

62-
private function createRequest(?App $config = null, $body = null, ?string $path = null): IncomingRequest
61+
private function createRequest(?App $config = null, false|string|null $body = null, ?string $path = null): IncomingRequest
6362
{
6463
$config ??= new App();
6564
$path ??= '';
@@ -635,6 +634,9 @@ public function testCanGrabGetRawInputVar($rawstring, $var, $expected, $filter,
635634
$this->assertSame($expected, $request->getRawInputVar($var, $filter, $flag));
636635
}
637636

637+
/**
638+
* @return iterable<array{string, mixed, mixed, mixed, mixed}>
639+
*/
638640
public static function provideCanGrabGetRawInputVar(): iterable
639641
{
640642
return [
@@ -729,6 +731,9 @@ public function testIsHTTPMethodLowerCase(string $value): void
729731
$this->assertTrue($request->is(strtolower($value)));
730732
}
731733

734+
/**
735+
* @return iterable<array{string}>
736+
*/
732737
public static function provideIsHTTPMethods(): iterable
733738
{
734739
yield from [
@@ -756,21 +761,22 @@ public function testIsInvalidValue(): void
756761
$this->expectExceptionMessage('Unknown type: invalid');
757762

758763
$request = $this->request->withMethod('GET');
759-
760764
$request->is('invalid');
761765
}
762766

763767
public function testIsJson(): void
764768
{
765769
$request = $this->request->setHeader('Content-Type', 'application/json');
766770

771+
$this->assertInstanceOf(IncomingRequest::class, $request);
767772
$this->assertTrue($request->is('json'));
768773
}
769774

770775
public function testIsWithAjax(): void
771776
{
772777
$request = $this->request->setHeader('X-Requested-With', 'XMLHttpRequest');
773778

779+
$this->assertInstanceOf(IncomingRequest::class, $request);
774780
$this->assertTrue($request->is('ajax'));
775781
}
776782

@@ -1047,12 +1053,12 @@ public function testGetIPAddressNormal(): void
10471053
$expected = '123.123.123.123';
10481054
service('superglobals')->setServer('REMOTE_ADDR', $expected);
10491055

1050-
$this->request = new Request(new App());
1051-
$this->request->populateHeaders();
1056+
$request = new Request(new App());
1057+
$request->populateHeaders();
10521058

1053-
$this->assertSame($expected, $this->request->getIPAddress());
1059+
$this->assertSame($expected, $request->getIPAddress());
10541060
// call a second time to exercise the initial conditional block in getIPAddress()
1055-
$this->assertSame($expected, $this->request->getIPAddress());
1061+
$this->assertSame($expected, $request->getIPAddress());
10561062
}
10571063

10581064
public function testGetIPAddressThruProxy(): void
@@ -1068,11 +1074,11 @@ public function testGetIPAddressThruProxy(): void
10681074
'192.168.5.0/24' => 'X-Forwarded-For',
10691075
];
10701076
Factories::injectMock('config', App::class, $config);
1071-
$this->request = new Request();
1072-
$this->request->populateHeaders();
1077+
$request = new Request();
1078+
$request->populateHeaders();
10731079

10741080
// we should see the original forwarded address
1075-
$this->assertSame($expected, $this->request->getIPAddress());
1081+
$this->assertSame($expected, $request->getIPAddress());
10761082
}
10771083

10781084
public function testGetIPAddressThruProxyIPv6(): void
@@ -1087,11 +1093,11 @@ public function testGetIPAddressThruProxyIPv6(): void
10871093
'2001:db8::2:1' => 'X-Forwarded-For',
10881094
];
10891095
Factories::injectMock('config', App::class, $config);
1090-
$this->request = new Request();
1091-
$this->request->populateHeaders();
1096+
$request = new Request();
1097+
$request->populateHeaders();
10921098

10931099
// we should see the original forwarded address
1094-
$this->assertSame($expected, $this->request->getIPAddress());
1100+
$this->assertSame($expected, $request->getIPAddress());
10951101
}
10961102

10971103
public function testGetIPAddressThruProxyInvalidIPAddress(): void
@@ -1105,11 +1111,11 @@ public function testGetIPAddressThruProxyInvalidIPAddress(): void
11051111
'10.0.1.200' => 'X-Forwarded-For',
11061112
'192.168.5.0/24' => 'X-Forwarded-For',
11071113
];
1108-
$this->request = new Request($config);
1109-
$this->request->populateHeaders();
1114+
$request = new Request($config);
1115+
$request->populateHeaders();
11101116

11111117
// spoofed address invalid
1112-
$this->assertSame($expected, $this->request->getIPAddress());
1118+
$this->assertSame($expected, $request->getIPAddress());
11131119
}
11141120

11151121
public function testGetIPAddressThruProxyInvalidIPAddressIPv6(): void
@@ -1122,11 +1128,11 @@ public function testGetIPAddressThruProxyInvalidIPAddressIPv6(): void
11221128
$config->proxyIPs = [
11231129
'2001:db8::2:1' => 'X-Forwarded-For',
11241130
];
1125-
$this->request = new Request($config);
1126-
$this->request->populateHeaders();
1131+
$request = new Request($config);
1132+
$request->populateHeaders();
11271133

11281134
// spoofed address invalid
1129-
$this->assertSame($expected, $this->request->getIPAddress());
1135+
$this->assertSame($expected, $request->getIPAddress());
11301136
}
11311137

11321138
public function testGetIPAddressThruProxyNotWhitelisted(): void
@@ -1140,11 +1146,11 @@ public function testGetIPAddressThruProxyNotWhitelisted(): void
11401146
'10.0.1.200' => 'X-Forwarded-For',
11411147
'192.168.5.0/24' => 'X-Forwarded-For',
11421148
];
1143-
$this->request = new Request($config);
1144-
$this->request->populateHeaders();
1149+
$request = new Request($config);
1150+
$request->populateHeaders();
11451151

11461152
// spoofed address invalid
1147-
$this->assertSame($expected, $this->request->getIPAddress());
1153+
$this->assertSame($expected, $request->getIPAddress());
11481154
}
11491155

11501156
public function testGetIPAddressThruProxyNotWhitelistedIPv6(): void
@@ -1157,11 +1163,11 @@ public function testGetIPAddressThruProxyNotWhitelistedIPv6(): void
11571163
$config->proxyIPs = [
11581164
'2001:db8::2:1' => 'X-Forwarded-For',
11591165
];
1160-
$this->request = new Request($config);
1161-
$this->request->populateHeaders();
1166+
$request = new Request($config);
1167+
$request->populateHeaders();
11621168

11631169
// spoofed address invalid
1164-
$this->assertSame($expected, $this->request->getIPAddress());
1170+
$this->assertSame($expected, $request->getIPAddress());
11651171
}
11661172

11671173
public function testGetIPAddressThruProxySubnet(): void
@@ -1173,11 +1179,11 @@ public function testGetIPAddressThruProxySubnet(): void
11731179
$config = new App();
11741180
$config->proxyIPs = ['192.168.5.0/24' => 'X-Forwarded-For'];
11751181
Factories::injectMock('config', App::class, $config);
1176-
$this->request = new Request();
1177-
$this->request->populateHeaders();
1182+
$request = new Request();
1183+
$request->populateHeaders();
11781184

11791185
// we should see the original forwarded address
1180-
$this->assertSame($expected, $this->request->getIPAddress());
1186+
$this->assertSame($expected, $request->getIPAddress());
11811187
}
11821188

11831189
public function testGetIPAddressThruProxySubnetIPv6(): void
@@ -1189,11 +1195,11 @@ public function testGetIPAddressThruProxySubnetIPv6(): void
11891195
$config = new App();
11901196
$config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For'];
11911197
Factories::injectMock('config', App::class, $config);
1192-
$this->request = new Request();
1193-
$this->request->populateHeaders();
1198+
$request = new Request();
1199+
$request->populateHeaders();
11941200

11951201
// we should see the original forwarded address
1196-
$this->assertSame($expected, $this->request->getIPAddress());
1202+
$this->assertSame($expected, $request->getIPAddress());
11971203
}
11981204

11991205
public function testGetIPAddressThruProxyOutOfSubnet(): void
@@ -1204,11 +1210,11 @@ public function testGetIPAddressThruProxyOutOfSubnet(): void
12041210

12051211
$config = new App();
12061212
$config->proxyIPs = ['192.168.5.0/28' => 'X-Forwarded-For'];
1207-
$this->request = new Request($config);
1208-
$this->request->populateHeaders();
1213+
$request = new Request($config);
1214+
$request->populateHeaders();
12091215

12101216
// we should see the original forwarded address
1211-
$this->assertSame($expected, $this->request->getIPAddress());
1217+
$this->assertSame($expected, $request->getIPAddress());
12121218
}
12131219

12141220
public function testGetIPAddressThruProxyOutOfSubnetIPv6(): void
@@ -1219,11 +1225,11 @@ public function testGetIPAddressThruProxyOutOfSubnetIPv6(): void
12191225

12201226
$config = new App();
12211227
$config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For'];
1222-
$this->request = new Request($config);
1223-
$this->request->populateHeaders();
1228+
$request = new Request($config);
1229+
$request->populateHeaders();
12241230

12251231
// we should see the original forwarded address
1226-
$this->assertSame($expected, $this->request->getIPAddress());
1232+
$this->assertSame($expected, $request->getIPAddress());
12271233
}
12281234

12291235
public function testGetIPAddressThruProxyBothIPv4AndIPv6(): void
@@ -1237,23 +1243,11 @@ public function testGetIPAddressThruProxyBothIPv4AndIPv6(): void
12371243
'192.168.5.0/28' => 'X-Forwarded-For',
12381244
'2001:db8:1234::/48' => 'X-Forwarded-For',
12391245
];
1240-
$this->request = new Request($config);
1241-
$this->request->populateHeaders();
1246+
$request = new Request($config);
1247+
$request->populateHeaders();
12421248

12431249
// we should see the original forwarded address
1244-
$this->assertSame($expected, $this->request->getIPAddress());
1245-
}
1246-
1247-
public function testGetIPAddressThruProxyInvalidConfigString(): void
1248-
{
1249-
$this->expectException(TypeError::class);
1250-
1251-
$config = new App();
1252-
$config->proxyIPs = '192.168.5.0/28';
1253-
$this->request = new Request($config);
1254-
$this->request->populateHeaders();
1255-
1256-
$this->request->getIPAddress();
1250+
$this->assertSame($expected, $request->getIPAddress());
12571251
}
12581252

12591253
public function testGetIPAddressThruProxyInvalidConfigArray(): void
@@ -1264,12 +1258,12 @@ public function testGetIPAddressThruProxyInvalidConfigArray(): void
12641258
);
12651259

12661260
$config = new App();
1267-
$config->proxyIPs = ['192.168.5.0/28'];
1261+
$config->proxyIPs = ['192.168.5.0/28']; // @phpstan-ignore assign.propertyType (deliberately keyless, to assert the ConfigException)
12681262
Factories::injectMock('config', App::class, $config);
1269-
$this->request = new Request();
1270-
$this->request->populateHeaders();
1263+
$request = new Request();
1264+
$request->populateHeaders();
12711265

1272-
$this->request->getIPAddress();
1266+
$request->getIPAddress();
12731267
}
12741268

12751269
// @TODO getIPAddress should have more testing, to 100% code coverage

‎utils/phpstan-baseline/assign.propertyType.neon‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 28 errors
1+
# total 26 errors
22

33
parameters:
44
ignoreErrors:
@@ -57,16 +57,6 @@ parameters:
5757
count: 4
5858
path: ../../tests/system/Filters/HoneypotTest.php
5959

60-
-
61-
message: '#^Property Config\\App\:\:\$proxyIPs \(array\<string, string\>\) does not accept array\<int, string\>\.$#'
62-
count: 1
63-
path: ../../tests/system/HTTP/IncomingRequestTest.php
64-
65-
-
66-
message: '#^Property Config\\App\:\:\$proxyIPs \(array\<string, string\>\) does not accept string\.$#'
67-
count: 1
68-
path: ../../tests/system/HTTP/IncomingRequestTest.php
69-
7060
-
7161
message: '#^Property CodeIgniter\\Helpers\\CookieHelperTest\:\:\$response \(CodeIgniter\\HTTP\\Response\) does not accept CodeIgniter\\HTTP\\ResponseInterface\.$#'
7262
count: 1

‎utils/phpstan-baseline/loader.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 1527 errors
1+
# total 1475 errors
22

33
includes:
44
- argument.type.neon

0 commit comments

Comments
 (0)