Skip to content

Commit

Permalink
style: fix code-style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodmain committed Sep 25, 2023
1 parent e00022f commit 1d8a9cb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 42 deletions.
4 changes: 3 additions & 1 deletion src/Exceptions/SpecValidation/InvalidFieldValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public function __construct(string $fieldName, array $allowedValues, array $inva
$allowedValuesString = implode(', ', $allowedValues);
$invalidValuesString = implode(', ', $invalidValues);

parent::__construct("Field '{$fieldName}' has an invalid value: {$invalidValuesString}. Allowed values: {$allowedValuesString}.");
parent::__construct(
"Field '{$fieldName}' has an invalid value: {$invalidValuesString}. Allowed values: {$allowedValuesString}."
);
}
}
5 changes: 4 additions & 1 deletion src/Exceptions/SpecValidation/InvalidStatusCodeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class InvalidStatusCodeException extends InvalidSwaggerSpecException
{
public function __construct(string $responseId)
{
parent::__construct("Operation at '{$responseId}' should only have three-digit status codes, `default`, and vendor extensions (`x-*`) as properties.");
parent::__construct(
"Operation at '{$responseId}' should only have three-digit status codes, `default`, "
. "and vendor extensions (`x-*`) as properties."
);
}
}
31 changes: 23 additions & 8 deletions src/Validators/SwaggerSpecValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ protected function validateResponse(array $response, string $statusCode, string
}

if (!empty($response['schema'])) {
$this->validateType($response['schema'], array_merge(self::SCHEMA_TYPES, ['file']), "{$responseId}.schema");
$this->validateType(
$response['schema'], array_merge(self::SCHEMA_TYPES, ['file']), "{$responseId}.schema"
);
}

if (!empty($response['items'])) {
Expand All @@ -195,7 +197,9 @@ protected function validateParameters(array $operation, string $path, string $op
$this->validateFieldsPresent(self::REQUIRED_FIELDS['parameter'], $paramId);

$this->validateFieldValue("{$paramId}.in", self::ALLOWED_VALUES['parameter_in']);
$this->validateFieldValue("{$paramId}.collectionFormat", self::ALLOWED_VALUES['parameter_collection_format']);
$this->validateFieldValue(
"{$paramId}.collectionFormat", self::ALLOWED_VALUES['parameter_collection_format']
);

$this->validateParameterType($param, $operation, $paramId, $operationId);

Expand Down Expand Up @@ -226,7 +230,7 @@ protected function validateType(array $schema, array $validTypes, string $schema
protected function validatePathParameters(array $params, string $path, string $operationId): void
{
$pathParams = Arr::where($params, function ($param) {
return ($param['in'] === 'path');
return $param['in'] === 'path';
});

preg_match_all(self::PATH_PARAM_REGEXP, $path, $matches);
Expand All @@ -243,7 +247,10 @@ protected function validatePathParameters(array $params, string $path, string $o
}));

if (!empty($requiredParams)) {
throw new InvalidSwaggerSpecException("Path parameters cannot be optional. Set required=true for the '{$requiredParams}' parameters at operation '{$operationId}'.");
throw new InvalidSwaggerSpecException(
"Path parameters cannot be optional. Set required=true for the "
. "'{$requiredParams}' parameters at operation '{$operationId}'."
);
}

$missingPlaceholders = array_diff(Arr::pluck($pathParams, 'name'), $placeholders);
Expand All @@ -265,11 +272,15 @@ protected function validateBodyParameters(array $parameters, string $operationId
$formParamsCount = collect($parameters)->where('in', 'formData')->count();

if ($bodyParamsCount > 1) {
throw new InvalidSwaggerSpecException("Operation '{$operationId}' has {$bodyParamsCount} body parameters. Only one is allowed.");
throw new InvalidSwaggerSpecException(
"Operation '{$operationId}' has {$bodyParamsCount} body parameters. Only one is allowed."
);
}

if (!empty($bodyParams) && $formParamsCount) {
throw new InvalidSwaggerSpecException("Operation '{$operationId}' has body and formData parameters. Only one or the other is allowed.");
throw new InvalidSwaggerSpecException(
"Operation '{$operationId}' has body and formData parameters. Only one or the other is allowed."
);
}
}

Expand Down Expand Up @@ -358,7 +369,9 @@ protected function validateRefs(): void

if (!empty($refFilename) && !file_exists($refFilename)) {
throw new MissingRefFileException($refFilename);
} elseif (!empty($refFilename) && file_exists($refFilename)) {
}

if (!empty($refFilename)) {
$externalDoc = json_decode(file_get_contents($refFilename), true);

$missingRefs = $this->getMissingFields([$refKey], $refParentKey, $externalDoc);
Expand Down Expand Up @@ -426,7 +439,9 @@ function ($consume) {
);

if (empty($requiredConsume)) {
throw new InvalidSwaggerSpecException("Operation '{$operationId}' has body and formData parameters. Only one or the other is allowed.");
throw new InvalidSwaggerSpecException(
"Operation '{$operationId}' has body and formData parameters. Only one or the other is allowed."
);
}
}

Expand Down
10 changes: 7 additions & 3 deletions tests/AutoDocControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ public function testGetJSONDocumentationWithAdditionalPaths()

public function getJSONDocumentationInvalidAdditionalDoc(): array
{
$basePath = 'tests/fixtures/AutoDocControllerTest';

return [
[
'additionalDocPath' => 'invalid_path/non_existent_file.json'
],
[
'additionalDocPath' => 'tests/fixtures/AutoDocControllerTest/documentation__non_json.txt'
'additionalDocPath' => $basePath . '/documentation__non_json.txt'
],
[
'additionalDocPath' => 'tests/fixtures/AutoDocControllerTest/documentation__invalid_format__missing_field__paths.json'
'additionalDocPath' => $basePath. '/documentation__invalid_format__missing_field__paths.json'
]
];
}
Expand Down Expand Up @@ -174,7 +176,9 @@ public function testGetElementsAssetFile()

$response->assertStatus(Response::HTTP_OK);

$this->assertEquals($response->getContent(), file_get_contents(resource_path('/assets/elements/web-components.min.js')));
$this->assertEquals(
$response->getContent(), file_get_contents(resource_path('/assets/elements/web-components.min.js'))
);

$response->assertHeader('Content-Type', 'text/html; charset=UTF-8');
}
Expand Down
Loading

0 comments on commit 1d8a9cb

Please sign in to comment.