Skip to content

Commit

Permalink
refactor: Stop using array_merge to create associative arrays
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Mar 14, 2024
1 parent 09030f8 commit cd537bc
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 99 deletions.
142 changes: 80 additions & 62 deletions generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -616,17 +616,17 @@ foreach ($routes as $scope => $scopeRoutes) {
$schema["default"] = $route->defaults[$urlParameter];
}

$pathParameters[] = array_merge(
[
"name" => $urlParameter,
"in" => "path",
],
$description != null ? ["description" => $description] : [],
[
"required" => true,
"schema" => $schema,
],
);
$parameter = [
"name" => $urlParameter,
"in" => "path",
];
if ($description !== null) {
$parameter["description"] = $description;
}
$parameter["required"] = true;
$parameter["schema"] = $schema;

$pathParameters[] = $parameter;
}

$queryParameters = [];
Expand Down Expand Up @@ -687,25 +687,24 @@ foreach ($routes as $scope => $scopeRoutes) {
}
}

$mergedResponses[$statusCode] = array_merge(
[
"description" => array_key_exists($statusCode, $route->controllerMethod->responseDescription) ? $route->controllerMethod->responseDescription[$statusCode] : "",
],
count($headers) > 0 ? [
"headers" => array_combine(
array_keys($headers),
array_map(
fn (OpenApiType $type) => [
"schema" => $type->toArray($openapiVersion),
],
array_values($headers),
),
$response = [
"description" => array_key_exists($statusCode, $route->controllerMethod->responseDescription) ? $route->controllerMethod->responseDescription[$statusCode] : "",
];
if (count($headers) > 0) {
$response["headers"] = array_combine(
array_keys($headers),
array_map(
fn (OpenApiType $type) => [
"schema" => $type->toArray($openapiVersion),
],
array_values($headers),
),
] : [],
count($mergedContentTypeResponses) > 0 ? [
"content" => $mergedContentTypeResponses,
] : [],
);
);
}
if (count($mergedContentTypeResponses) > 0) {
$response["content"] = $mergedContentTypeResponses;
}
$mergedResponses[$statusCode] = $response;
}

$operationId = [...$route->tags, ...Helpers::splitOnUppercaseFollowedByNonUppercase($route->methodName)];
Expand All @@ -727,39 +726,58 @@ foreach ($routes as $scope => $scopeRoutes) {
$security[] = ["basic_auth" => []];
}

$operation = array_merge(
["operationId" => strtolower(implode("-", $operationId))],
$route->controllerMethod->summary != null ? ["summary" => $route->controllerMethod->summary] : [],
count($route->controllerMethod->description) > 0 ? ["description" => implode("\n", $route->controllerMethod->description)] : [],
$route->controllerMethod->isDeprecated ? ["deprecated" => true] : [],
$useTags ? ["tags" => $route->tags] : [],
count($security) > 0 ? ["security" => $security] : [],
count($queryParameters) > 0 || count($pathParameters) > 0 || $route->isOCS ? [
"parameters" => array_merge(
array_map(fn (ControllerMethodParameter $parameter) => array_merge(
[
"name" => $parameter->name . ($parameter->type->type == "array" ? "[]" : ""),
"in" => "query",
],
$parameter->docType != null && $parameter->docType->description != "" ? ["description" => Helpers::cleanDocComment($parameter->docType->description)] : [],
!$parameter->type->nullable && !$parameter->type->hasDefaultValue ? ["required" => true] : [],
["schema" => $parameter->type->toArray($openapiVersion, true),],
), $queryParameters),
$pathParameters,
$route->isOCS ? [[
"name" => "OCS-APIRequest",
"in" => "header",
"description" => "Required to be true for the API request to pass",
"required" => true,
"schema" => [
"type" => "boolean",
"default" => true,
],
]] : [],
),
] : [],
["responses" => $mergedResponses],
);
$operation = [
"operationId" => strtolower(implode("-", $operationId)),
];
if ($route->controllerMethod->summary !== null) {
$operation["summary"] = $route->controllerMethod->summary;
}
if (count($route->controllerMethod->description) > 0) {
$operation["description"] = implode("\n", $route->controllerMethod->description);
}
if ($route->controllerMethod->isDeprecated) {
$operation["deprecated"] = true;
}
if ($useTags) {
$operation["tags"] = $route->tags;
}
if (count($security) > 0) {
$operation["security"] = $security;
}
if (count($queryParameters) > 0 || count($pathParameters) > 0 || $route->isOCS) {
$parameters = [
...array_map(static function (ControllerMethodParameter $parameter) use ($openapiVersion) {
$out = [
"name" => $parameter->name . ($parameter->type->type === "array" ? "[]" : ""),
"in" => "query",
];
if ($parameter->docType !== null && $parameter->docType->description !== "") {
$out["description"] = Helpers::cleanDocComment($parameter->docType->description);
}
if (!$parameter->type->nullable && !$parameter->type->hasDefaultValue) {
$out["required"] = true;
}
$out["schema"] = $parameter->type->toArray($openapiVersion, true);

return $out;
}, $queryParameters),
...$pathParameters,
];
if ($route->isOCS) {
$parameters[] = [
"name" => "OCS-APIRequest",
"in" => "header",
"description" => "Required to be true for the API request to pass",
"required" => true,
"schema" => [
"type" => "boolean",
"default" => true,
],
];
}
$operation["parameters"] = $parameters;
}
$operation["responses"] = $mergedResponses;

$scopePaths[$scope] ??= [];
$scopePaths[$scope][$route->url] ??= [];
Expand Down
11 changes: 7 additions & 4 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ public static function license(string $openapiVersion, string $license): array {
"agpl" => "AGPL-3.0-only",
default => Logger::panic("license", "Unable to convert " . $license . " to SPDX identifier"),
};
return array_merge([

$out = [
"name" => "agpl",
],
version_compare($openapiVersion, "3.1.0", ">=") ? ["identifier" => $identifier] : [],
);
];
if (version_compare($openapiVersion, "3.1.0", ">=")) {
$out["identifier"] = $identifier;
}
return $out;
}

public static function jsonFlags(): int {
Expand Down
106 changes: 73 additions & 33 deletions src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,18 @@ public function toArray(string $openapiVersion, bool $isParameter = false): arra
$this->anyOf !== null ||
$this->allOf !== null);
if ($asContentString) {
return array_merge([
$values = [
"type" => "string",
],
$this->nullable ? ["nullable" => true] : [],
version_compare($openapiVersion, "3.1.0", ">=") ? [
"contentMediaType" => "application/json",
"contentSchema" => $this->toArray($openapiVersion),
] : [],
);
];
if ($this->nullable) {
$values["nullable"] = true;
}
if (version_compare($openapiVersion, "3.1.0", ">=")) {
$values["contentMediaType"] = "application/json";
$values["contentSchema"] = $this->toArray($openapiVersion);
}

return $values;
}

$type = $this->type;
Expand All @@ -80,31 +83,68 @@ public function toArray(string $openapiVersion, bool $isParameter = false): arra
}
}

$values = array_merge(
$this->ref != null ? ["\$ref" => $this->ref] : [],
$type != null ? ["type" => $type] : [],
$this->format != null ? ["format" => $this->format] : [],
$this->nullable ? ["nullable" => true] : [],
$this->hasDefaultValue && $defaultValue !== null ? ["default" => $defaultValue] : [],
$enum != null ? ["enum" => $enum] : [],
$this->description != null && $this->description != "" && !$isParameter ? ["description" => $this->description] : [],
$this->items != null ? ["items" => $this->items->toArray($openapiVersion)] : [],
$this->minLength !== null ? ["minLength" => $this->minLength] : [],
$this->maxLength !== null ? ["maxLength" => $this->maxLength] : [],
$this->minimum !== null ? ["minimum" => $this->minimum] : [],
$this->maximum !== null ? ["maximum" => $this->maximum] : [],
$this->required != null ? ["required" => $this->required] : [],
$this->properties != null ? ["properties" =>
array_combine(array_keys($this->properties),
array_map(fn (OpenApiType $property) => $property->toArray($openapiVersion), array_values($this->properties)),
)] : [],
$this->additionalProperties != null ? [
"additionalProperties" => $this->additionalProperties instanceof OpenApiType ? $this->additionalProperties->toArray($openapiVersion) : $this->additionalProperties,
] : [],
$this->oneOf != null ? ["oneOf" => array_map(fn (OpenApiType $type) => $type->toArray($openapiVersion), $this->oneOf)] : [],
$this->anyOf != null ? ["anyOf" => array_map(fn (OpenApiType $type) => $type->toArray($openapiVersion), $this->anyOf)] : [],
$this->allOf != null ? ["allOf" => array_map(fn (OpenApiType $type) => $type->toArray($openapiVersion), $this->allOf)] : [],
);
$values = [];
if ($this->ref !== null) {
$values["\$ref"] = $this->ref;
}
if ($type !== null) {
$values["type"] = $type;
}
if ($this->format !== null) {
$values["format"] = $this->format;
}
if ($this->nullable) {
$values["nullable"] = true;
}
if ($this->hasDefaultValue && $defaultValue !== null) {
$values["default"] = $defaultValue;
}
if ($enum !== null) {
$values["enum"] = $enum;
}
if ($this->description !== null && $this->description !== "" && !$isParameter) {
$values["description"] = $this->description;
}
if ($this->items !== null) {
$values["items"] = $this->items->toArray($openapiVersion);
}
if ($this->minLength !== null) {
$values["minLength"] = $this->minLength;
}
if ($this->maxLength !== null) {
$values["maxLength"] = $this->maxLength;
}
if ($this->minimum !== null) {
$values["minimum"] = $this->minimum;
}
if ($this->maximum !== null) {
$values["maximum"] = $this->maximum;
}
if ($this->required !== null) {
$values["required"] = $this->required;
}
if ($this->properties !== null && count($this->properties) > 0) {
$values["properties"] = array_combine(array_keys($this->properties),
array_map(static fn (OpenApiType $property) => $property->toArray($openapiVersion), array_values($this->properties)),
);
}
if ($this->additionalProperties !== null) {
if ($this->additionalProperties instanceof OpenApiType) {
$values["additionalProperties"] = $this->additionalProperties->toArray($openapiVersion);
} else {
$values["additionalProperties"] = $this->additionalProperties;
}
}
if ($this->oneOf !== null) {
$values["oneOf"] = array_map(fn (OpenApiType $type) => $type->toArray($openapiVersion), $this->oneOf);
}
if ($this->anyOf !== null) {
$values["anyOf"] = array_map(fn (OpenApiType $type) => $type->toArray($openapiVersion), $this->anyOf);
}
if ($this->allOf !== null) {
$values["allOf"] = array_map(fn (OpenApiType $type) => $type->toArray($openapiVersion), $this->allOf);
}

return count($values) > 0 ? $values : new stdClass();
}

Expand Down

0 comments on commit cd537bc

Please sign in to comment.