Skip to content

Commit d903a0e

Browse files
committed
qa: apply automated fixes from Psalm and set initial baseline
Signed-off-by: Matthew Weier O'Phinney <[email protected]>
1 parent e10dc22 commit d903a0e

14 files changed

+606
-54
lines changed

psalm-baseline.xml

Lines changed: 546 additions & 0 deletions
Large diffs are not rendered by default.

src/ApiProblem.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,11 @@ protected function getDetail()
259259
*
260260
* If an exception was provided, creates the status code from it;
261261
* otherwise, code as provided is used.
262-
*
263-
* @return string
264262
*/
265-
protected function getStatus()
263+
protected function getStatus(): int
266264
{
267265
if ($this->detail instanceof Throwable || $this->detail instanceof Exception) {
268-
$this->status = $this->createStatusFromException();
266+
$this->status = (int) $this->createStatusFromException();
269267
}
270268

271269
return $this->status;
@@ -346,7 +344,7 @@ protected function createDetailFromException()
346344
/**
347345
* Create HTTP status from an exception.
348346
*
349-
* @return int
347+
* @return int|string
350348
*/
351349
protected function createStatusFromException()
352350
{

src/Listener/ApiProblemListener.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public function attach(EventManagerInterface $events, $priority = 1)
7676

7777
/**
7878
* Listen to the render event.
79+
*
80+
* @return void
7981
*/
8082
public function onRender(MvcEvent $e)
8183
{
@@ -111,6 +113,8 @@ public function onRender(MvcEvent $e)
111113
* Handle dispatch.
112114
*
113115
* It checks if the controller is in our list
116+
*
117+
* @return void
114118
*/
115119
public function onDispatch(MvcEvent $e)
116120
{
@@ -140,7 +144,7 @@ public function onDispatch(MvcEvent $e)
140144
* If the event represents an error, and has an exception composed, marshals an ApiProblem
141145
* based on the exception, stops event propagation, and returns an ApiProblemResponse.
142146
*
143-
* @return ApiProblemResponse
147+
* @return ApiProblemResponse|null
144148
*/
145149
public function onDispatchError(MvcEvent $e)
146150
{

src/Listener/RenderErrorListener.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function setDisplayExceptions($flag)
4848
* the PhpRenderer, when we have no templates.
4949
*
5050
* As such, report as an unacceptable response.
51+
*
52+
* @return void
5153
*/
5254
public function onRenderError(MvcEvent $e)
5355
{

src/Listener/SendApiProblemResponseListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function __invoke(SendResponseEvent $event)
114114
/**
115115
* Merge headers set on the application response into the API Problem response.
116116
*/
117-
protected function mergeHeaders(HttpResponse $applicationResponse, ApiProblemResponse $apiProblemResponse)
117+
protected function mergeHeaders(HttpResponse $applicationResponse, ApiProblemResponse $apiProblemResponse): void
118118
{
119119
$apiProblemHeaders = $apiProblemResponse->getHeaders();
120120
foreach ($applicationResponse->getHeaders() as $header) {

src/Module.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function getConfig()
2222
* Listener for bootstrap event.
2323
*
2424
* Attaches a render event.
25+
*
26+
* @return void
2527
*/
2628
public function onBootstrap(MvcEvent $e)
2729
{
@@ -44,6 +46,8 @@ public function onBootstrap(MvcEvent $e)
4446
* Listener for the render event.
4547
*
4648
* Attaches a rendering/response strategy to the View.
49+
*
50+
* @return void
4751
*/
4852
public function onRender(MvcEvent $e)
4953
{

test/ApiProblemResponseTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class ApiProblemResponseTest extends TestCase
1616
{
17-
public function testApiProblemResponseIsAnHttpResponse()
17+
public function testApiProblemResponseIsAnHttpResponse(): void
1818
{
1919
$response = new ApiProblemResponse(new ApiProblem(400, 'Random error'));
2020
$this->assertInstanceOf(Response::class, $response);
@@ -23,7 +23,7 @@ public function testApiProblemResponseIsAnHttpResponse()
2323
/**
2424
* @depends testApiProblemResponseIsAnHttpResponse
2525
*/
26-
public function testApiProblemResponseSetsStatusCodeAndReasonPhrase()
26+
public function testApiProblemResponseSetsStatusCodeAndReasonPhrase(): void
2727
{
2828
$response = new ApiProblemResponse(new ApiProblem(400, 'Random error'));
2929
$this->assertEquals(400, $response->getStatusCode());
@@ -32,7 +32,7 @@ public function testApiProblemResponseSetsStatusCodeAndReasonPhrase()
3232
$this->assertEquals('bad request', strtolower($response->getReasonPhrase()));
3333
}
3434

35-
public function testApiProblemResponseBodyIsSerializedApiProblem()
35+
public function testApiProblemResponseBodyIsSerializedApiProblem(): void
3636
{
3737
$additional = [
3838
'foo' => fopen('php://memory', 'r'),
@@ -54,7 +54,7 @@ public function testApiProblemResponseBodyIsSerializedApiProblem()
5454
/**
5555
* @depends testApiProblemResponseIsAnHttpResponse
5656
*/
57-
public function testApiProblemResponseSetsContentTypeHeader()
57+
public function testApiProblemResponseSetsContentTypeHeader(): void
5858
{
5959
$response = new ApiProblemResponse(new ApiProblem(400, 'Random error'));
6060
$headers = $response->getHeaders();
@@ -64,7 +64,7 @@ public function testApiProblemResponseSetsContentTypeHeader()
6464
$this->assertEquals(ApiProblem::CONTENT_TYPE, $header->getFieldValue());
6565
}
6666

67-
public function testComposeApiProblemIsAccessible()
67+
public function testComposeApiProblemIsAccessible(): void
6868
{
6969
$apiProblem = new ApiProblem(400, 'Random error');
7070
$response = new ApiProblemResponse($apiProblem);
@@ -74,7 +74,7 @@ public function testComposeApiProblemIsAccessible()
7474
/**
7575
* @group 14
7676
*/
77-
public function testOverridesReasonPhraseIfStatusCodeIsUnknown()
77+
public function testOverridesReasonPhraseIfStatusCodeIsUnknown(): void
7878
{
7979
$response = new ApiProblemResponse(new ApiProblem(7, 'Random error'));
8080
$this->assertStringContainsString('Internal Server Error', $response->getReasonPhrase());

test/ApiProblemTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function statusCodes(): array
2929
/**
3030
* @dataProvider statusCodes
3131
*/
32-
public function testStatusIsUsedVerbatim(int $status)
32+
public function testStatusIsUsedVerbatim(int $status): void
3333
{
3434
$apiProblem = new ApiProblem($status, 'foo');
3535
$payload = $apiProblem->toArray();
@@ -40,7 +40,7 @@ public function testStatusIsUsedVerbatim(int $status)
4040
/**
4141
* @requires PHP 7.0
4242
*/
43-
public function testErrorAsDetails()
43+
public function testErrorAsDetails(): void
4444
{
4545
$error = new TypeError('error message', 705);
4646
$apiProblem = new ApiProblem(500, $error);
@@ -54,7 +54,7 @@ public function testErrorAsDetails()
5454
$this->assertEquals('error message', $payload['detail']);
5555
}
5656

57-
public function testExceptionCodeIsUsedForStatus()
57+
public function testExceptionCodeIsUsedForStatus(): void
5858
{
5959
$exception = new \Exception('exception message', 401);
6060
$apiProblem = new ApiProblem('500', $exception);
@@ -63,15 +63,15 @@ public function testExceptionCodeIsUsedForStatus()
6363
$this->assertEquals($exception->getCode(), $payload['status']);
6464
}
6565

66-
public function testDetailStringIsUsedVerbatim()
66+
public function testDetailStringIsUsedVerbatim(): void
6767
{
6868
$apiProblem = new ApiProblem('500', 'foo');
6969
$payload = $apiProblem->toArray();
7070
$this->assertArrayHasKey('detail', $payload);
7171
$this->assertEquals('foo', $payload['detail']);
7272
}
7373

74-
public function testExceptionMessageIsUsedForDetail()
74+
public function testExceptionMessageIsUsedForDetail(): void
7575
{
7676
$exception = new \Exception('exception message');
7777
$apiProblem = new ApiProblem('500', $exception);
@@ -80,7 +80,7 @@ public function testExceptionMessageIsUsedForDetail()
8080
$this->assertEquals($exception->getMessage(), $payload['detail']);
8181
}
8282

83-
public function testExceptionsCanTriggerInclusionOfStackTraceInDetails()
83+
public function testExceptionsCanTriggerInclusionOfStackTraceInDetails(): void
8484
{
8585
$exception = new \Exception('exception message');
8686
$apiProblem = new ApiProblem('500', $exception);
@@ -91,7 +91,7 @@ public function testExceptionsCanTriggerInclusionOfStackTraceInDetails()
9191
$this->assertEquals($exception->getTrace(), $payload['trace']);
9292
}
9393

94-
public function testExceptionsCanTriggerInclusionOfNestedExceptions()
94+
public function testExceptionsCanTriggerInclusionOfNestedExceptions(): void
9595
{
9696
$exceptionChild = new \Exception('child exception');
9797
$exceptionParent = new \Exception('parent exception', null, $exceptionChild);
@@ -111,7 +111,7 @@ public function testExceptionsCanTriggerInclusionOfNestedExceptions()
111111
$this->assertEquals($expected, $payload['exception_stack']);
112112
}
113113

114-
public function testTypeUrlIsUsedVerbatim()
114+
public function testTypeUrlIsUsedVerbatim(): void
115115
{
116116
$apiProblem = new ApiProblem('500', 'foo', 'http://status.dev:8080/details.md');
117117
$payload = $apiProblem->toArray();
@@ -133,7 +133,7 @@ public function knownStatusCodes(): array
133133
/**
134134
* @dataProvider knownStatusCodes
135135
*/
136-
public function testKnownStatusResultsInKnownTitle(int $status)
136+
public function testKnownStatusResultsInKnownTitle(int $status): void
137137
{
138138
$apiProblem = new ApiProblem($status, 'foo');
139139
$r = new ReflectionObject($apiProblem);
@@ -146,23 +146,23 @@ public function testKnownStatusResultsInKnownTitle(int $status)
146146
$this->assertEquals($titles[$status], $payload['title']);
147147
}
148148

149-
public function testUnknownStatusResultsInUnknownTitle()
149+
public function testUnknownStatusResultsInUnknownTitle(): void
150150
{
151151
$apiProblem = new ApiProblem(420, 'foo');
152152
$payload = $apiProblem->toArray();
153153
$this->assertArrayHasKey('title', $payload);
154154
$this->assertEquals('Unknown', $payload['title']);
155155
}
156156

157-
public function testProvidedTitleIsUsedVerbatim()
157+
public function testProvidedTitleIsUsedVerbatim(): void
158158
{
159159
$apiProblem = new ApiProblem('500', 'foo', 'http://status.dev:8080/details.md', 'some title');
160160
$payload = $apiProblem->toArray();
161161
$this->assertArrayHasKey('title', $payload);
162162
$this->assertEquals('some title', $payload['title']);
163163
}
164164

165-
public function testCanPassArbitraryDetailsToConstructor()
165+
public function testCanPassArbitraryDetailsToConstructor(): void
166166
{
167167
$problem = new ApiProblem(
168168
400,
@@ -174,7 +174,7 @@ public function testCanPassArbitraryDetailsToConstructor()
174174
$this->assertEquals('bar', $problem->foo);
175175
}
176176

177-
public function testArraySerializationIncludesArbitraryDetails()
177+
public function testArraySerializationIncludesArbitraryDetails(): void
178178
{
179179
$problem = new ApiProblem(
180180
400,
@@ -188,7 +188,7 @@ public function testArraySerializationIncludesArbitraryDetails()
188188
$this->assertEquals('bar', $array['foo']);
189189
}
190190

191-
public function testArbitraryDetailsShouldNotOverwriteRequiredFieldsInArraySerialization()
191+
public function testArbitraryDetailsShouldNotOverwriteRequiredFieldsInArraySerialization(): void
192192
{
193193
$problem = new ApiProblem(
194194
400,
@@ -202,7 +202,7 @@ public function testArbitraryDetailsShouldNotOverwriteRequiredFieldsInArraySeria
202202
$this->assertEquals('Invalid entity', $array['title']);
203203
}
204204

205-
public function testUsesTitleFromExceptionWhenProvided()
205+
public function testUsesTitleFromExceptionWhenProvided(): void
206206
{
207207
$exception = new Exception\DomainException('exception message', 401);
208208
$exception->setTitle('problem title');
@@ -212,7 +212,7 @@ public function testUsesTitleFromExceptionWhenProvided()
212212
$this->assertEquals($exception->getTitle(), $payload['title']);
213213
}
214214

215-
public function testUsesTypeFromExceptionWhenProvided()
215+
public function testUsesTypeFromExceptionWhenProvided(): void
216216
{
217217
$exception = new Exception\DomainException('exception message', 401);
218218
$exception->setType('http://example.com/api/help/401');
@@ -222,7 +222,7 @@ public function testUsesTypeFromExceptionWhenProvided()
222222
$this->assertEquals($exception->getType(), $payload['type']);
223223
}
224224

225-
public function testUsesAdditionalDetailsFromExceptionWhenProvided()
225+
public function testUsesAdditionalDetailsFromExceptionWhenProvided(): void
226226
{
227227
$exception = new Exception\DomainException('exception message', 401);
228228
$exception->setAdditionalDetails(['foo' => 'bar']);
@@ -248,7 +248,7 @@ public function invalidStatusCodes(): array
248248
* @dataProvider invalidStatusCodes
249249
* @group api-tools-118
250250
*/
251-
public function testInvalidHttpStatusCodesAreCastTo500(int $code)
251+
public function testInvalidHttpStatusCodesAreCastTo500(int $code): void
252252
{
253253
$e = new \Exception('Testing', $code);
254254
$problem = new ApiProblem($code, $e);

test/Listener/ApiProblemListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ protected function setUp(): void
2525
$this->listener = new ApiProblemListener();
2626
}
2727

28-
public function testOnRenderReturnsEarlyWhenNonHttpRequestDetected()
28+
public function testOnRenderReturnsEarlyWhenNonHttpRequestDetected(): void
2929
{
3030
$request = $this->prophesize(RequestInterface::class)->reveal();
3131
$this->event->setRequest($request);
3232

3333
$this->assertNull($this->listener->onRender($this->event));
3434
}
3535

36-
public function testOnDispatchErrorReturnsAnApiProblemResponseBasedOnCurrentEventException()
36+
public function testOnDispatchErrorReturnsAnApiProblemResponseBasedOnCurrentEventException(): void
3737
{
3838
$request = new Request();
3939
$request->getHeaders()->addHeaderLine('Accept', 'application/json');
@@ -57,7 +57,7 @@ public function testOnDispatchErrorReturnsAnApiProblemResponseBasedOnCurrentEven
5757
/**
5858
* @requires PHP 7.0
5959
*/
60-
public function testOnDispatchErrorReturnsAnApiProblemResponseBasedOnCurrentEventThrowable()
60+
public function testOnDispatchErrorReturnsAnApiProblemResponseBasedOnCurrentEventThrowable(): void
6161
{
6262
$request = new Request();
6363
$request->getHeaders()->addHeaderLine('Accept', 'application/json');

test/Listener/RenderErrorListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function setUp(): void
2424
$this->listener = new RenderErrorListener();
2525
}
2626

27-
public function testOnRenderErrorCreatesAnApiProblemResponse()
27+
public function testOnRenderErrorCreatesAnApiProblemResponse(): void
2828
{
2929
$response = new Response();
3030
$request = new Request();
@@ -55,7 +55,7 @@ public function testOnRenderErrorCreatesAnApiProblemResponse()
5555
$this->assertStringContainsString('accept', $content['detail']);
5656
}
5757

58-
public function testOnRenderErrorCreatesAnApiProblemResponseFromException()
58+
public function testOnRenderErrorCreatesAnApiProblemResponseFromException(): void
5959
{
6060
$response = new Response();
6161
$request = new Request();
@@ -101,7 +101,7 @@ public function testOnRenderErrorCreatesAnApiProblemResponseFromException()
101101
/**
102102
* @requires PHP 7.0
103103
*/
104-
public function testOnRenderErrorCreatesAnApiProblemResponseFromThrowable()
104+
public function testOnRenderErrorCreatesAnApiProblemResponseFromThrowable(): void
105105
{
106106
$response = new Response();
107107
$request = new Request();

0 commit comments

Comments
 (0)