diff --git a/src/Exceptions/SchemaValidationException.php b/src/Exceptions/SchemaValidationException.php index 3d5ff76..5e4c1e5 100644 --- a/src/Exceptions/SchemaValidationException.php +++ b/src/Exceptions/SchemaValidationException.php @@ -52,8 +52,8 @@ public function hasErrors(): bool * Given a schema and ValidationError, returns a helpful error message where the validation * errors are mapped over a structured schema representation. * - * @param array|object $schema A JSON schema. - * @param ValidationError $validation_error A validation error generated by Opis\JsonSchema. + * @param array|object $schema A JSON schema. + * @param ValidationError $validation_error A validation error generated by Opis\JsonSchema. */ public static function validationErrorMessage($schema, ValidationError $validation_error): string { @@ -89,7 +89,7 @@ public static function validationErrorMessage($schema, ValidationError $validati // error alongside the item. $strings = []; - if (! is_null($schema_formatted)) { + if (!is_null($schema_formatted)) { foreach ($schema_formatted as $key => $schema_item) { if (isset($error_location_map[$key])) { $schema_item = self::colorize($schema_item, Format::TEXT_LIGHT_GREY); @@ -112,8 +112,9 @@ public static function validationErrorMessage($schema, ValidationError $validati /** * Returns a formatted validation error. * - * @param ValidationError $validation_error A validation error generated by Opis\JsonSchema. - * @param bool $flat Should the formatted error be flat (a simple array) or structured? + * @param ValidationError $validation_error A validation error generated by Opis\JsonSchema. + * @param bool $flat Should the formatted error be flat (a simple array) or structured? + * * @return array|string */ public static function formatValidationError(ValidationError $validation_error, bool $flat = false) @@ -151,11 +152,11 @@ public static function formatValidationError(ValidationError $validation_error, * status*: string * message*: string <== The data (integer) must match the type: string * - * @param array $schema JSON schema represented as an array. - * @param string $location_current The current location within the JSON schema structure. - * @param string $key_current The key at the current location, if one is present. - * @param array $keys_required The keys required at the current location, if provided. - * @param int $indent_level Represents how much newly added values should be indented. + * @param array $schema JSON schema represented as an array. + * @param string $location_current The current location within the JSON schema structure. + * @param string $key_current The key at the current location, if one is present. + * @param array $keys_required The keys required at the current location, if provided. + * @param int $indent_level Represents how much newly added values should be indented. */ public static function formatSchema(array $schema, string $location_current, string $key_current, array $keys_required, int $indent_level): array { @@ -168,7 +169,7 @@ public static function formatSchema(array $schema, string $location_current, str }, ARRAY_FILTER_USE_KEY); $polymorphic_keys = array_values(array_flip($polymorphic_keys)); - if (! empty($polymorphic_keys)) { // first, check for a polymorphic schema... + if (!empty($polymorphic_keys)) { // first, check for a polymorphic schema... $polymorphic_key = $polymorphic_keys[0]; // create entry for polymorphic schema @@ -185,7 +186,7 @@ public static function formatSchema(array $schema, string $location_current, str } elseif (isset($schema['type'])) { // otherwise, check for explicit schema type... // convert "type" to an array (to support single/multiple types) $types = []; - if (! is_array($schema['type'])) { + if (!is_array($schema['type'])) { $types = [$schema['type']]; } else { $types = $schema['type']; @@ -282,10 +283,10 @@ public static function formatSchema(array $schema, string $location_current, str * Applies modifiers to schema's key name and value, if provided. Then returns display string * for the schema. * - * @param string $type The schema's type (ex: allOf, object, string). - * @param string $type_modifier A modifier for the type (ex: "++" in "object++"). - * @param string $key The schema's key (ex: "name" in "name: string"). - * @param string $key_modifier A modifier for the key (ex: "?" in "string?"). + * @param string $type The schema's type (ex: allOf, object, string). + * @param string $type_modifier A modifier for the type (ex: "++" in "object++"). + * @param string $key The schema's key (ex: "name" in "name: string"). + * @param string $key_modifier A modifier for the key (ex: "?" in "string?"). */ protected static function schemaItemDisplayString(string $type, string $type_modifier = '', string $key = '', string $key_modifier = ''): string { @@ -298,8 +299,8 @@ protected static function schemaItemDisplayString(string $type, string $type_mod /** * Applies indentation to a display string and returns it. * - * @param string $display_string A string to display. - * @param int $indent_level The level of indentation to apply to the display string. + * @param string $display_string A string to display. + * @param int $indent_level The level of indentation to apply to the display string. */ protected static function indentedDisplayString(string $display_string, int $indent_level = 0): string { diff --git a/src/Middleware.php b/src/Middleware.php index 3483f62..2dbffa7 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -38,16 +38,16 @@ public function __construct(RequestFactory $spectator, ExceptionHandler $excepti } /** - * @return JsonResponse|Request - * * @throws InvalidPathException * @throws MissingSpecException * @throws RequestValidationException * @throws \Throwable + * + * @return JsonResponse|Request */ public function handle(Request $request, Closure $next) { - if (! $this->spectator->getSpec()) { + if (!$this->spectator->getSpec()) { return $next($request); } @@ -79,7 +79,7 @@ protected function formatResponse($exception, $code): JsonResponse return Response::json(array_merge([ 'exception' => get_class($exception), - 'message' => $exception->getMessage(), + 'message' => $exception->getMessage(), ], $errors), $code); } @@ -125,7 +125,7 @@ protected function validate(Request $request, Closure $next, string $requestPath */ protected function pathItem($requestPath, $requestMethod): PathItem { - if (! Str::startsWith($requestPath, '/')) { + if (!Str::startsWith($requestPath, '/')) { $requestPath = '/'.$requestPath; } diff --git a/src/RequestFactory.php b/src/RequestFactory.php index ccedd38..25dfa79 100644 --- a/src/RequestFactory.php +++ b/src/RequestFactory.php @@ -130,13 +130,13 @@ public function captureResponseValidation(Throwable $throwable) /** * Retrieve the spec file. * - * @return mixed - * * @throws MissingSpecException + * + * @return mixed */ protected function getFile() { - if (! $source = Arr::get(config('spectator.sources', []), config('spectator.default'))) { + if (!$source = Arr::get(config('spectator.sources', []), config('spectator.default'))) { throw new MissingSpecException('Cannot resolve schema with missing or invalid spec.'); } @@ -211,7 +211,7 @@ protected function standardizeFileName($file): string */ protected function standardizePath($path): string { - if (! Str::endsWith($path, '/')) { + if (!Str::endsWith($path, '/')) { $path = $path.'/'; } diff --git a/src/Validation/AbstractValidator.php b/src/Validation/AbstractValidator.php index 3115d58..fbde32d 100644 --- a/src/Validation/AbstractValidator.php +++ b/src/Validation/AbstractValidator.php @@ -13,14 +13,15 @@ abstract class AbstractValidator /** * Check if properties exist, and if so, prepare them based on version. * - * @param string|null $mode Access mode 'read' or 'write' + * @param string|null $mode Access mode 'read' or 'write' + * * @return mixed */ protected function prepareData(Schema $schema, ?string $mode = null) { $data = $schema->getSerializableData(); - if (! isset($data->properties)) { + if (!isset($data->properties)) { return $data; } @@ -40,7 +41,8 @@ protected function prepareData(Schema $schema, ?string $mode = null) /** * Filters out readonly|writeonly properties. * - * @param string|null $type Access mode 'read' or 'write' + * @param string|null $type Access mode 'read' or 'write' + * * @return mixed */ protected function filterProperties(object $data, ?string $mode = null): object @@ -87,7 +89,7 @@ function ($property) use ($filter_by) { $data->required = array_filter( $data->required, function ($property) use ($filter_properties) { - return ! in_array($property, $filter_properties); + return !in_array($property, $filter_properties); }, ); } @@ -174,7 +176,7 @@ protected function wrapAttributesToArray($properties) } // Before we check recursive cases, make sure this object defines a "type". - if (! isset($attributes->type)) { + if (!isset($attributes->type)) { break; } diff --git a/src/Validation/RequestValidator.php b/src/Validation/RequestValidator.php index 8eb6dfe..6a31fd4 100644 --- a/src/Validation/RequestValidator.php +++ b/src/Validation/RequestValidator.php @@ -71,13 +71,13 @@ protected function validateParameters() foreach ($required_parameters as $parameter) { // Verify presence, if required. - if ($parameter->in === 'path' && ! $route->hasParameter($parameter->name)) { + if ($parameter->in === 'path' && !$route->hasParameter($parameter->name)) { throw new RequestValidationException("Missing required parameter {$parameter->name} in URL path."); - } elseif ($parameter->in === 'query' && ! $this->hasQueryParam($parameter->name)) { + } elseif ($parameter->in === 'query' && !$this->hasQueryParam($parameter->name)) { throw new RequestValidationException("Missing required query parameter [?{$parameter->name}=]."); - } elseif ($parameter->in === 'header' && ! $this->request->headers->has($parameter->name)) { + } elseif ($parameter->in === 'header' && !$this->request->headers->has($parameter->name)) { throw new RequestValidationException("Missing required header [{$parameter->name}]."); - } elseif ($parameter->in === 'cookie' && ! $this->request->cookies->has($parameter->name)) { + } elseif ($parameter->in === 'cookie' && !$this->request->cookies->has($parameter->name)) { throw new RequestValidationException("Missing required cookie [{$parameter->name}]."); } } @@ -133,7 +133,8 @@ protected function validateParameters() } /** - * @param mixed $parameter + * @param mixed $parameter + * * @return mixed */ private function castParameter($parameter, ?string $type) @@ -170,7 +171,7 @@ protected function validateBody(): void // Content types should match. $content_type = $this->request->header('Content-Type'); - if (! array_key_exists($content_type, $expected_body->content)) { + if (!array_key_exists($content_type, $expected_body->content)) { throw new RequestValidationException('Request did not match any specified media type for request body.'); } @@ -218,7 +219,7 @@ protected function parseBodySchema(): object private function toObject($data) { - if (! is_array($data)) { + if (!is_array($data)) { return $data; } elseif (Arr::isAssoc($data)) { return (object) array_map([$this, 'toObject'], $data); diff --git a/src/Validation/ResponseValidator.php b/src/Validation/ResponseValidator.php index 21c47b1..ae7e302 100644 --- a/src/Validation/ResponseValidator.php +++ b/src/Validation/ResponseValidator.php @@ -63,7 +63,7 @@ protected function parseResponse(Response $response) )); // Does the response match any of the specified media types? - if (! in_array($contentType, $specTypes)) { + if (!in_array($contentType, $specTypes)) { $message = 'Response did not match any specified content type.'; $message .= PHP_EOL.PHP_EOL.' Expected: '.$specTypes[0]; $message .= PHP_EOL.' Actual: '.$contentType; @@ -149,9 +149,9 @@ protected function schemaType(Schema $schema) } /** - * @return mixed - * * @throws ResponseValidationException + * + * @return mixed */ protected function body($contentType, $schemaType) {