Skip to content

Commit

Permalink
chore: correct code structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Lapkovsky committed Dec 13, 2023
1 parent 1d84a44 commit ddcc184
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Http/Middleware/AutoDocMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function handle($request, Closure $next)
{
$response = $next($request);

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

Expand Down
83 changes: 45 additions & 38 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(Container $container)

$this->setDriver();

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

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

protected function saveResponseSchema(?array $content, int $code)
protected function saveResponseSchema(?array $content, string $definition): void
{
if (empty($content)) {
return;
}

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

if (array_is_list($content)) {
$schemaType = 'array';
$types = [];
$this->saveListResponseDefinitions($content, $schemaProperties);

foreach ($content as $value) {
$type = gettype($value);
if (!in_array($type, $types)) {
$types[] = $type;
$schemaProperties['items']['allOf'][]['type'] = $type;
}
}
$schemaType = 'array';
} else {
$properties = Arr::get(
$this->data['definitions'],
"{$this->method}{$action}{$code}ResponseObject.properties",
[]
);
$this->saveObjectResponseDefinitions($content, $schemaProperties, $definition);
}

foreach ($content as $name => $value) {
$property = Arr::get($properties, $name, []);
$this->data['definitions'][$definition] = [
'type' => $schemaType,
'properties' => $schemaProperties
];
}

if (is_null($value)) {
$property['nullable'] = true;
} else {
$property['type'] = gettype($value);
}
protected function saveListResponseDefinitions(array $content, array &$schemaProperties): void
{
$types = [];

foreach ($content as $value) {
$type = gettype($value);

$schemaProperties[$name] = $property;
if (!in_array($type, $types)) {
$types[] = $type;
$schemaProperties['items']['allOf'][]['type'] = $type;
}
}
}

$this->data['definitions']["{$this->method}{$action}{$code}ResponseObject"] = [
'type' => $schemaType,
'properties' => $schemaProperties
];
protected function saveObjectResponseDefinitions(array $content, array &$schemaProperties, string $definition): void
{
$properties = Arr::get($this->data['definitions'], $definition, []);

foreach ($content as $name => $value) {
$property = Arr::get($properties, $name, []);

if (is_null($value)) {
$property['nullable'] = true;
} else {
$property['type'] = gettype($value);
}

$schemaProperties[$name] = $property;
}
}

protected function parseResponse($response)
Expand Down Expand Up @@ -373,13 +380,13 @@ protected function parseResponse($response)
);
}

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

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

$this->item['responses'][$code]['schema']['$ref'] = $definition;
if (is_array($this->item['responses'][$code])) {
$this->item['responses'][$code]['schema']['$ref'] = "#/definitions/{$definition}";
}
}

Expand Down Expand Up @@ -480,7 +487,7 @@ protected function saveGetRequestParameters($rules, array $attributes, array $an
}

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

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

$bodyParamExisted = Arr::where($parameters, function ($value) {
return $value['name'] == 'body';
return $value['name'] === 'body';
});

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

if ($controller == 'Closure') {
if ($controller === 'Closure') {
return null;
}

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

foreach ($ret as &$match) {
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
$match = ($match === strtoupper($match)) ? strtolower($match) : lcfirst($match);
}

return implode('_', $ret);
Expand Down

0 comments on commit ddcc184

Please sign in to comment.