Skip to content

Commit

Permalink
Apply fixes from StyleCI (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
hotmeteor authored Jan 17, 2024
1 parent 6ade96f commit 8ee0c63
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 173 deletions.
30 changes: 15 additions & 15 deletions _ide_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
/**
* @see \Spectator\Assertions
*
* @method static assertValidRequest()
* @method static assertInvalidRequest()
* @method static assertValidResponse($status = null)
* @method static assertInvalidResponse($status = null)
* @method static assertValidationMessage($expected)
* @method static assertErrorsContain($errors)
* @method static dumpSpecErrors()
* @method static void skipRequestValidation()
* @method static assertValidRequest()
* @method static assertInvalidRequest()
* @method static assertValidResponse($status = null)
* @method static assertInvalidResponse($status = null)
* @method static assertValidationMessage($expected)
* @method static assertErrorsContain($errors)
* @method static dumpSpecErrors()
* @method static void skipRequestValidation()
*/
class TestResponse
{
Expand All @@ -27,13 +27,13 @@ class TestResponse
/**
* @see \Spectator\Assertions
*
* @method static assertValidRequest()
* @method static assertInvalidRequest()
* @method static assertValidResponse($status = null)
* @method static assertInvalidResponse($status = null)
* @method static assertValidationMessage($expected)
* @method static assertErrorsContain($errors)
* @method static void skipRequestValidation()
* @method static assertValidRequest()
* @method static assertInvalidRequest()
* @method static assertValidResponse($status = null)
* @method static assertInvalidResponse($status = null)
* @method static assertValidationMessage($expected)
* @method static assertErrorsContain($errors)
* @method static void skipRequestValidation()
*/
class TestResponse
{
Expand Down
12 changes: 6 additions & 6 deletions config/spectator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@

'sources' => [
'local' => [
'source' => 'local',
'source' => 'local',
'base_path' => env('SPEC_PATH'),
],

'remote' => [
'source' => 'remote',
'source' => 'remote',
'base_path' => env('SPEC_PATH'),
'params' => env('SPEC_URL_PARAMS', ''),
'params' => env('SPEC_URL_PARAMS', ''),
],

'github' => [
'source' => 'github',
'source' => 'github',
'base_path' => env('SPEC_GITHUB_PATH'),
'repo' => env('SPEC_GITHUB_REPO'),
'token' => env('SPEC_GITHUB_TOKEN'),
'repo' => env('SPEC_GITHUB_REPO'),
'token' => env('SPEC_GITHUB_TOKEN'),
],
],

Expand Down
2 changes: 1 addition & 1 deletion src/Assertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function assertErrorsContain()
return fn ($errors) => $this->runAssertion(function () use ($errors) {
$matches = 0;

if (! is_array($errors)) {
if (!is_array($errors)) {
$errors = [$errors];
}

Expand Down
37 changes: 19 additions & 18 deletions src/Exceptions/SchemaValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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'];
Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down
10 changes: 5 additions & 5 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 4 additions & 4 deletions src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down Expand Up @@ -211,7 +211,7 @@ protected function standardizeFileName($file): string
*/
protected function standardizePath($path): string
{
if (! Str::endsWith($path, '/')) {
if (!Str::endsWith($path, '/')) {
$path = $path.'/';
}

Expand Down
28 changes: 14 additions & 14 deletions src/Spectator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static void using($name) Set the spec file to use.
* @method static void reset() Reset the name of the spec.
* @method static string getSpec() Get the spec being used.
* @method static string getPathPrefix() Get the path prefix being used.
* @method static RequestFactory setPathPrefix($prefix) Set the path prefix being used.
* @method static object resolve() Resolve the spec into an object.
* @method bool shouldValidateRequest() Indicate if request should be validated.
* @method static void using($name) Set the spec file to use.
* @method static void reset() Reset the name of the spec.
* @method static string getSpec() Get the spec being used.
* @method static string getPathPrefix() Get the path prefix being used.
* @method static RequestFactory setPathPrefix($prefix) Set the path prefix being used.
* @method static object resolve() Resolve the spec into an object.
* @method bool shouldValidateRequest() Indicate if request should be validated.
*
* @see \Spectator\RequestFactory
*/
class Spectator extends Facade
{
/**
* @method static void using($name) Set the spec file to use.
* @method static void reset() Reset the name of the spec.
* @method static string getSpec() Get the spec being used.
* @method static string getPathPrefix() Get the path prefix being used.
* @method static RequestFactory setPathPrefix($prefix) Set the path prefix being used.
* @method static object resolve() Resolve the spec into an object.
* @method bool shouldValidateRequest() Indicate if request should be validated.
* @method static void using($name) Set the spec file to use.
* @method static void reset() Reset the name of the spec.
* @method static string getSpec() Get the spec being used.
* @method static string getPathPrefix() Get the path prefix being used.
* @method static RequestFactory setPathPrefix($prefix) Set the path prefix being used.
* @method static object resolve() Resolve the spec into an object.
* @method bool shouldValidateRequest() Indicate if request should be validated.
*
* @see \Spectator\RequestFactory
*/
Expand Down
12 changes: 7 additions & 5 deletions src/Validation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
},
);
}
Expand Down Expand Up @@ -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;
}

Expand Down
15 changes: 8 additions & 7 deletions src/Validation/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}].");
}
}
Expand Down Expand Up @@ -133,7 +133,8 @@ protected function validateParameters()
}

/**
* @param mixed $parameter
* @param mixed $parameter
*
* @return mixed
*/
private function castParameter($parameter, ?string $type)
Expand Down Expand Up @@ -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.');
}

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Validation/ResponseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -149,9 +149,9 @@ protected function schemaType(Schema $schema)
}

/**
* @return mixed
*
* @throws ResponseValidationException
*
* @return mixed
*/
protected function body($contentType, $schemaType)
{
Expand Down
Loading

0 comments on commit 8ee0c63

Please sign in to comment.