Skip to content

Commit e1a2a47

Browse files
Merge pull request #274 from MarcinOrlowski/dev
Release v12.1.1
2 parents aff27e6 + 828fa91 commit e1a2a47

File tree

7 files changed

+95
-79
lines changed

7 files changed

+95
-79
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
# quotes are needed it is treated as a number and zero at decimal part is gone
3030
# at runtime i.e. 8.10 -> 8.1, while "8.10" => "8.10".
3131
laravel: ["12.0"]
32-
php: ["8.2"]
32+
php: ["8.2", "8.3", "8.4"]
3333

3434
runs-on: ubuntu-latest
3535

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
matrix:
7070
# Quotes are needed it is treated as a number and zero at decimal part is gone
7171
# at runtime i.e. 8.10 -> 8.1, while "8.10" => "8.10".
72-
php: [ "8.2" ]
72+
php: [ "8.2", "8.3", "8.4" ]
7373

7474
runs-on: ubuntu-latest
7575

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "marcin-orlowski/laravel-api-response-builder",
33
"description": "Helps building nice, normalized and easy to consume Laravel REST API.",
44
"homepage": "https://github.com/MarcinOrlowski/laravel-api-response-builder",
5-
"version": "12.1.0",
5+
"version": "12.1.1",
66
"keywords": [
77
"laravel",
88
"laravel10",

docs/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ should be able to easily backport future new features to older versions.
1818

1919
## CHANGE LOG ##
2020

21+
* v12.1.1 (2025-07-16)
22+
* [GH-265] Fixed remaining PHP 8.4 deprecations `Implicitly nullable parameters...`
23+
* Included PHP 8.4 in PHPStan action
24+
2125
* v12.1.0 (2025-07-11)
2226
* [GH-267] Fixed `Util::mergeConfig()` causing PHPStan errors due to incorrect type usage.
2327
* Refactored `ResponseBuilder` to extract message handling into a new `MessageManager` class.

src/ResponseBuilder.php

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525
*/
2626
class ResponseBuilder extends ResponseBuilderBase
2727
{
28-
protected bool $success = false;
28+
protected bool $success = false;
2929
protected int $api_code;
30-
protected ?int $http_code = null;
31-
protected ?string $message = null;
30+
protected ?int $http_code = null;
31+
protected ?string $message = null;
3232
/** @var array<string, mixed>|null */
33-
protected ?array $placeholders = null;
34-
protected ?int $json_opts = null;
33+
protected ?array $placeholders = null;
34+
protected ?int $json_opts = null;
3535
/** @var array<string, mixed>|null */
36-
protected ?array $debug_data = null;
36+
protected ?array $debug_data = null;
3737
/** @var array<string, mixed> */
38-
protected array $http_headers = [];
38+
protected array $http_headers = [];
3939

4040
/** @var mixed|null $data */
4141
protected $data = null;
@@ -59,18 +59,18 @@ protected function __construct(bool $success, int $api_code)
5959
/**
6060
* Returns success
6161
*
62-
* @param mixed|null $data Array of primitives and supported objects to be returned in
63-
* 'data' node of the JSON response, single supported object
64-
* or @null if there's no to be returned.
65-
* @param integer|null $api_code API code to be returned or @null to use value of
66-
* BaseApiCodes::OK().
67-
* @param array<string, mixed>|null $placeholders Placeholders passed to Lang::get() for message placeholders
68-
* substitution or @null if none.
69-
* @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null
70-
* for default DEFAULT_HTTP_CODE_OK.
71-
* @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for
72-
* supported options or pass @null to use value from your
73-
* config (or defaults).
62+
* @param mixed|null $data Array of primitives and supported objects to be returned in
63+
* 'data' node of the JSON response, single supported object
64+
* or @null if there's no to be returned.
65+
* @param integer|null $api_code API code to be returned or @null to use value of
66+
* BaseApiCodes::OK().
67+
* @param array<string, mixed>|null $placeholders Placeholders passed to Lang::get() for message placeholders
68+
* substitution or @null if none.
69+
* @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null
70+
* for default DEFAULT_HTTP_CODE_OK.
71+
* @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for
72+
* supported options or pass @null to use value from your
73+
* config (or defaults).
7474
*
7575
* @throws Ex\MissingConfigurationKeyException
7676
* @throws Ex\ConfigurationNotFoundException
@@ -97,17 +97,17 @@ public static function success(mixed $data = null,
9797
* Builds error Response object. Supports optional arguments passed to Lang::get() if associated error
9898
* message uses placeholders as well as return data payload
9999
*
100-
* @param integer $api_code Your API code to be returned with the response object.
101-
* @param array<string, mixed>|null $placeholders Placeholders passed to Lang::get() for message
102-
* placeholders substitution or @null if none.
103-
* @param object|array<string, mixed>|null $data Array of primitives and supported objects to be
104-
* returned in 'data' node of the JSON response, single
105-
* supported object or @null if there's no to be returned.
106-
* @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null
107-
* for default DEFAULT_HTTP_CODE_ERROR.
108-
* @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php
109-
* for supported options or pass @null to use value from
110-
* your config (or defaults).
100+
* @param integer $api_code Your API code to be returned with the response object.
101+
* @param array<string, mixed>|null $placeholders Placeholders passed to Lang::get() for message
102+
* placeholders substitution or @null if none.
103+
* @param object|array<string, mixed>|null $data Array of primitives and supported objects to be
104+
* returned in 'data' node of the JSON response, single
105+
* supported object or @null if there's no to be returned.
106+
* @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null
107+
* for default DEFAULT_HTTP_CODE_ERROR.
108+
* @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php
109+
* for supported options or pass @null to use value from
110+
* your config (or defaults).
111111
*
112112
* @throws Ex\ArrayWithMixedKeysException
113113
* @throws Ex\MissingConfigurationKeyException
@@ -119,8 +119,8 @@ public static function success(mixed $data = null,
119119
public static function error(int $api_code,
120120
?array $placeholders = null,
121121
mixed $data = null,
122-
int $http_code = null,
123-
int $json_opts = null): HttpResponse
122+
?int $http_code = null,
123+
?int $json_opts = null): HttpResponse
124124
{
125125
return static::asError($api_code)
126126
->withPlaceholders($placeholders)
@@ -139,7 +139,7 @@ public static function error(int $api_code,
139139
* @throws Ex\MissingConfigurationKeyException
140140
* @throws Ex\NotIntegerException
141141
*/
142-
public static function asSuccess(int $api_code = null): self
142+
public static function asSuccess(?int $api_code = null): self
143143
{
144144
/** @noinspection PhpUnhandledExceptionInspection */
145145
return new static(true, $api_code ?? BaseApiCodes::OK());
@@ -321,21 +321,21 @@ public function build(): HttpResponse
321321
}
322322

323323
/**
324-
* @param boolean $success TRUE if response reports successful operation,
325-
* FALSE otherwise.
326-
* @param integer $api_code API code to be returned with the response object.
327-
* @param string|integer $msg_or_api_code Message string or valid API code to get message for
328-
* @param mixed|null $data optional additional data to be included in response.
329-
* @param integer|null $http_code HTTP code for the HttpResponse or @null for either
330-
* DEFAULT_HTTP_CODE_OK or DEFAULT_HTTP_CODE_ERROR
331-
* depending on the $success.
332-
* @param array<string, mixed>|null $placeholders Placeholders passed to Lang::get() for message
333-
* placeholders substitution or @null if none.
334-
* @param array<string, mixed>|null $http_headers Optional HTTP headers to be returned in the response.
335-
* @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php
336-
* for supported options or pass @null to use value from
337-
* your config (or defaults).
338-
* @param array<string, mixed>|null $debug_data Optional debug data array to be added to returned JSON.
324+
* @param boolean $success TRUE if response reports successful operation,
325+
* FALSE otherwise.
326+
* @param integer $api_code API code to be returned with the response object.
327+
* @param string|integer $msg_or_api_code Message string or valid API code to get message for
328+
* @param mixed|null $data optional additional data to be included in response.
329+
* @param integer|null $http_code HTTP code for the HttpResponse or @null for either
330+
* DEFAULT_HTTP_CODE_OK or DEFAULT_HTTP_CODE_ERROR
331+
* depending on the $success.
332+
* @param array<string, mixed>|null $placeholders Placeholders passed to Lang::get() for message
333+
* placeholders substitution or @null if none.
334+
* @param array<string, mixed>|null $http_headers Optional HTTP headers to be returned in the response.
335+
* @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php
336+
* for supported options or pass @null to use value from
337+
* your config (or defaults).
338+
* @param array<string, mixed>|null $debug_data Optional debug data array to be added to returned JSON.
339339
*
340340
* @throws Ex\MissingConfigurationKeyException
341341
* @throws Ex\ConfigurationNotFoundException
@@ -382,13 +382,13 @@ protected function make(bool $success,
382382
* added to reported JSON for easier manual debugging. Returns response ready to be encoded as
383383
* JSON and sent back to client.
384384
*
385-
* @param boolean $success TRUE if response reports successful operation, FALSE otherwise.
386-
* @param integer $api_code Your API code to be returned with the response object.
387-
* @param string|integer $msg_or_api_code Message string or valid API code to get message for.
388-
* @param array<string, mixed>|null $placeholders Placeholders passed to Lang::get() for message placeholders
389-
* substitution or @null if none.
390-
* @param mixed|null $data API response data if any
391-
* @param array<string, mixed>|null $debug_data optional debug data array to be added to returned JSON.
385+
* @param boolean $success TRUE if response reports successful operation, FALSE otherwise.
386+
* @param integer $api_code Your API code to be returned with the response object.
387+
* @param string|integer $msg_or_api_code Message string or valid API code to get message for.
388+
* @param array<string, mixed>|null $placeholders Placeholders passed to Lang::get() for message placeholders
389+
* substitution or @null if none.
390+
* @param mixed|null $data API response data if any
391+
* @param array<string, mixed>|null $debug_data optional debug data array to be added to returned JSON.
392392
*
393393
* @return array<string, mixed>
394394
*

tests/Traits/TestingHelpers.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ public function setUp(): void
9191
}
9292
$idx = \random_int(1, \count($map));
9393
$keys = \array_keys($map);
94-
$key = $keys[ $idx - 1 ];
95-
$this->random_api_code_message_key = $map[ $key ];
94+
$key = $keys[$idx - 1];
95+
$this->random_api_code_message_key = $map[$key];
9696
$this->random_api_code_message = $this->langGet($this->random_api_code_message_key,
97-
['api_code' => $this->random_api_code,]);
97+
['api_code' => $this->random_api_code,]);
9898

9999
$this->error_message_map = [
100100
$this->random_api_code => $this->random_api_code_message_key,
@@ -124,7 +124,7 @@ public function getResponseContent(HttpResponse $response): string
124124
* do not want to happen, not handle separately after each invocation, so this wrapper deals with it for
125125
* us.
126126
*
127-
* @param string $key String key as passed to Lang::get()
127+
* @param string $key String key as passed to Lang::get()
128128
* @param array<string, mixed>|null $replace Optional replacement array as passed to Lang::get()
129129
*/
130130
public function langGet(string $key, ?array $replace = null): string
@@ -155,12 +155,12 @@ public function langGet(string $key, ?array $replace = null): string
155155
* @throws \ReflectionException
156156
*/
157157
public function getResponseSuccessObject(?int $expected_api_code_offset = null,
158-
int $expected_http_code = null,
158+
?int $expected_http_code = null,
159159
?string $expected_message = null): ApiResponse
160160
{
161161
if ($expected_api_code_offset === null) {
162-
/** @var string $api_codes */
163162
$api_codes = $this->getApiCodesClassName();
163+
/** @var string $api_codes */
164164
/** @var int $expected_api_code_offset */
165165
$expected_api_code_offset = Lockpick::getConstant($api_codes, 'OK_OFFSET');
166166
}
@@ -210,11 +210,11 @@ public function getResponseErrorObject(?int $expected_api_code_offset = null,
210210

211211
if ($expected_http_code > RB::ERROR_HTTP_CODE_MAX) {
212212
$this->fail(\sprintf('TEST: Error HTTP code (%d) cannot be higher than %d',
213-
$expected_http_code, RB::ERROR_HTTP_CODE_MAX));
213+
$expected_http_code, RB::ERROR_HTTP_CODE_MAX));
214214
}
215215
if ($expected_http_code < RB::ERROR_HTTP_CODE_MIN) {
216216
$this->fail(\sprintf('TEST: Error HTTP code (%d) cannot be lower than %d',
217-
$expected_http_code, RB::ERROR_HTTP_CODE_MIN));
217+
$expected_http_code, RB::ERROR_HTTP_CODE_MIN));
218218
}
219219

220220
// Ensure we have a valid integer for API code
@@ -245,7 +245,7 @@ private function getResponseObjectRaw(int $expected_api_code,
245245
$actual = $this->response->getStatusCode();
246246
$contents = $this->getResponseContent($this->response);
247247
$this->assertEquals($expected_http_code, $actual,
248-
"Expected status code {$expected_http_code}, got {$actual}. Response: {$contents}");
248+
"Expected status code {$expected_http_code}, got {$actual}. Response: {$contents}");
249249

250250
// get response
251251
$api = ApiResponse::fromJson($this->getResponseContent($this->response));
@@ -289,9 +289,9 @@ public function assertResponseStatusCode(int $expected_code, \stdClass $response
289289
$response_code_int = is_int($response_code) ? $response_code : (int)$response_code;
290290
}
291291
$msg = \sprintf('Status code mismatch. Expected: %s, found %s. Message: "%s"',
292-
$this->resolveConstantFromCode($expected_code),
293-
$this->resolveConstantFromCode($response_code_int),
294-
is_string($response_json->message) ? $response_json->message : var_export($response_json->message, true));
292+
$this->resolveConstantFromCode($expected_code),
293+
$this->resolveConstantFromCode($response_code_int),
294+
is_string($response_json->message) ? $response_json->message : var_export($response_json->message, true));
295295

296296
$this->fail($msg);
297297
}
@@ -334,14 +334,14 @@ protected function callMakeMethod(bool $success,
334334

335335
$result = Lockpick::call(
336336
RB::asSuccess(), 'make', [$success,
337-
$api_code_offset,
338-
$message_or_api_code_offset,
339-
$data,
340-
$http_code,
341-
$lang_args,
342-
$headers,
343-
$encoding_options,
344-
$debug_data,
337+
$api_code_offset,
338+
$message_or_api_code_offset,
339+
$data,
340+
$http_code,
341+
$lang_args,
342+
$headers,
343+
$encoding_options,
344+
$debug_data,
345345
]);
346346

347347
/** @var HttpResponse $result */
@@ -393,7 +393,7 @@ protected function resolveConstantFromCode(int $api_code_offset)
393393
*/
394394
protected function ord8(string $string, int &$offset): int
395395
{
396-
$code = \ord($string[ $offset ]);
396+
$code = \ord($string[$offset]);
397397
$bytes_number = 1;
398398
if ($code >= 128) { //otherwise 0xxxxxxx
399399
if ($code < 224) { //110xxxxx

tests/phpunit/Builder/BuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* @link https://github.com/MarcinOrlowski/laravel-api-response-builder
1717
*/
1818

19+
use Illuminate\Http\JsonResponse;
1920
use MarcinOrlowski\PhpunitExtraAsserts\Generator;
2021
use MarcinOrlowski\ResponseBuilder\BaseApiCodes;
2122
use MarcinOrlowski\ResponseBuilder\Builder;
@@ -89,4 +90,15 @@ public function testErrorWithOkCode(): void
8990
RB::asError(BaseApiCodes::OK());
9091
}
9192

93+
/**
94+
* Checksi if explicit @null as HTTP code falls back to default HTTP error code..
95+
*/
96+
public function testErrorWithNullHttpCode(): void
97+
{
98+
$apiCode = BaseApiCodes::getMinCode();
99+
$response = RB::error($apiCode, http_code: null);
100+
$this->assertInstanceOf(JsonResponse::class, $response);
101+
$this->assertEquals(RB::DEFAULT_HTTP_CODE_ERROR, $response->getStatusCode());
102+
}
103+
92104
} // end of class

0 commit comments

Comments
 (0)