From b9a20cc77f163072a54ef32db248338cab459fb0 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Wed, 13 Dec 2023 15:05:10 +0100 Subject: [PATCH] fix: Adjust parameter and return types Signed-off-by: jld3103 --- merge-specs | 7 ++++--- src/ControllerMethod.php | 2 +- src/ControllerMethodParameter.php | 2 +- src/Helpers.php | 4 ++-- src/Logger.php | 7 +++++++ src/LoggerException.php | 1 + src/OpenApiType.php | 3 ++- src/ResponseType.php | 2 +- src/Route.php | 2 +- 9 files changed, 20 insertions(+), 10 deletions(-) diff --git a/merge-specs b/merge-specs index db676dd..1ffc1d3 100755 --- a/merge-specs +++ b/merge-specs @@ -97,7 +97,7 @@ function loadSpec(string $path): array { function rewriteRefs(array $spec): array { $readableAppID = Helpers::generateReadableAppID($spec["info"]["title"]); - array_walk_recursive($spec, function (&$item, $key) use ($readableAppID) { + array_walk_recursive($spec, function (string &$item, string $key) use ($readableAppID) { if ($key == "\$ref" && $item != "#/components/schemas/OCSMeta") { $item = str_replace("#/components/schemas/", "#/components/schemas/" . $readableAppID, $item); } @@ -151,8 +151,9 @@ function rewriteOperations(array $spec): array { $operation["tags"] = [$spec["info"]["title"]]; } if ($firstStatusCode && array_key_exists("responses", $operation)) { - $firstStatusCode = array_key_first($operation["responses"]); - $operation["responses"] = [$firstStatusCode => $operation["responses"][$firstStatusCode]]; + /** @var string $value */ + $value = array_key_first($operation["responses"]); + $operation["responses"] = [$value => $operation["responses"][$value]]; } if (array_key_exists("security", $operation)) { for ($i = 0; $i < count($operation["security"]); $i++) { diff --git a/src/ControllerMethod.php b/src/ControllerMethod.php index 7c138fb..693a3e3 100644 --- a/src/ControllerMethod.php +++ b/src/ControllerMethod.php @@ -13,7 +13,7 @@ class ControllerMethod { /** * @param ControllerMethodParameter[] $parameters - * @param ControllerMethodResponse[] $responses + * @param list $responses * @param OpenApiType[] $returns * @param array $responseDescription * @param string[] $description diff --git a/src/ControllerMethodParameter.php b/src/ControllerMethodParameter.php index 365f0d6..389ad70 100644 --- a/src/ControllerMethodParameter.php +++ b/src/ControllerMethodParameter.php @@ -46,7 +46,7 @@ private static function exprToValue(string $context, Expr $expr): mixed { return -self::exprToValue($context, $expr->expr); } if ($expr instanceof Array_) { - $values = array_map(fn (ArrayItem $item) => self::exprToValue($context, $item), $expr->items); + $values = array_map(fn (ArrayItem $item): mixed => self::exprToValue($context, $item), $expr->items); $filteredValues = array_filter($values, fn (mixed $value) => $value !== null); if (count($filteredValues) != count($values)) { return null; diff --git a/src/Helpers.php b/src/Helpers.php index 15984cc..dfea8e2 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -57,7 +57,7 @@ public static function mapVerb(string $verb): string { }; } - public static function mergeSchemas(array $schemas) { + public static function mergeSchemas(array $schemas): mixed { if (!in_array(true, array_map(fn ($schema) => is_array($schema), $schemas))) { $results = array_values(array_unique($schemas)); if (count($results) > 1) { @@ -123,7 +123,7 @@ public static function wrapOCSResponse(Route $route, ControllerMethodResponse $r return $schema; } - public static function cleanEmptyResponseArray(array|stdClass $schema): array|stdClass { + public static function cleanEmptyResponseArray(array $schema): array|stdClass { if (key_exists("type", $schema) && $schema["type"] == "array" && key_exists("maxLength", $schema) && $schema["maxLength"] === 0) { return new stdClass(); } diff --git a/src/Logger.php b/src/Logger.php index 3d3ca86..b3fd860 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -34,6 +34,9 @@ public static function warning(string $context, string $text): void { self::log(LoggerLevel::Warning, $context, $text); } + /** + * @throws LoggerException + */ public static function error(string $context, string $text): void { if (self::$exitOnError) { throw new LoggerException(LoggerLevel::Error, $context, $text); @@ -42,6 +45,10 @@ public static function error(string $context, string $text): void { } } + /** + * @throws LoggerException + * @psalm-return no-return + */ public static function panic(string $context, string $text): void { throw new LoggerException(LoggerLevel::Error, $context, $text); } diff --git a/src/LoggerException.php b/src/LoggerException.php index 402a184..37e5c47 100644 --- a/src/LoggerException.php +++ b/src/LoggerException.php @@ -5,6 +5,7 @@ use Exception; class LoggerException extends Exception { + /** @psalm-suppress MissingParamType False-positive */ public function __construct( public LoggerLevel $level, public string $context, diff --git a/src/OpenApiType.php b/src/OpenApiType.php index 6b8dea8..fafe236 100644 --- a/src/OpenApiType.php +++ b/src/OpenApiType.php @@ -259,8 +259,9 @@ enum: [(int) $node->constExpr->value], /** * @param OpenApiType[] $types + * @return OpenApiType[] */ - private static function mergeEnums(array $types) { + private static function mergeEnums(array $types): array { $enums = []; $nonEnums = []; diff --git a/src/ResponseType.php b/src/ResponseType.php index 3d00064..0fb06ff 100644 --- a/src/ResponseType.php +++ b/src/ResponseType.php @@ -159,7 +159,7 @@ public static function getAll(): array { /** * @param string $context * @param TypeNode $obj - * @return ControllerMethodResponse[] + * @return list * @throws Exception */ public static function resolve(string $context, TypeNode $obj): array { diff --git a/src/Route.php b/src/Route.php index a68dcf8..f53c72b 100644 --- a/src/Route.php +++ b/src/Route.php @@ -36,7 +36,7 @@ public static function parseRoutes(string $path): array { } } - private static function includeRoutes(string $code) { + private static function includeRoutes(string $code): array { $tmpPath = tempnam(sys_get_temp_dir(), "routes-"); file_put_contents($tmpPath, $code); $routes = include($tmpPath);