Skip to content

Commit e2c1a5e

Browse files
authored
Merge pull request #33 from samsonasik/apply-php80
Apply PHP 8.0 Syntax and constructor promotion
2 parents ee10fe6 + 6152adb commit e2c1a5e

9 files changed

+67
-86
lines changed

src/ProblemDetailsMiddleware.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ class ProblemDetailsMiddleware implements MiddlewareInterface
2828
/** @var callable[] */
2929
private array $listeners = [];
3030

31-
private ProblemDetailsResponseFactory $responseFactory;
32-
33-
public function __construct(ProblemDetailsResponseFactory $responseFactory)
31+
public function __construct(private ProblemDetailsResponseFactory $responseFactory)
3432
{
35-
$this->responseFactory = $responseFactory;
3633
}
3734

3835
/**
@@ -107,7 +104,7 @@ private function createErrorHandler(): callable
107104
* @param int $errline
108105
* @throws ErrorException if error is not within the error_reporting mask.
109106
*/
110-
return function (int $errno, string $errstr, string $errfile, int $errline): void {
107+
return static function (int $errno, string $errstr, string $errfile, int $errline): void {
111108
if (! (error_reporting() & $errno)) {
112109
// error_reporting does not include this error
113110
return;
@@ -125,7 +122,7 @@ private function triggerListeners(
125122
ServerRequestInterface $request,
126123
ResponseInterface $response
127124
): void {
128-
array_walk($this->listeners, function ($listener) use ($error, $request, $response) {
125+
array_walk($this->listeners, static function ($listener) use ($error, $request, $response): void {
129126
$listener($error, $request, $response);
130127
});
131128
}

src/ProblemDetailsNotFoundHandler.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515

1616
class ProblemDetailsNotFoundHandler implements MiddlewareInterface
1717
{
18-
private ProblemDetailsResponseFactory $responseFactory;
19-
2018
/**
2119
* @param ProblemDetailsResponseFactory $responseFactory Factory to create a response to
2220
* update and return when returning an 404 response.
2321
*/
24-
public function __construct(ProblemDetailsResponseFactory $responseFactory)
22+
public function __construct(private ProblemDetailsResponseFactory $responseFactory)
2523
{
26-
$this->responseFactory = $responseFactory;
2724
}
2825

2926
/**

src/ProblemDetailsResponseFactory.php

+31-43
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
use function preg_replace;
2727
use function print_r;
2828
use function sprintf;
29+
use function str_contains;
2930
use function str_replace;
30-
use function strpos;
3131

3232
use const JSON_PARTIAL_OUTPUT_ON_ERROR;
3333
use const JSON_PRESERVE_ZERO_FRACTION;
@@ -149,15 +149,6 @@ class ProblemDetailsResponseFactory
149149
'application/*+xml',
150150
];
151151

152-
/**
153-
* Whether or not to include debug details.
154-
*
155-
* Debug details are only included for responses created from throwables,
156-
* and include full exception details and previous exceptions and their
157-
* details.
158-
*/
159-
private bool $isDebug;
160-
161152
/**
162153
* JSON flags to use when generating JSON response payload.
163154
*
@@ -177,38 +168,39 @@ class ProblemDetailsResponseFactory
177168
*/
178169
private ResponseFactoryInterface $responseFactory;
179170

180-
/**
181-
* Flag to enable show exception details in detail field.
182-
*
183-
* Disabled by default for security reasons.
184-
*/
185-
private bool $exceptionDetailsInResponse;
186-
187-
/**
188-
* Default detail field value. Will be visible when
189-
* $exceptionDetailsInResponse disabled.
190-
*
191-
* Empty string by default
192-
*/
193-
private string $defaultDetailMessage;
194-
195-
/**
196-
* A map used to infer the "type" property based on the status code.
197-
*
198-
* Defaults to an empty map.
199-
*/
200-
private array $defaultTypesMap;
201-
202171
/**
203172
* @param (callable():ResponseInterface)|ResponseFactoryInterface $responseFactory
204173
*/
205174
public function __construct(
206175
$responseFactory,
207-
bool $isDebug = self::EXCLUDE_THROWABLE_DETAILS,
176+
/**
177+
* Whether or not to include debug details.
178+
*
179+
* Debug details are only included for responses created from throwables,
180+
* and include full exception details and previous exceptions and their
181+
* details.
182+
*/
183+
private bool $isDebug = self::EXCLUDE_THROWABLE_DETAILS,
208184
?int $jsonFlags = null,
209-
bool $exceptionDetailsInResponse = false,
210-
string $defaultDetailMessage = self::DEFAULT_DETAIL_MESSAGE,
211-
array $defaultTypesMap = []
185+
/**
186+
* Flag to enable show exception details in detail field.
187+
*
188+
* Disabled by default for security reasons.
189+
*/
190+
private bool $exceptionDetailsInResponse = false,
191+
/**
192+
* Default detail field value. Will be visible when
193+
* $exceptionDetailsInResponse disabled.
194+
*
195+
* Empty string by default
196+
*/
197+
private string $defaultDetailMessage = self::DEFAULT_DETAIL_MESSAGE,
198+
/**
199+
* A map used to infer the "type" property based on the status code.
200+
*
201+
* Defaults to an empty map.
202+
*/
203+
private array $defaultTypesMap = []
212204
) {
213205
if (is_callable($responseFactory)) {
214206
$responseFactory = new CallableResponseFactoryDecorator(
@@ -217,7 +209,6 @@ public function __construct(
217209
}
218210
// Ensures type safety of the composed factory
219211
$this->responseFactory = $responseFactory;
220-
$this->isDebug = $isDebug;
221212
if (! $jsonFlags) {
222213
$jsonFlags = JSON_UNESCAPED_SLASHES
223214
| JSON_UNESCAPED_UNICODE
@@ -227,10 +218,7 @@ public function __construct(
227218
$jsonFlags = JSON_PRETTY_PRINT | $jsonFlags;
228219
}
229220
}
230-
$this->jsonFlags = $jsonFlags;
231-
$this->exceptionDetailsInResponse = $exceptionDetailsInResponse;
232-
$this->defaultDetailMessage = $defaultDetailMessage;
233-
$this->defaultTypesMap = $defaultTypesMap;
221+
$this->jsonFlags = $jsonFlags;
234222
}
235223

236224
public function createResponse(
@@ -254,7 +242,7 @@ public function createResponse(
254242

255243
if ($additional) {
256244
// ensure payload can be json_encoded
257-
array_walk_recursive($additional, function (&$value) {
245+
array_walk_recursive($additional, static function (&$value): void {
258246
if (is_resource($value)) {
259247
$value = print_r($value, true) . ' of type ' . get_resource_type($value);
260248
}
@@ -374,7 +362,7 @@ private function getResponseGenerator(ServerRequestInterface $request): callable
374362
$accept = $request->getHeaderLine('Accept') ?: '*/*';
375363
$mediaType = (new Negotiator())->getBest($accept, self::NEGOTIATION_PRIORITIES);
376364

377-
return ! $mediaType || false === strpos($mediaType->getValue(), 'json')
365+
return ! $mediaType || ! str_contains($mediaType->getValue(), 'json')
378366
? Closure::fromCallable([$this, 'generateXmlResponse'])
379367
: Closure::fromCallable([$this, 'generateJsonResponse']);
380368
}

test/InMemoryContainer.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public function has($id): bool
3737
return array_key_exists($id, $this->services);
3838
}
3939

40-
/** @param mixed $item */
41-
public function set(string $id, $item): void
40+
public function set(string $id, mixed $item): void
4241
{
4342
$this->services[$id] = $item;
4443
}

test/ProblemDetailsAssertionsTrait.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function preparePayloadForJsonResponse(MockObject $stream, callable $asse
8383
$stream
8484
->expects($this->any())
8585
->method('write')
86-
->with($this->callback(function ($body) use ($assertion) {
86+
->with($this->callback(static function ($body) use ($assertion): bool {
8787
Assert::assertIsString($body);
8888
$data = json_decode($body, true);
8989
$assertion($data);
@@ -99,7 +99,7 @@ public function preparePayloadForXmlResponse(MockObject $stream, callable $asser
9999
$stream
100100
->expects($this->any())
101101
->method('write')
102-
->with($this->callback(function ($body) use ($assertion) {
102+
->with($this->callback(function ($body) use ($assertion): bool {
103103
Assert::assertIsString($body);
104104
$data = $this->deserializeXmlPayload($body);
105105
$assertion($data);
@@ -114,7 +114,7 @@ public function deserializeXmlPayload(string $xml): array
114114
$payload = json_decode($json, true);
115115

116116
// Ensure ints and floats are properly represented
117-
array_walk_recursive($payload, function (&$item) {
117+
array_walk_recursive($payload, static function (&$item): void {
118118
if ((string) (int) $item === $item) {
119119
$item = (int) $item;
120120
return;

test/ProblemDetailsMiddlewareTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ public function testMiddlewareRegistersErrorHandlerToConvertErrorsToProblemDetai
104104
$handler
105105
->method('handle')
106106
->with($this->request)
107-
->willReturnCallback(function () {
107+
->willReturnCallback(static function (): void {
108108
trigger_error('Triggered error!', E_USER_ERROR);
109109
});
110110

111111
$expected = $this->createMock(ResponseInterface::class);
112112
$this->responseFactory
113113
->method('createResponseFromThrowable')
114-
->with($this->request, $this->callback(function ($e) {
114+
->with($this->request, $this->callback(function ($e): bool {
115115
$this->assertInstanceOf(ErrorException::class, $e);
116116
$this->assertEquals(E_USER_ERROR, $e->getSeverity());
117117
$this->assertEquals('Triggered error!', $e->getMessage());
@@ -168,7 +168,7 @@ public function testErrorHandlingTriggersListeners(string $accept): void
168168
->with($this->request, $exception)
169169
->willReturn($expected);
170170

171-
$listener = function ($error, $request, $response) use ($exception, $expected) {
171+
$listener = function ($error, $request, $response) use ($exception, $expected): void {
172172
$this->assertSame($exception, $error, 'Listener did not receive same exception as was raised');
173173
$this->assertSame($this->request, $request, 'Listener did not receive same request');
174174
$this->assertSame($expected, $response, 'Listener did not receive same response');

test/ProblemDetailsResponseFactoryFactoryTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function testUsesPrettyPrintFlagOnEnabledDebugMode(): void
127127
['config', ['debug' => true]],
128128
[
129129
ResponseInterface::class,
130-
function () {
130+
static function (): void {
131131
},
132132
],
133133
]);
@@ -154,7 +154,7 @@ public function testUsesDebugSettingFromConfigWhenPresent(): void
154154
['config', ['debug' => true]],
155155
[
156156
ResponseInterface::class,
157-
function () {
157+
static function (): void {
158158
},
159159
],
160160
]);
@@ -185,7 +185,7 @@ public function testUsesJsonFlagsSettingFromConfigWhenPresent(): void
185185
['config', ['problem-details' => ['json_flags' => JSON_PRETTY_PRINT]]],
186186
[
187187
ResponseInterface::class,
188-
function () {
188+
static function (): void {
189189
},
190190
],
191191
]);
@@ -216,7 +216,7 @@ public function testUsesDefaultTypesSettingFromConfigWhenPresent(): void
216216
['config', ['problem-details' => ['default_types_map' => $expectedDefaultTypes]]],
217217
[
218218
ResponseInterface::class,
219-
function () {
219+
static function (): void {
220220
},
221221
],
222222
]);

0 commit comments

Comments
 (0)