Skip to content

Commit a3f27d1

Browse files
committed
Improve variables naming, add missing docblocks
1 parent a89ab7b commit a3f27d1

File tree

5 files changed

+113
-33
lines changed

5 files changed

+113
-33
lines changed

src/Tools/ResponseResolver.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace Mpociot\ApiDoc\Tools;
44

55
use Illuminate\Routing\Route;
6-
use Illuminate\Http\JsonResponse;
76
use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseTagStrategy;
87
use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseCallStrategy;
98
use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseFileStrategy;
109
use Mpociot\ApiDoc\Tools\ResponseStrategies\TransformerTagsStrategy;
10+
use Symfony\Component\HttpFoundation\Response;
1111

1212
class ResponseResolver
1313
{
@@ -38,28 +38,24 @@ public function __construct(Route $route)
3838
* @param array $tags
3939
* @param array $routeProps
4040
*
41-
* @return array
41+
* @return array|null
4242
*/
4343
private function resolve(array $tags, array $routeProps)
4444
{
45-
$response = null;
46-
4745
foreach (static::$strategies as $strategy) {
4846
$strategy = new $strategy();
4947

50-
/** @var JsonResponse|array|null $response */
51-
$response = $strategy($this->route, $tags, $routeProps);
48+
/** @var Response[]|null $response */
49+
$responses = $strategy($this->route, $tags, $routeProps);
5250

53-
if (! is_null($response)) {
54-
if (is_array($response)) {
55-
return array_map(function (JsonResponse $response) {
56-
return ['status' => $response->getStatusCode(), 'content' => $this->getResponseContent($response)];
57-
}, $response);
58-
}
59-
60-
return [['status' => $response->getStatusCode(), 'content' => $this->getResponseContent($response)]];
51+
if (! is_null($responses)) {
52+
return array_map(function (Response $response) {
53+
return ['status' => $response->getStatusCode(), 'content' => $this->getResponseContent($response)];
54+
}, $responses);
6155
}
6256
}
57+
58+
return null;
6359
}
6460

6561
/**

src/Tools/ResponseStrategies/ResponseCallStrategy.php

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@
1212
*/
1313
class ResponseCallStrategy
1414
{
15+
/**
16+
* @param Route $route
17+
* @param array $tags
18+
* @param array $routeProps
19+
*
20+
* @return array|null
21+
*/
1522
public function __invoke(Route $route, array $tags, array $routeProps)
1623
{
1724
$rulesToApply = $routeProps['rules']['response_calls'] ?? [];
1825
if (! $this->shouldMakeApiCall($route, $rulesToApply)) {
19-
return;
26+
return null;
2027
}
2128

2229
$this->configureEnvironment($rulesToApply);
2330
$request = $this->prepareRequest($route, $rulesToApply, $routeProps['body'], $routeProps['query']);
31+
2432
try {
25-
$response = $this->makeApiCall($request);
33+
$response = [$this->makeApiCall($request)];
2634
} catch (\Exception $e) {
2735
$response = null;
2836
} finally {
@@ -32,12 +40,25 @@ public function __invoke(Route $route, array $tags, array $routeProps)
3240
return $response;
3341
}
3442

43+
/**
44+
* @param array $rulesToApply
45+
*
46+
* @return void
47+
*/
3548
private function configureEnvironment(array $rulesToApply)
3649
{
3750
$this->startDbTransaction();
3851
$this->setEnvironmentVariables($rulesToApply['env'] ?? []);
3952
}
4053

54+
/**
55+
* @param Route $route
56+
* @param array $rulesToApply
57+
* @param array $bodyParams
58+
* @param array $queryParams
59+
*
60+
* @return Request
61+
*/
4162
private function prepareRequest(Route $route, array $rulesToApply, array $bodyParams, array $queryParams)
4263
{
4364
$uri = $this->replaceUrlParameterBindings($route, $rulesToApply['bindings'] ?? []);
@@ -77,6 +98,11 @@ protected function replaceUrlParameterBindings(Route $route, $bindings)
7798
return $uri;
7899
}
79100

101+
/**
102+
* @param array $env
103+
*
104+
* @return void
105+
*/
80106
private function setEnvironmentVariables(array $env)
81107
{
82108
foreach ($env as $name => $value) {
@@ -87,6 +113,9 @@ private function setEnvironmentVariables(array $env)
87113
}
88114
}
89115

116+
/**
117+
* @return void
118+
*/
90119
private function startDbTransaction()
91120
{
92121
try {
@@ -95,6 +124,9 @@ private function startDbTransaction()
95124
}
96125
}
97126

127+
/**
128+
* @return void
129+
*/
98130
private function endDbTransaction()
99131
{
100132
try {
@@ -103,11 +135,19 @@ private function endDbTransaction()
103135
}
104136
}
105137

138+
/**
139+
* @return void
140+
*/
106141
private function finish()
107142
{
108143
$this->endDbTransaction();
109144
}
110145

146+
/**
147+
* @param Request $request
148+
*
149+
* @return \Illuminate\Http\JsonResponse|mixed
150+
*/
111151
public function callDingoRoute(Request $request)
112152
{
113153
/** @var Dispatcher $dispatcher */
@@ -138,11 +178,23 @@ public function callDingoRoute(Request $request)
138178
return $response;
139179
}
140180

181+
/**
182+
* @param Route $route
183+
*
184+
* @return array
185+
*/
141186
public function getMethods(Route $route)
142187
{
143188
return array_diff($route->methods(), ['HEAD']);
144189
}
145190

191+
/**
192+
* @param Request $request
193+
* @param Route $route
194+
* @param array|null $headers
195+
*
196+
* @return Request
197+
*/
146198
private function addHeaders(Request $request, Route $route, $headers)
147199
{
148200
// set the proper domain
@@ -162,6 +214,12 @@ private function addHeaders(Request $request, Route $route, $headers)
162214
return $request;
163215
}
164216

217+
/**
218+
* @param Request $request
219+
* @param array $query
220+
*
221+
* @return Request
222+
*/
165223
private function addQueryParameters(Request $request, array $query)
166224
{
167225
$request->query->add($query);
@@ -170,13 +228,25 @@ private function addQueryParameters(Request $request, array $query)
170228
return $request;
171229
}
172230

231+
/**
232+
* @param Request $request
233+
* @param array $body
234+
*
235+
* @return Request
236+
*/
173237
private function addBodyParameters(Request $request, array $body)
174238
{
175239
$request->request->add($body);
176240

177241
return $request;
178242
}
179243

244+
/**
245+
* @param Request $request
246+
*
247+
* @return \Illuminate\Http\JsonResponse|mixed|\Symfony\Component\HttpFoundation\Response
248+
* @throws \Exception
249+
*/
180250
private function makeApiCall(Request $request)
181251
{
182252
if (config('apidoc.router') == 'dingo') {
@@ -189,9 +259,10 @@ private function makeApiCall(Request $request)
189259
}
190260

191261
/**
192-
* @param $request
262+
* @param Request $request
193263
*
194264
* @return \Symfony\Component\HttpFoundation\Response
265+
* @throws \Exception
195266
*/
196267
private function callLaravelRoute(Request $request): \Symfony\Component\HttpFoundation\Response
197268
{
@@ -202,6 +273,12 @@ private function callLaravelRoute(Request $request): \Symfony\Component\HttpFoun
202273
return $response;
203274
}
204275

276+
/**
277+
* @param Route $route
278+
* @param array $rulesToApply
279+
*
280+
* @return bool
281+
*/
205282
private function shouldMakeApiCall(Route $route, array $rulesToApply): bool
206283
{
207284
$allowedMethods = $rulesToApply['methods'] ?? [];

src/Tools/ResponseStrategies/ResponseFileStrategy.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,31 @@ class ResponseFileStrategy
1616
* @param array $tags
1717
* @param array $routeProps
1818
*
19-
* @return mixed
19+
* @return array|null
2020
*/
2121
public function __invoke(Route $route, array $tags, array $routeProps)
2222
{
23-
return $this->getFileResponse($tags);
23+
return $this->getFileResponses($tags);
2424
}
2525

2626
/**
2727
* Get the response from the file if available.
2828
*
2929
* @param array $tags
3030
*
31-
* @return mixed
31+
* @return array|null
3232
*/
33-
protected function getFileResponse(array $tags)
33+
protected function getFileResponses(array $tags)
3434
{
3535
$responseFileTags = array_filter($tags, function ($tag) {
3636
return $tag instanceof Tag && strtolower($tag->getName()) === 'responsefile';
3737
});
3838

3939
if (empty($responseFileTags)) {
40-
return;
40+
return null;
4141
}
4242

43-
return array_map(function ($responseFileTag) {
43+
return array_map(function (Tag $responseFileTag) {
4444
preg_match('/^(\d{3})?\s?([\s\S]*)$/', $responseFileTag->getContent(), $result);
4545

4646
$status = $result[1] ?: 200;

src/Tools/ResponseStrategies/ResponseTagStrategy.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,31 @@ class ResponseTagStrategy
1616
* @param array $tags
1717
* @param array $routeProps
1818
*
19-
* @return mixed
19+
* @return array|null
2020
*/
2121
public function __invoke(Route $route, array $tags, array $routeProps)
2222
{
23-
return $this->getDocBlockResponse($tags);
23+
return $this->getDocBlockResponses($tags);
2424
}
2525

2626
/**
2727
* Get the response from the docblock if available.
2828
*
2929
* @param array $tags
3030
*
31-
* @return mixed
31+
* @return array|null
3232
*/
33-
protected function getDocBlockResponse(array $tags)
33+
protected function getDocBlockResponses(array $tags)
3434
{
3535
$responseTags = array_filter($tags, function ($tag) {
3636
return $tag instanceof Tag && strtolower($tag->getName()) === 'response';
3737
});
3838

3939
if (empty($responseTags)) {
40-
return;
40+
return null;
4141
}
4242

43-
return array_map(function ($responseTag) {
43+
return array_map(function (Tag $responseTag) {
4444
preg_match('/^(\d{3})?\s?([\s\S]*)$/', $responseTag->getContent(), $result);
4545

4646
$status = $result[1] ?: 200;

src/Tools/ResponseStrategies/TransformerTagsStrategy.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
*/
1616
class TransformerTagsStrategy
1717
{
18+
/**
19+
* @param Route $route
20+
* @param array $tags
21+
* @param array $routeProps
22+
*
23+
* @return array|null
24+
*/
1825
public function __invoke(Route $route, array $tags, array $routeProps)
1926
{
2027
return $this->getTransformerResponse($tags);
@@ -25,13 +32,13 @@ public function __invoke(Route $route, array $tags, array $routeProps)
2532
*
2633
* @param array $tags
2734
*
28-
* @return mixed
35+
* @return array|null
2936
*/
3037
protected function getTransformerResponse(array $tags)
3138
{
3239
try {
3340
if (empty($transformerTag = $this->getTransformerTag($tags))) {
34-
return;
41+
return null;
3542
}
3643

3744
$transformer = $this->getTransformerClass($transformerTag);
@@ -43,9 +50,9 @@ protected function getTransformerResponse(array $tags)
4350
? new Collection([$modelInstance, $modelInstance], new $transformer)
4451
: new Item($modelInstance, new $transformer);
4552

46-
return response($fractal->createData($resource)->toJson());
53+
return [response($fractal->createData($resource)->toJson())];
4754
} catch (\Exception $e) {
48-
return;
55+
return null;
4956
}
5057
}
5158

0 commit comments

Comments
 (0)