Skip to content

Commit 3068772

Browse files
committed
Add ALL the types
1 parent c89b604 commit 3068772

File tree

5 files changed

+114
-66
lines changed

5 files changed

+114
-66
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"scripts": {
3434
"test": "phpunit",
35-
"analyse": "phpstan analyse src tests --level=5",
35+
"analyse": "phpstan analyse src tests --level=6",
3636
"check-style": "phpcs -p --standard=PSR12 --exclude=Generic.Files.LineLength --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests",
3737
"fix-style": "phpcbf -p --standard=PSR12 --exclude=Generic.Files.LineLength --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests"
3838
},

src/CorsService.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,51 @@
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
1818

19+
/**
20+
* @phpstan-type CorsInputOptions array{
21+
* 'allowedOrigins'?: array{string}|array{},
22+
* 'allowedOriginsPatterns'?: array{string}|array{},
23+
* 'supportsCredentials'?: bool,
24+
* 'allowedHeaders'?: array{string}|array{},
25+
* 'allowedMethods'?: array{string}|array{},
26+
* 'exposedHeaders'?: array{string}|array{},
27+
* 'maxAge'?: int|bool|null,
28+
* 'allowed_origins'?: array{string}|array{},
29+
* 'allowed_origins_patterns'?: array{string}|array{},
30+
* 'supports_credentials'?: bool,
31+
* 'allowed_headers'?: array{string}|array{},
32+
* 'allowed_methods'?: array{string}|array{},
33+
* 'exposed_headers'?: array{string}|array{},
34+
* 'max_age'?: int|bool|null
35+
* }
36+
*
37+
* @phpstan-type CorsNormalizedOptions array{
38+
* 'allowedOrigins': array{string}|array{}|true,
39+
* 'allowedOriginsPatterns': array{string}|array{},
40+
* 'supportsCredentials': bool,
41+
* 'allowedHeaders': array{string}|array{}|bool,
42+
* 'allowedMethods': array{string}|array{}|bool,
43+
* 'exposedHeaders': array{string}|array{},
44+
* 'maxAge': int|bool|null
45+
* }
46+
*/
1947
class CorsService
2048
{
49+
/** @var CorsNormalizedOptions */
2150
private $options;
2251

52+
/**
53+
* @param CorsInputOptions $options
54+
*/
2355
public function __construct(array $options = [])
2456
{
2557
$this->options = $this->normalizeOptions($options);
2658
}
2759

60+
/**
61+
* @param CorsInputOptions $options
62+
* @return CorsNormalizedOptions
63+
*/
2864
private function normalizeOptions(array $options = []): array
2965
{
3066
$aliases = [
@@ -191,7 +227,7 @@ public function addActualRequestHeaders(Response $response, Request $request): R
191227
return $response;
192228
}
193229

194-
private function configureAllowedOrigin(Response $response, Request $request)
230+
private function configureAllowedOrigin(Response $response, Request $request): void
195231
{
196232
if ($this->options['allowedOrigins'] === true && !$this->options['supportsCredentials']) {
197233
// Safe+cacheable, allow everything
@@ -211,14 +247,14 @@ private function configureAllowedOrigin(Response $response, Request $request)
211247

212248
private function isSingleOriginAllowed(): bool
213249
{
214-
if ($this->options['allowedOrigins'] === true || !empty($this->options['allowedOriginsPatterns'])) {
250+
if ($this->options['allowedOrigins'] === true || count($this->options['allowedOriginsPatterns']) > 0) {
215251
return false;
216252
}
217253

218254
return count($this->options['allowedOrigins']) === 1;
219255
}
220256

221-
private function configureAllowedMethods(Response $response, Request $request)
257+
private function configureAllowedMethods(Response $response, Request $request): void
222258
{
223259
if ($this->options['allowedMethods'] === true) {
224260
$allowMethods = strtoupper($request->headers->get('Access-Control-Request-Method'));
@@ -230,7 +266,7 @@ private function configureAllowedMethods(Response $response, Request $request)
230266
$response->headers->set('Access-Control-Allow-Methods', $allowMethods);
231267
}
232268

233-
private function configureAllowedHeaders(Response $response, Request $request)
269+
private function configureAllowedHeaders(Response $response, Request $request): void
234270
{
235271
if ($this->options['allowedHeaders'] === true) {
236272
$allowHeaders = $request->headers->get('Access-Control-Request-Headers');
@@ -241,28 +277,28 @@ private function configureAllowedHeaders(Response $response, Request $request)
241277
$response->headers->set('Access-Control-Allow-Headers', $allowHeaders);
242278
}
243279

244-
private function configureAllowCredentials(Response $response, Request $request)
280+
private function configureAllowCredentials(Response $response, Request $request): void
245281
{
246282
if ($this->options['supportsCredentials']) {
247283
$response->headers->set('Access-Control-Allow-Credentials', 'true');
248284
}
249285
}
250286

251-
private function configureExposedHeaders(Response $response, Request $request)
287+
private function configureExposedHeaders(Response $response, Request $request): void
252288
{
253289
if ($this->options['exposedHeaders']) {
254290
$response->headers->set('Access-Control-Expose-Headers', implode(', ', $this->options['exposedHeaders']));
255291
}
256292
}
257293

258-
private function configureMaxAge(Response $response, Request $request)
294+
private function configureMaxAge(Response $response, Request $request): void
259295
{
260296
if ($this->options['maxAge'] !== null) {
261297
$response->headers->set('Access-Control-Max-Age', (string) $this->options['maxAge']);
262298
}
263299
}
264300

265-
public function varyHeader(Response $response, $header): Response
301+
public function varyHeader(Response $response, string $header): Response
266302
{
267303
if (!$response->headers->has('Vary')) {
268304
$response->headers->set('Vary', $header);

tests/CorsServiceTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CorsServiceTest extends TestCase
2121
/**
2222
* @test
2323
*/
24-
public function itCanHaveOptions()
24+
public function itCanHaveOptions(): void
2525
{
2626
$service = new CorsService([
2727
'allowedOrigins' => ['localhost']
@@ -34,7 +34,7 @@ public function itCanHaveOptions()
3434
/**
3535
* @test
3636
*/
37-
public function itCanHaveNoOptions()
37+
public function itCanHaveNoOptions(): void
3838
{
3939
$service = new CorsService();
4040
$this->assertInstanceOf(CorsService::class, $service);
@@ -44,7 +44,7 @@ public function itCanHaveNoOptions()
4444
/**
4545
* @test
4646
*/
47-
public function itCanHaveEmptyOptions()
47+
public function itCanHaveEmptyOptions(): void
4848
{
4949
$service = new CorsService([]);
5050
$this->assertInstanceOf(CorsService::class, $service);
@@ -54,27 +54,29 @@ public function itCanHaveEmptyOptions()
5454
/**
5555
* @test
5656
*/
57-
public function itThrowsExceptionOnInvalidExposedHeaders()
57+
public function itThrowsExceptionOnInvalidExposedHeaders(): void
5858
{
5959
$this->expectException(InvalidOptionException::class);
6060

61+
/** @phpstan-ignore-next-line */
6162
$service = new CorsService(['exposedHeaders' => true]);
6263
}
6364

6465
/**
6566
* @test
6667
*/
67-
public function itThrowsExceptionOnInvalidOriginsArray()
68+
public function itThrowsExceptionOnInvalidOriginsArray(): void
6869
{
6970
$this->expectException(InvalidOptionException::class);
7071

72+
/** @phpstan-ignore-next-line */
7173
$service = new CorsService(['allowedOrigins' => 'string']);
7274
}
7375

7476
/**
7577
* @test
7678
*/
77-
public function itNormalizesWildcardOrigins()
79+
public function itNormalizesWildcardOrigins(): void
7880
{
7981
$origins = ['*'];
8082

@@ -87,7 +89,7 @@ public function itNormalizesWildcardOrigins()
8789
/**
8890
* @test
8991
*/
90-
public function itConvertsWildcardOriginPatterns()
92+
public function itConvertsWildcardOriginPatterns(): void
9193
{
9294
$service = new CorsService(['allowedOrigins' => ['*.mydomain.com']]);
9395
$this->assertInstanceOf(CorsService::class, $service);
@@ -99,7 +101,7 @@ public function itConvertsWildcardOriginPatterns()
99101
/**
100102
* @test
101103
*/
102-
public function itNormalizesUnderscoreOptions()
104+
public function itNormalizesUnderscoreOptions(): void
103105
{
104106
$origins = ['localhost'];
105107

@@ -109,7 +111,11 @@ public function itNormalizesUnderscoreOptions()
109111
$this->assertEquals($origins, $this->getOptionsFromService($service)['allowedOrigins']);
110112
}
111113

112-
private function getOptionsFromService(CorsService $service)
114+
/**
115+
* @param CorsService $service
116+
* @return array<mixed>
117+
*/
118+
private function getOptionsFromService(CorsService $service): array
113119
{
114120
$reflected = new \ReflectionClass($service);
115121

0 commit comments

Comments
 (0)