Skip to content

Commit 7b9d4fd

Browse files
Merge pull request #131 from MarcinOrlowski/dev
Release v7.0.1
2 parents bb6c145 + 1f2c5eb commit 7b9d4fd

File tree

11 files changed

+192
-77
lines changed

11 files changed

+192
-77
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
66

77
## CHANGE LOG ##
88

9+
* v7.0.1 (2019-11-25)
10+
* Disabled Scrutinizer's "false positive" in Builder class.
11+
* Added more tests to improve overall coverage.
12+
* Updated PHPDocs
13+
914
* v7.0.0 (2019-11-22)
1015
* **BACKWARD INCOMPATIBLE CHANGES** ([more info](docs/compatibility.md))
1116
* New, flexible API based on `Builder` pattern (see [docs](docs/compatibility.md) for details).

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": "7.0.0",
5+
"version": "7.0.1",
66
"keywords": [
77
"laravel",
88
"json",

docs/docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
## Table of contents ##
99

1010
* [Response structure](#response-structure)
11-
* [Usage examples](#examples.md)
11+
* [Usage examples](examples.md)
1212
* [Return Codes and Code Ranges](#return-codes)
1313
* [Exposed Methods](#exposed-methods)
1414
* [Data Conversion](#data-conversion)

src/BaseApiCodes.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,21 @@ protected static function getBaseMap(): array
9898
$tpl = 'response-builder::builder.http_%d';
9999

100100
return [
101+
/** @scrutinizer ignore-deprecated */
101102
self::OK() => 'response-builder::builder.ok',
103+
/** @scrutinizer ignore-deprecated */
102104
self::NO_ERROR_MESSAGE() => 'response-builder::builder.no_error_message',
105+
/** @scrutinizer ignore-deprecated */
103106
self::EX_HTTP_EXCEPTION() => 'response-builder::builder.http_exception',
107+
/** @scrutinizer ignore-deprecated */
104108
self::EX_UNCAUGHT_EXCEPTION() => 'response-builder::builder.uncaught_exception',
109+
/** @scrutinizer ignore-deprecated */
105110
self::EX_HTTP_NOT_FOUND() => sprintf($tpl, HttpResponse::HTTP_NOT_FOUND),
111+
/** @scrutinizer ignore-deprecated */
106112
self::EX_HTTP_SERVICE_UNAVAILABLE() => sprintf($tpl, HttpResponse::HTTP_SERVICE_UNAVAILABLE),
113+
/** @scrutinizer ignore-deprecated */
107114
self::EX_AUTHENTICATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNAUTHORIZED),
115+
/** @scrutinizer ignore-deprecated */
108116
self::EX_VALIDATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNPROCESSABLE_ENTITY),
109117
];
110118
}
@@ -113,6 +121,8 @@ protected static function getBaseMap(): array
113121
* Returns API code for internal code OK
114122
*
115123
* @return int valid API code in current range
124+
*
125+
* @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
116126
*/
117127
public static function OK(): int
118128
{
@@ -134,7 +144,7 @@ public static function NO_ERROR_MESSAGE(): int
134144
*
135145
* @return int valid API code in current range
136146
*
137-
* @deprecated Configure Exception Handler to use your own API code.
147+
* @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
138148
*/
139149
public static function EX_HTTP_NOT_FOUND(): int
140150
{
@@ -145,6 +155,8 @@ public static function EX_HTTP_NOT_FOUND(): int
145155
* Returns API code for internal code EX_HTTP_EXCEPTION
146156
*
147157
* @return int valid API code in current range
158+
*
159+
* @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
148160
*/
149161
public static function EX_HTTP_EXCEPTION(): int
150162
{
@@ -155,6 +167,8 @@ public static function EX_HTTP_EXCEPTION(): int
155167
* Returns API code for internal code EX_UNCAUGHT_EXCEPTION
156168
*
157169
* @return int valid API code in current range
170+
*
171+
* @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
158172
*/
159173
public static function EX_UNCAUGHT_EXCEPTION(): int
160174
{
@@ -166,7 +180,7 @@ public static function EX_UNCAUGHT_EXCEPTION(): int
166180
*
167181
* @return int valid API code in current range
168182
*
169-
* @deprecated Configure Exception Handler to use your own API code.
183+
* @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
170184
*/
171185
public static function EX_AUTHENTICATION_EXCEPTION(): int
172186
{
@@ -178,7 +192,7 @@ public static function EX_AUTHENTICATION_EXCEPTION(): int
178192
*
179193
* @return int valid API code in current range
180194
*
181-
* @deprecated Configure Exception Handler to use your own API code.
195+
* @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
182196
*/
183197
public static function EX_VALIDATION_EXCEPTION(): int
184198
{
@@ -190,7 +204,7 @@ public static function EX_VALIDATION_EXCEPTION(): int
190204
*
191205
* @return int valid API code in current range
192206
*
193-
* @deprecated Configure Exception Handler to use your own API code.
207+
* @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8.
194208
*/
195209
public static function EX_HTTP_SERVICE_UNAVAILABLE(): int
196210
{

src/Builder.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/ExceptionHandlerHelper.php

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,68 +41,68 @@ public static function render(/** @scrutinizer ignore-unused */ $request, Except
4141
$cfg = static::getExceptionHandlerConfig()['map'];
4242

4343
if ($ex instanceof HttpException) {
44-
// Check if we have any exception configuration for this particular Http status code.
44+
// Check if we have any exception configuration for this particular HTTP status code.
4545
// This confing entry is guaranted to exist (at least 'default'). Enforced by tests.
4646
$http_code = $ex->getStatusCode();
4747
$ex_cfg = $cfg[ HttpException::class ][ $http_code ] ?? null;
4848
$ex_cfg = $ex_cfg ?? $cfg[ HttpException::class ]['default'];
49-
$result = self::processException($ex, $ex_cfg, $http_code);
49+
$result = self::processException($ex, /** @scrutinizer ignore-type */ $ex_cfg, $http_code);
5050
} elseif ($ex instanceof ValidationException) {
5151
// This entry is guaranted to exist. Enforced by tests.
5252
$http_code = HttpResponse::HTTP_UNPROCESSABLE_ENTITY;
53-
$ex_cfg = $cfg[ HttpException::class ][ $http_code ];
54-
$result = self::processException($ex, $ex_cfg, $http_code);
53+
$result = self::processException($ex, $cfg[ HttpException::class ][ $http_code ], $http_code);
5554
}
5655

5756
if ($result === null) {
5857
// This entry is guaranted to exist. Enforced by tests.
59-
$http_code = HttpResponse::HTTP_INTERNAL_SERVER_ERROR;
60-
$ex_cfg = $cfg['default'];
61-
$result = self::processException($ex, $ex_cfg, $http_code);
58+
$result = self::processException($ex, $cfg['default'], HttpResponse::HTTP_INTERNAL_SERVER_ERROR);
6259
}
6360

6461
return $result;
6562
}
6663

67-
protected static function processException(\Exception $ex, array $ex_cfg, int $http_code)
64+
/**
65+
* Handles given exception and produces valid HTTP response object.
66+
*
67+
* @param \Exception $ex Exception to be handled.
68+
* @param array $ex_cfg ExceptionHandler's config excerpt related to $ex exception type.
69+
* @param int $fallback_http_code HTTP code to be assigned to produced $ex related response in
70+
* case configuration array lacks own `http_code` value.
71+
*
72+
* @return \Symfony\Component\HttpFoundation\Response
73+
*/
74+
protected static function processException(\Exception $ex, array $ex_cfg, int $fallback_http_code)
6875
{
6976
$api_code = $ex_cfg['api_code'];
70-
$http_code = $ex_cfg['http_code'] ?? $http_code;
77+
$http_code = $ex_cfg['http_code'] ?? $fallback_http_code;
7178
$msg_key = $ex_cfg['msg_key'] ?? null;
7279
$msg_enforce = $ex_cfg['msg_enforce'] ?? false;
7380

7481
// No message key, let's get exception message and if there's nothing useful, fallback to built-in one.
75-
$error_message = $ex->getMessage();
82+
$msg = $ex->getMessage();
7683
$placeholders = [
7784
'api_code' => $api_code,
78-
'message' => ($error_message !== '') ? $error_message : '???',
85+
'message' => ($msg !== '') ? $msg : '???',
7986
];
8087

8188
// shall we enforce error message?
8289
if ($msg_enforce) {
8390
// yes, please.
8491
if ($msg_key === null) {
8592
// there's no msg_key configured for this exact code, so let's obtain our default message
86-
$error_message = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) : Lang::get($msg_key, $placeholders);
93+
$msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders)
94+
: Lang::get($msg_key, $placeholders);
8795
}
8896
} else {
89-
// nothing enforced, handling pipeline: ex_message -> user_defined msg -> http_ex -> default
90-
if ($error_message === '') {
91-
$error_message = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) : Lang::get($msg_key, $placeholders);
97+
// nothing enforced, handling pipeline: ex_message -> user_defined_msg -> http_ex -> default
98+
if ($msg === '') {
99+
$msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders)
100+
: Lang::get($msg_key, $placeholders);
92101
}
93102
}
94103

95104
// Lets' try to build the error response with what we have now
96-
$result = static::error($ex, $api_code, $http_code, $error_message);
97-
98-
if ($result === null) {
99-
$ex_cfg = $cfg[ HttpException::class ][ $http_code ];
100-
$api_code = $ex_cfg['api_code'] ?? BaseApiCodes::EX_VALIDATION_EXCEPTION();
101-
$http_code = $ex_cfg['http_code'] ?? $http_code;
102-
$result = static::error($ex, $api_code, $http_code, $error_message);
103-
}
104-
105-
return $result;
105+
return static::error($ex, $api_code, $http_code, $msg);
106106
}
107107

108108
/**
@@ -124,7 +124,8 @@ protected static function getErrorMessageForException(\Exception $ex, int $http_
124124
} else {
125125
// Still got nothing? Fall back to built-in generic message for this type of exception.
126126
$key = BaseApiCodes::getCodeMessageKey(($ex instanceof HttpException)
127-
? BaseApiCodes::EX_HTTP_EXCEPTION() : BaseApiCodes::NO_ERROR_MESSAGE());
127+
? /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION()
128+
: /** @scrutinizer ignore-deprecated */ BaseApiCodes::NO_ERROR_MESSAGE());
128129
$error_message = Lang::get($key, $placeholders);
129130
}
130131

@@ -206,29 +207,33 @@ protected static function error(Exception $ex,
206207
->build();
207208
}
208209

209-
protected
210-
static function getExceptionHandlerDefaultConfig(): array
210+
/**
211+
* Returns default (built-in) exception handler config array.
212+
*
213+
* @return array
214+
*/
215+
protected static function getExceptionHandlerDefaultConfig(): array
211216
{
212217
return [
213218
'map' => [
214219
HttpException::class => [
215220
// used by unauthenticated() to obtain api and http code for the exception
216221
HttpResponse::HTTP_UNAUTHORIZED => [
217-
'api_code' => BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(),
222+
'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(),
218223
],
219224
// Required by ValidationException handler
220225
HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [
221-
'api_code' => BaseApiCodes::EX_VALIDATION_EXCEPTION(),
226+
'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_VALIDATION_EXCEPTION(),
222227
],
223228
// default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set.
224229
'default' => [
225-
'api_code' => BaseApiCodes::EX_HTTP_EXCEPTION(),
230+
'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION(),
226231
'http_code' => HttpResponse::HTTP_BAD_REQUEST,
227232
],
228233
],
229234
// default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set.
230235
'default' => [
231-
'api_code' => BaseApiCodes::EX_UNCAUGHT_EXCEPTION(),
236+
'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_UNCAUGHT_EXCEPTION(),
232237
'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR,
233238
],
234239
],
@@ -240,8 +245,7 @@ static function getExceptionHandlerDefaultConfig(): array
240245
*
241246
* @return array
242247
*/
243-
protected
244-
static function getExceptionHandlerConfig(): array
248+
protected static function getExceptionHandlerConfig(): array
245249
{
246250
return Util::mergeConfig(static::getExceptionHandlerDefaultConfig(),
247251
\Config::get(ResponseBuilder::CONF_KEY_EXCEPTION_HANDLER, []));

0 commit comments

Comments
 (0)