Skip to content

Commit

Permalink
Merge pull request #57 from nextcloud/fix/parameter-return-types
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin authored Dec 14, 2023
2 parents 2e3ab22 + b9a20cc commit f00db4c
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 10 deletions.
7 changes: 4 additions & 3 deletions merge-specs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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++) {
Expand Down
2 changes: 1 addition & 1 deletion src/ControllerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class ControllerMethod {
/**
* @param ControllerMethodParameter[] $parameters
* @param ControllerMethodResponse[] $responses
* @param list<ControllerMethodResponse|null> $responses
* @param OpenApiType[] $returns
* @param array<int, string> $responseDescription
* @param string[] $description
Expand Down
2 changes: 1 addition & 1 deletion src/ControllerMethodParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 7 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/LoggerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;

class LoggerException extends Exception {
/** @psalm-suppress MissingParamType False-positive */
public function __construct(
public LoggerLevel $level,
public string $context,
Expand Down
3 changes: 2 additions & 1 deletion src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
2 changes: 1 addition & 1 deletion src/ResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function getAll(): array {
/**
* @param string $context
* @param TypeNode $obj
* @return ControllerMethodResponse[]
* @return list<ControllerMethodResponse|null>
* @throws Exception
*/
public static function resolve(string $context, TypeNode $obj): array {
Expand Down
2 changes: 1 addition & 1 deletion src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f00db4c

Please sign in to comment.