Skip to content

Commit

Permalink
refactor: Cleanup array handling
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <[email protected]>
  • Loading branch information
provokateurin authored and nickvergessen committed Dec 21, 2023
1 parent bcee8f3 commit 360870c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
7 changes: 6 additions & 1 deletion generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion merge-specs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ControllerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
21 changes: 12 additions & 9 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,31 @@ public static function mergeSchemas(array $schemas): mixed {

$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;
Expand Down

0 comments on commit 360870c

Please sign in to comment.