Skip to content

Commit ddcc184

Browse files
author
Konstantin Lapkovsky
committed
chore: correct code structure.
1 parent 1d84a44 commit ddcc184

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

src/Http/Middleware/AutoDocMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function handle($request, Closure $next)
1616
{
1717
$response = $next($request);
1818

19-
if ((config('app.env') == 'testing') && !self::$skipped && !empty($request->route())) {
19+
if ((config('app.env') === 'testing') && !self::$skipped && !empty($request->route())) {
2020
app(SwaggerService::class)->addData($request, $response);
2121
}
2222

src/Services/SwaggerService.php

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct(Container $container)
6969

7070
$this->setDriver();
7171

72-
if (config('app.env') == 'testing') {
72+
if (config('app.env') === 'testing') {
7373
$this->container = $container;
7474

7575
$this->security = $this->config['security'];
@@ -281,51 +281,58 @@ protected function markAsDeprecated(array $annotations)
281281
$this->item['deprecated'] = Arr::get($annotations, 'deprecated', false);
282282
}
283283

284-
protected function saveResponseSchema(?array $content, int $code)
284+
protected function saveResponseSchema(?array $content, string $definition): void
285285
{
286286
if (empty($content)) {
287287
return;
288288
}
289289

290290
$schemaProperties = [];
291-
$action = Str::ucfirst($this->getActionName($this->uri));
292291
$schemaType = 'object';
293292

294293
if (array_is_list($content)) {
295-
$schemaType = 'array';
296-
$types = [];
294+
$this->saveListResponseDefinitions($content, $schemaProperties);
297295

298-
foreach ($content as $value) {
299-
$type = gettype($value);
300-
if (!in_array($type, $types)) {
301-
$types[] = $type;
302-
$schemaProperties['items']['allOf'][]['type'] = $type;
303-
}
304-
}
296+
$schemaType = 'array';
305297
} else {
306-
$properties = Arr::get(
307-
$this->data['definitions'],
308-
"{$this->method}{$action}{$code}ResponseObject.properties",
309-
[]
310-
);
298+
$this->saveObjectResponseDefinitions($content, $schemaProperties, $definition);
299+
}
311300

312-
foreach ($content as $name => $value) {
313-
$property = Arr::get($properties, $name, []);
301+
$this->data['definitions'][$definition] = [
302+
'type' => $schemaType,
303+
'properties' => $schemaProperties
304+
];
305+
}
314306

315-
if (is_null($value)) {
316-
$property['nullable'] = true;
317-
} else {
318-
$property['type'] = gettype($value);
319-
}
307+
protected function saveListResponseDefinitions(array $content, array &$schemaProperties): void
308+
{
309+
$types = [];
310+
311+
foreach ($content as $value) {
312+
$type = gettype($value);
320313

321-
$schemaProperties[$name] = $property;
314+
if (!in_array($type, $types)) {
315+
$types[] = $type;
316+
$schemaProperties['items']['allOf'][]['type'] = $type;
322317
}
323318
}
319+
}
324320

325-
$this->data['definitions']["{$this->method}{$action}{$code}ResponseObject"] = [
326-
'type' => $schemaType,
327-
'properties' => $schemaProperties
328-
];
321+
protected function saveObjectResponseDefinitions(array $content, array &$schemaProperties, string $definition): void
322+
{
323+
$properties = Arr::get($this->data['definitions'], $definition, []);
324+
325+
foreach ($content as $name => $value) {
326+
$property = Arr::get($properties, $name, []);
327+
328+
if (is_null($value)) {
329+
$property['nullable'] = true;
330+
} else {
331+
$property['type'] = gettype($value);
332+
}
333+
334+
$schemaProperties[$name] = $property;
335+
}
329336
}
330337

331338
protected function parseResponse($response)
@@ -373,13 +380,13 @@ protected function parseResponse($response)
373380
);
374381
}
375382

376-
$this->saveResponseSchema($content, $code);
383+
$action = Str::ucfirst($this->getActionName($this->uri));
384+
$definition = "{$this->method}{$action}{$code}ResponseObject";
377385

378-
if (is_array($this->item['responses'][$code])) {
379-
$action = Str::ucfirst($this->getActionName($this->uri));
380-
$definition = "#/definitions/{$this->method}{$action}{$code}ResponseObject";
386+
$this->saveResponseSchema($content, $definition);
381387

382-
$this->item['responses'][$code]['schema']['$ref'] = $definition;
388+
if (is_array($this->item['responses'][$code])) {
389+
$this->item['responses'][$code]['schema']['$ref'] = "#/definitions/{$definition}";
383390
}
384391
}
385392

@@ -480,7 +487,7 @@ protected function saveGetRequestParameters($rules, array $attributes, array $an
480487
}
481488

482489
$existedParameter = Arr::first($this->item['parameters'], function ($existedParameter) use ($parameter) {
483-
return $existedParameter['name'] == $parameter;
490+
return $existedParameter['name'] === $parameter;
484491
});
485492

486493
if (empty($existedParameter)) {
@@ -598,7 +605,7 @@ protected function requestHasBody(): bool
598605
$parameters = $this->data['paths'][$this->uri][$this->method]['parameters'];
599606

600607
$bodyParamExisted = Arr::where($parameters, function ($value) {
601-
return $value['name'] == 'body';
608+
return $value['name'] === 'body';
602609
});
603610

604611
return empty($bodyParamExisted);
@@ -608,7 +615,7 @@ public function getConcreteRequest()
608615
{
609616
$controller = $this->request->route()->getActionName();
610617

611-
if ($controller == 'Closure') {
618+
if ($controller === 'Closure') {
612619
return null;
613620
}
614621

@@ -791,7 +798,7 @@ protected function camelCaseToUnderScore($input): string
791798
$ret = $matches[0];
792799

793800
foreach ($ret as &$match) {
794-
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
801+
$match = ($match === strtoupper($match)) ? strtolower($match) : lcfirst($match);
795802
}
796803

797804
return implode('_', $ret);

0 commit comments

Comments
 (0)