From 1d84a44a65c9693e3204536119de8ff5ab0433ab Mon Sep 17 00:00:00 2001 From: Konstantin Lapkovsky Date: Wed, 13 Dec 2023 19:21:20 +0400 Subject: [PATCH] Revert "style: correct code style." This reverts commit ecafd32c38586bdf751ea7a694be01f822ac4b41. --- src/AutoDocServiceProvider.php | 2 +- src/Drivers/BaseDriver.php | 2 +- src/Drivers/LocalDriver.php | 2 +- src/Drivers/RemoteDriver.php | 4 +- src/Drivers/StorageDriver.php | 5 +- src/Http/Controllers/AutoDocController.php | 13 +- src/Http/Middleware/AutoDocMiddleware.php | 2 +- src/Interfaces/SwaggerDriverInterface.php | 8 +- src/Services/SwaggerService.php | 265 +++++++++++---------- src/Tests/AutoDocTestCaseTrait.php | 2 +- src/Traits/GetDependenciesTrait.php | 8 +- src/Validators/SwaggerSpecValidator.php | 7 +- 12 files changed, 167 insertions(+), 153 deletions(-) diff --git a/src/AutoDocServiceProvider.php b/src/AutoDocServiceProvider.php index f985f9d0..ec4a2764 100644 --- a/src/AutoDocServiceProvider.php +++ b/src/AutoDocServiceProvider.php @@ -7,7 +7,7 @@ class AutoDocServiceProvider extends ServiceProvider { - public function boot(): void + public function boot() { $this->mergeConfigFrom(__DIR__ . '/../config/auto-doc.php', 'auto-doc'); diff --git a/src/Drivers/BaseDriver.php b/src/Drivers/BaseDriver.php index 7fd70a09..ddd18797 100644 --- a/src/Drivers/BaseDriver.php +++ b/src/Drivers/BaseDriver.php @@ -6,7 +6,7 @@ abstract class BaseDriver implements SwaggerDriverInterface { - protected string $tempFilePath; + protected $tempFilePath; public function __construct() { diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index 128adc80..c2801563 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -7,7 +7,7 @@ class LocalDriver extends BaseDriver { - protected string $prodFilePath; + protected $prodFilePath; public function __construct() { diff --git a/src/Drivers/RemoteDriver.php b/src/Drivers/RemoteDriver.php index ca92166d..c7605463 100755 --- a/src/Drivers/RemoteDriver.php +++ b/src/Drivers/RemoteDriver.php @@ -7,8 +7,8 @@ class RemoteDriver extends BaseDriver { - protected string $key; - protected string $remoteUrl; + protected $key; + protected $remoteUrl; public function __construct() { diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index 0166f2f6..b0b142f9 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -3,14 +3,13 @@ namespace RonasIT\Support\AutoDoc\Drivers; use Illuminate\Contracts\Filesystem\FileNotFoundException; -use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Support\Facades\Storage; use RonasIT\Support\AutoDoc\Exceptions\MissedProductionFilePathException; class StorageDriver extends BaseDriver { - protected Filesystem $disk; - protected string $prodFilePath; + protected $disk; + protected $prodFilePath; public function __construct() { diff --git a/src/Http/Controllers/AutoDocController.php b/src/Http/Controllers/AutoDocController.php index 33807873..6b23f483 100644 --- a/src/Http/Controllers/AutoDocController.php +++ b/src/Http/Controllers/AutoDocController.php @@ -2,18 +2,15 @@ namespace RonasIT\Support\AutoDoc\Http\Controllers; -use Illuminate\Contracts\View\View; -use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; -use Illuminate\Http\Response; use Illuminate\Routing\Controller as BaseController; use RonasIT\Support\AutoDoc\Services\SwaggerService; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class AutoDocController extends BaseController { - protected SwaggerService $service; - protected string $documentationViewer; + protected $service; + protected $documentationViewer; public function __construct() { @@ -21,14 +18,14 @@ public function __construct() $this->documentationViewer = config('auto-doc.documentation_viewer'); } - public function documentation(): JsonResponse + public function documentation() { $documentation = $this->service->getDocFileContent(); return response()->json($documentation); } - public function index(): View|Response + public function index() { $currentEnvironment = config('app.env'); @@ -39,7 +36,7 @@ public function index(): View|Response return response('Forbidden.', 403); } - public function getFile(Request $request, $file): Response + public function getFile(Request $request, $file) { $filePath = __DIR__ . "/../../../resources/assets/{$this->documentationViewer}/" . $file; diff --git a/src/Http/Middleware/AutoDocMiddleware.php b/src/Http/Middleware/AutoDocMiddleware.php index e0472113..a048448d 100644 --- a/src/Http/Middleware/AutoDocMiddleware.php +++ b/src/Http/Middleware/AutoDocMiddleware.php @@ -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); } diff --git a/src/Interfaces/SwaggerDriverInterface.php b/src/Interfaces/SwaggerDriverInterface.php index f0e18408..45ba0fc7 100644 --- a/src/Interfaces/SwaggerDriverInterface.php +++ b/src/Interfaces/SwaggerDriverInterface.php @@ -6,18 +6,20 @@ interface SwaggerDriverInterface { /** * Save temporary data + * + * @param array $data */ - public function saveTmpData(array $data): void; + public function saveTmpData($data); /** * Get temporary data */ - public function getTmpData(): void; + public function getTmpData(); /** * Save production data */ - public function saveData(): void; + public function saveData(); /** * Get production documentation diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index c7c669a3..f295b50b 100755 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -31,22 +31,22 @@ class SwaggerService public const SWAGGER_VERSION = '2.0'; + protected $driver; + protected $openAPIValidator; + + protected $data; + protected $config; + protected $container; + private $uri; + private $method; /** - * @var SwaggerDriverInterface|mixed + * @var Request */ - protected $driver; - protected SwaggerSpecValidator $openAPIValidator; - - protected ?array $data; - protected array $config; - protected Container $container; - private string $uri; - private string $method; - private Request $request; - private array $item; - private string $security; - - protected array $ruleToTypeMap = [ + private $request; + private $item; + private $security; + + protected $ruleToTypeMap = [ 'array' => 'object', 'boolean' => 'boolean', 'date' => 'date', @@ -57,7 +57,7 @@ class SwaggerService 'int' => 'integer' ]; - protected array $booleanAnnotations = [ + protected $booleanAnnotations = [ 'deprecated' ]; @@ -66,9 +66,10 @@ public function __construct(Container $container) $this->openAPIValidator = app(SwaggerSpecValidator::class); $this->initConfig(); + $this->setDriver(); - if (config('app.env') === 'testing') { + if (config('app.env') == 'testing') { $this->container = $container; $this->security = $this->config['security']; @@ -83,7 +84,7 @@ public function __construct(Container $container) } } - protected function initConfig(): void + protected function initConfig() { $this->config = config('auto-doc'); @@ -106,7 +107,7 @@ protected function initConfig(): void } } - protected function setDriver(): void + protected function setDriver() { $driver = $this->config['driver']; $className = Arr::get($this->config, "drivers.{$driver}.class"); @@ -167,35 +168,40 @@ protected function generateSecurityDefinition(): ?array ]; } - protected function generateSecurityDefinitionObject(string $type): array + protected function generateSecurityDefinitionObject($type): array { - return match ($type) { - 'jwt' => [ - 'type' => 'apiKey', - 'name' => 'authorization', - 'in' => 'header' - ], - 'laravel' => [ - 'type' => 'apiKey', - 'name' => 'Cookie', - 'in' => 'header' - ], - default => throw new WrongSecurityConfigException(), - }; + switch ($type) { + case 'jwt': + return [ + 'type' => 'apiKey', + 'name' => 'authorization', + 'in' => 'header' + ]; + + case 'laravel': + return [ + 'type' => 'apiKey', + 'name' => 'Cookie', + 'in' => 'header' + ]; + default: + throw new WrongSecurityConfigException(); + } } - public function addData(Request $request, $response): void + public function addData(Request $request, $response) { $this->request = $request; $this->prepareItem(); + $this->parseRequest(); $this->parseResponse($response); $this->driver->saveTmpData($this->data); } - protected function prepareItem(): void + protected function prepareItem() { $this->uri = "/{$this->getUri()}"; $this->method = strtolower($this->request->getMethod()); @@ -215,7 +221,7 @@ protected function prepareItem(): void $this->item = &$this->data['paths'][$this->uri][$this->method]; } - protected function getUri(): string + protected function getUri() { $uri = $this->request->route()->uri(); $basePath = preg_replace("/^\//", '', $this->config['basePath']); @@ -249,7 +255,7 @@ protected function getPathParams(): array return $result; } - protected function parseRequest(): void + protected function parseRequest() { $this->saveConsume(); $this->saveTags(); @@ -270,68 +276,62 @@ protected function parseRequest(): void $this->saveDescription($concreteRequest, $annotations); } - protected function markAsDeprecated(array $annotations): void + protected function markAsDeprecated(array $annotations) { $this->item['deprecated'] = Arr::get($annotations, 'deprecated', false); } - protected function saveResponseSchema(?array $content, string $definition): void + protected function saveResponseSchema(?array $content, int $code) { if (empty($content)) { return; } $schemaProperties = []; + $action = Str::ucfirst($this->getActionName($this->uri)); $schemaType = 'object'; if (array_is_list($content)) { - $this->saveListResponseDefinition($content, $schemaProperties); - $schemaType = 'array'; - } else { - $this->saveObjectResponseDefinitions($definition, $content, $schemaProperties); - } + $types = []; - $this->data['definitions'][$definition] = [ - 'type' => $schemaType, - 'properties' => $schemaProperties - ]; - } - - protected function saveObjectResponseDefinitions(string $definition, array $content, array &$schemaProperties): void - { - $properties = Arr::get($this->data['definitions'], "{$definition}.properties", []); - - foreach ($content as $name => $value) { - $property = Arr::get($properties, $name, []); - - if (is_null($value)) { - $property['nullable'] = true; - } else { - $property['type'] = gettype($value); + foreach ($content as $value) { + $type = gettype($value); + if (!in_array($type, $types)) { + $types[] = $type; + $schemaProperties['items']['allOf'][]['type'] = $type; + } } + } else { + $properties = Arr::get( + $this->data['definitions'], + "{$this->method}{$action}{$code}ResponseObject.properties", + [] + ); - $schemaProperties[$name] = $property; - } - } - - protected function saveListResponseDefinition(array $content, array &$schemaProperties): void - { - $types = []; + foreach ($content as $name => $value) { + $property = Arr::get($properties, $name, []); - foreach ($content as $value) { - $type = gettype($value); + if (is_null($value)) { + $property['nullable'] = true; + } else { + $property['type'] = gettype($value); + } - if (!in_array($type, $types)) { - $types[] = $type; - $schemaProperties['items']['allOf'][]['type'] = $type; + $schemaProperties[$name] = $property; } } + + $this->data['definitions']["{$this->method}{$action}{$code}ResponseObject"] = [ + 'type' => $schemaType, + 'properties' => $schemaProperties + ]; } - protected function parseResponse($response): void + protected function parseResponse($response) { $produceList = $this->data['paths'][$this->uri][$this->method]['produces']; + $produce = $response->headers->get('Content-type'); if (is_null($produce)) { @@ -343,18 +343,23 @@ protected function parseResponse($response): void } $responses = $this->item['responses']; + $responseExampleLimitCount = config('auto-doc.response_example_limit_count'); + $content = json_decode($response->getContent(), true); - if (!empty($responseExampleLimitCount) && !empty($content['data'])) { - $limitedResponseData = array_slice($content['data'], 0, $responseExampleLimitCount, true); - $content['data'] = $limitedResponseData; - $content['to'] = count($limitedResponseData); - $content['total'] = count($limitedResponseData); + if (!empty($responseExampleLimitCount)) { + if (!empty($content['data'])) { + $limitedResponseData = array_slice($content['data'], 0, $responseExampleLimitCount, true); + $content['data'] = $limitedResponseData; + $content['to'] = count($limitedResponseData); + $content['total'] = count($limitedResponseData); + } } if (!empty($content['exception'])) { $uselessKeys = array_keys(Arr::except($content, ['message'])); + $content = Arr::except($content, $uselessKeys); } @@ -368,25 +373,25 @@ protected function parseResponse($response): void ); } - $action = Str::ucfirst($this->getActionName($this->uri)); - $definition = "#/definitions/{$this->method}{$action}{$code}ResponseObject"; - - $this->saveResponseSchema($content, $definition); + $this->saveResponseSchema($content, $code); if (is_array($this->item['responses'][$code])) { + $action = Str::ucfirst($this->getActionName($this->uri)); + $definition = "#/definitions/{$this->method}{$action}{$code}ResponseObject"; + $this->item['responses'][$code]['schema']['$ref'] = $definition; } } - protected function saveExample(int $code, string $content, string $produce): void + protected function saveExample($code, $content, $produce) { $description = $this->getResponseDescription($code); - $explodedContentType = explode('/', $produce); $availableContentTypes = [ 'application', 'text', 'image', ]; + $explodedContentType = explode('/', $produce); if (in_array($explodedContentType[0], $availableContentTypes)) { $this->item['responses'][$code] = $this->makeResponseExample($content, $produce, $description); @@ -395,7 +400,7 @@ protected function saveExample(int $code, string $content, string $produce): voi } } - protected function makeResponseExample(string $content, string $mimeType, string $description = ''): array + protected function makeResponseExample($content, $mimeType, $description = ''): array { $responseExample = ['description' => $description]; @@ -410,13 +415,14 @@ protected function makeResponseExample(string $content, string $mimeType, string return $responseExample; } - protected function saveParameters($request, array $annotations): void + protected function saveParameters($request, array $annotations) { $formRequest = new $request(); $formRequest->setUserResolver($this->request->getUserResolver()); $formRequest->setRouteResolver($this->request->getRouteResolver()); $rules = method_exists($formRequest, 'rules') ? $this->prepareRules($formRequest->rules()) : []; $attributes = method_exists($formRequest, 'attributes') ? $formRequest->attributes() : []; + $actionName = $this->getActionName($this->uri); if (in_array($this->method, ['get', 'delete'])) { @@ -445,7 +451,7 @@ protected function prepareRules(array $rules): array return $preparedRules; } - protected function getRuleAsString(object|string $rule): string + protected function getRuleAsString($rule): string { if (is_object($rule)) { if (method_exists($rule, '__toString')) { @@ -453,6 +459,7 @@ protected function getRuleAsString(object|string $rule): string } $shortName = Str::afterLast(get_class($rule), '\\'); + $ruleName = preg_replace('/Rule$/', '', $shortName); return Str::snake($ruleName); @@ -461,10 +468,11 @@ protected function getRuleAsString(object|string $rule): string return $rule; } - protected function saveGetRequestParameters(array $rules, array $attributes, array $annotations): void + protected function saveGetRequestParameters($rules, array $attributes, array $annotations) { foreach ($rules as $parameter => $rule) { $validation = explode('|', $rule); + $description = Arr::get($annotations, $parameter); if (empty($description)) { @@ -472,7 +480,7 @@ protected function saveGetRequestParameters(array $rules, array $attributes, arr } $existedParameter = Arr::first($this->item['parameters'], function ($existedParameter) use ($parameter) { - return $existedParameter['name'] === $parameter; + return $existedParameter['name'] == $parameter; }); if (empty($existedParameter)) { @@ -482,7 +490,6 @@ protected function saveGetRequestParameters(array $rules, array $attributes, arr 'description' => $description, 'type' => $this->getParameterType($validation) ]; - if (in_array('required', $validation)) { $parameterDefinition['required'] = true; } @@ -492,12 +499,7 @@ protected function saveGetRequestParameters(array $rules, array $attributes, arr } } - protected function savePostRequestParameters( - string $actionName, - array $rules, - array $attributes, - array $annotations - ): void + protected function savePostRequestParameters($actionName, $rules, array $attributes, array $annotations) { if ($this->requestHasMoreProperties($actionName)) { if ($this->requestHasBody()) { @@ -516,7 +518,7 @@ protected function savePostRequestParameters( } } - protected function saveDefinitions(string $objectName, array $rules, array $attributes, array $annotations): void + protected function saveDefinitions($objectName, $rules, $attributes, array $annotations) { $data = [ 'type' => 'object', @@ -548,6 +550,7 @@ protected function getParameterType(array $validation): string { $validationRules = $this->ruleToTypeMap; $validationRules['email'] = 'string'; + $parameterType = 'string'; foreach ($validation as $item) { @@ -559,20 +562,14 @@ protected function getParameterType(array $validation): string return $parameterType; } - protected function saveParameterType(array &$data, string $parameter, string $parameterType): void + protected function saveParameterType(&$data, $parameter, $parameterType) { $data['properties'][$parameter] = [ 'type' => $parameterType ]; } - protected function saveParameterDescription( - array &$data, - string $parameter, - array $rulesArray, - array $attributes, - array $annotations - ): void + protected function saveParameterDescription(&$data, $parameter, array $rulesArray, array $attributes, array $annotations) { $description = Arr::get($annotations, $parameter); @@ -583,7 +580,7 @@ protected function saveParameterDescription( $data['properties'][$parameter]['description'] = $description; } - protected function requestHasMoreProperties(string $actionName): bool + protected function requestHasMoreProperties($actionName): bool { $requestParametersCount = count($this->request->all()); @@ -601,25 +598,28 @@ 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); } - public function getConcreteRequest(): mixed + public function getConcreteRequest() { $controller = $this->request->route()->getActionName(); - if ($controller === 'Closure') { + if ($controller == 'Closure') { return null; } $explodedController = explode('@', $controller); + $class = $explodedController[0]; $method = $explodedController[1]; + $instance = app($class); $route = $this->request->route(); + $parameters = $this->resolveClassMethodDependencies( $route->parametersWithoutNulls(), $instance, @@ -631,7 +631,7 @@ public function getConcreteRequest(): mixed }); } - public function saveConsume(): void + public function saveConsume() { $consumeList = $this->data['paths'][$this->uri][$this->method]['consumes']; $consume = $this->request->header('Content-Type'); @@ -641,16 +641,18 @@ public function saveConsume(): void } } - public function saveTags(): void + public function saveTags() { $tagIndex = 1; + $explodedUri = explode('/', $this->uri); + $tag = Arr::get($explodedUri, $tagIndex); $this->item['tags'] = [$tag]; } - public function saveDescription($request, array $annotations): void + public function saveDescription($request, array $annotations) { $this->item['summary'] = $this->getSummary($request, $annotations); @@ -661,14 +663,14 @@ public function saveDescription($request, array $annotations): void } } - protected function saveSecurity(): void + protected function saveSecurity() { if ($this->requestSupportAuth()) { $this->addSecurityToOperation(); } } - protected function addSecurityToOperation(): void + protected function addSecurityToOperation() { $security = &$this->data['paths'][$this->uri][$this->method]['security']; @@ -679,7 +681,7 @@ protected function addSecurityToOperation(): void } } - protected function getSummary($request, array $annotations): ?string + protected function getSummary($request, array $annotations) { $summary = Arr::get($annotations, 'summary'); @@ -704,19 +706,21 @@ protected function requestSupportAuth(): bool return !empty($header); } - protected function parseRequestName($request): string + protected function parseRequestName($request) { $explodedRequest = explode('\\', $request); $requestName = array_pop($explodedRequest); $summaryName = str_replace('Request', '', $requestName); + $underscoreRequestName = $this->camelCaseToUnderScore($summaryName); return preg_replace('/[_]/', ' ', $underscoreRequestName); } - protected function getResponseDescription(int $code): string + protected function getResponseDescription($code) { $defaultDescription = Response::$statusTexts[$code]; + $request = $this->getConcreteRequest(); if (empty($request)) { @@ -724,6 +728,7 @@ protected function getResponseDescription(int $code): string } $annotations = $this->getClassAnnotations($request); + $localDescription = Arr::get($annotations, "_{$code}"); if (!empty($localDescription)) { @@ -733,7 +738,7 @@ protected function getResponseDescription(int $code): string return Arr::get($this->config, "defaults.code-descriptions.{$code}", $defaultDescription); } - protected function getActionName(string $uri): string + protected function getActionName($uri): string { $action = preg_replace('[\/]', '', $uri); @@ -744,7 +749,7 @@ protected function getActionName(string $uri): string * @deprecated method is not in use * @codeCoverageIgnore */ - protected function saveTempData(): void + protected function saveTempData() { $exportFile = Arr::get($this->config, 'files.temporary'); $data = json_encode($this->data); @@ -752,12 +757,12 @@ protected function saveTempData(): void file_put_contents($exportFile, $data); } - public function saveProductionData(): void + public function saveProductionData() { $this->driver->saveData(); } - public function getDocFileContent(): array + public function getDocFileContent() { $documentation = $this->driver->getDocumentation(); @@ -786,7 +791,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); @@ -802,17 +807,19 @@ protected function generateExample($properties): array return $example; } - protected function replaceObjectValues(array $parameters): array + protected function replaceObjectValues($parameters): array { - $parameters = Arr::dot($parameters); - $returnParameters = []; $classNamesValues = [ File::class => '[uploaded_file]', ]; + $parameters = Arr::dot($parameters); + $returnParameters = []; + foreach ($parameters as $parameter => $value) { if (is_object($value)) { $class = get_class($value); + $value = Arr::get($classNamesValues, $class, $class); } @@ -825,8 +832,11 @@ protected function replaceObjectValues(array $parameters): array protected function getClassAnnotations($class): array { $reflection = new ReflectionClass($class); + $annotations = $reflection->getDocComment(); + $blocks = explode("\n", $annotations); + $result = []; foreach ($blocks as $block) { @@ -834,6 +844,7 @@ protected function getClassAnnotations($class): array $index = strpos($block, '@'); $block = substr($block, $index); $exploded = explode(' ', $block); + $paramName = str_replace('@', '', array_shift($exploded)); $paramValue = implode(' ', $exploded); @@ -853,7 +864,7 @@ protected function getClassAnnotations($class): array * this issue: https://github.com/OAI/OpenAPI-Specification/issues/229 * We hope swagger developers will resolve this problem in next release of Swagger OpenAPI * */ - protected function replaceNullValues(array $parameters, array $types, array &$example): void + protected function replaceNullValues($parameters, $types, &$example) { foreach ($parameters as $parameter => $value) { if (is_null($value) && array_key_exists($parameter, $types)) { @@ -866,7 +877,7 @@ protected function replaceNullValues(array $parameters, array $types, array &$ex } } - protected function getDefaultValueByType(string $type): mixed + protected function getDefaultValueByType($type) { $values = [ 'object' => 'null', diff --git a/src/Tests/AutoDocTestCaseTrait.php b/src/Tests/AutoDocTestCaseTrait.php index 317d8073..77d3e6b3 100644 --- a/src/Tests/AutoDocTestCaseTrait.php +++ b/src/Tests/AutoDocTestCaseTrait.php @@ -9,7 +9,7 @@ trait AutoDocTestCaseTrait /** * Disabling documentation collecting on current test */ - public function skipDocumentationCollecting(): void + public function skipDocumentationCollecting() { AutoDocMiddleware::$skipped = true; } diff --git a/src/Traits/GetDependenciesTrait.php b/src/Traits/GetDependenciesTrait.php index 884c4c6b..8a049781 100644 --- a/src/Traits/GetDependenciesTrait.php +++ b/src/Traits/GetDependenciesTrait.php @@ -11,7 +11,7 @@ trait GetDependenciesTrait { - protected function resolveClassMethodDependencies(array $parameters, object $instance, string $method): array + protected function resolveClassMethodDependencies(array $parameters, $instance, $method) { if (!method_exists($instance, $method)) { return $parameters; @@ -22,14 +22,14 @@ protected function resolveClassMethodDependencies(array $parameters, object $ins ); } - public function getDependencies(ReflectionFunctionAbstract $reflector): array + public function getDependencies(ReflectionFunctionAbstract $reflector) { return array_map(function ($parameter) { return $this->transformDependency($parameter); }, $reflector->getParameters()); } - protected function transformDependency(ReflectionParameter $parameter): ?string + protected function transformDependency(ReflectionParameter $parameter) { $class = $parameter->getClass(); @@ -40,7 +40,7 @@ protected function transformDependency(ReflectionParameter $parameter): ?string return interface_exists($class->name) ? $this->getClassByInterface($class->name) : $class->name; } - protected function getClassByInterface(string $interfaceName): ?string + protected function getClassByInterface($interfaceName) { $bindings = Container::getInstance()->getBindings(); diff --git a/src/Validators/SwaggerSpecValidator.php b/src/Validators/SwaggerSpecValidator.php index f1e22108..968a4193 100644 --- a/src/Validators/SwaggerSpecValidator.php +++ b/src/Validators/SwaggerSpecValidator.php @@ -20,6 +20,9 @@ use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingRefFileException; use RonasIT\Support\AutoDoc\Services\SwaggerService; +/** + * @property array $doc + */ class SwaggerSpecValidator { public const SCHEMA_TYPES = [ @@ -74,7 +77,7 @@ class SwaggerSpecValidator public const MIME_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data'; public const MIME_TYPE_APPLICATION_URLENCODED = 'application/x-www-form-urlencoded'; - protected array $doc; + protected $doc; public function validate(array $doc): void { @@ -122,6 +125,7 @@ protected function validatePaths(): void $this->validateFieldsPresent(self::REQUIRED_FIELDS['operation'], $operationId); $this->validateFieldValue("{$operationId}.schemes", self::ALLOWED_VALUES['schemes']); + $this->validateParameters($operation, $path, $operationId); foreach ($operation['responses'] as $statusCode => $response) { @@ -150,6 +154,7 @@ protected function validateSecurityDefinitions(): void $parentId = "securityDefinitions.{$index}"; $this->validateFieldsPresent(self::REQUIRED_FIELDS['security_definition'], $parentId); + $this->validateFieldValue("{$parentId}.type", self::ALLOWED_VALUES['security_definition_type']); $this->validateFieldValue("{$parentId}.in", self::ALLOWED_VALUES['security_definition_in']); $this->validateFieldValue("{$parentId}.flow", self::ALLOWED_VALUES['security_definition_flow']);