Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Cleanup array handling #58

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {

$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