From d5de96ae1c827ed460a20d7ecb9879da8adda2ce Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 13 Dec 2023 12:21:14 +0100 Subject: [PATCH] refactor: Cleanup array handling Signed-off-by: jld3103 --- generate-spec | 7 ++++++- merge-specs | 2 +- src/ControllerMethod.php | 2 +- src/Helpers.php | 21 ++++++++++++--------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/generate-spec b/generate-spec index fa516d9..8bddbf7 100755 --- a/generate-spec +++ b/generate-spec @@ -508,7 +508,12 @@ foreach ($routes as $route) { } $statusCodeResponses = array_filter($route->controllerMethod->responses, fn (?ControllerMethodResponse $response) => $response != null && $response->statusCode == $statusCode); - $headers = array_merge(...array_map(fn (ControllerMethodResponse $response) => $response->headers ?? [], $statusCodeResponses)); + $headers = []; + foreach ($statusCodeResponses as $response) { + if ($response->headers !== null) { + $headers = array_merge($headers, $response->headers); + } + } $mergedContentTypeResponses = []; foreach (array_unique(array_map(fn (ControllerMethodResponse $response) => $response->contentType, array_filter($statusCodeResponses, fn (ControllerMethodResponse $response) => $response->contentType != null))) as $contentType) { diff --git a/merge-specs b/merge-specs index db676dd..ff33204 100755 --- a/merge-specs +++ b/merge-specs @@ -108,7 +108,7 @@ function rewriteRefs(array $spec): array { function collectCapabilities(array $spec): array { $capabilities = []; $readableAppID = Helpers::generateReadableAppID($spec["info"]["title"]); - foreach ($spec["components"]["schemas"] as $name => $schema) { + foreach (array_keys($spec["components"]["schemas"]) as $name) { if ($name == "Capabilities" || $name == "PublicCapabilities") { $capabilities[] = $readableAppID . $name; } diff --git a/src/ControllerMethod.php b/src/ControllerMethod.php index 7c138fb..6ac3a3f 100644 --- a/src/ControllerMethod.php +++ b/src/ControllerMethod.php @@ -199,7 +199,7 @@ public static function parse(string $context, array $definitions, ClassMethod $m Logger::warning($context, "Summary ends with a punctuation mark"); } - return new ControllerMethod($parameters, array_values($responses), $returns, $responseDescriptions, $methodDescription, $methodSummary, $isDeprecated); + return new ControllerMethod($parameters, $responses, $returns, $responseDescriptions, $methodDescription, $methodSummary, $isDeprecated); } } diff --git a/src/Helpers.php b/src/Helpers.php index 15984cc..1282f6d 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -68,28 +68,31 @@ public static function mergeSchemas(array $schemas) { $keys = []; foreach ($schemas as $schema) { - foreach ($schema as $key => $value) { + foreach (array_keys($schema) as $key) { $keys[] = $key; } } $result = []; + /** @var string $key */ foreach ($keys as $key) { if ($key == "required") { - $result["required"] = array_unique(array_merge(...array_map(function (array $schema) { + $required = []; + foreach ($schemas as $schema) { if (array_key_exists("required", $schema)) { - return $schema["required"]; + $required = array_merge($required, $schema["required"]); } - return []; - }, $schemas))); + } + $result["required"] = array_unique($required); continue; } - $result[$key] = self::mergeSchemas(array_filter(array_map(function (array $schema) use ($key) { + $subSchemas = []; + foreach ($schemas as $schema) { if (array_key_exists($key, $schema)) { - return $schema[$key]; + $subSchemas[] = $schema[$key]; } - return null; - }, $schemas), fn ($schema) => $schema != null)); + } + $result[$key] = self::mergeSchemas($subSchemas); } return $result;