Skip to content

Commit 34968eb

Browse files
committed
refactor: Apply rector refactorings
Signed-off-by: provokateurin <[email protected]>
1 parent c631d38 commit 34968eb

File tree

6 files changed

+35
-39
lines changed

6 files changed

+35
-39
lines changed

generate-spec.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@
292292
/** @var AttributeGroup $attrGroup */
293293
foreach ($classMethod->attrGroups as $attrGroup) {
294294
foreach ($attrGroup->attrs as $attr) {
295-
if ($attr->name->getLast() !== 'Route' && $attr->name->getLast() !== 'ApiRoute' && $attr->name->getLast() !== 'FrontpageRoute') {
295+
if (!in_array($attr->name->getLast(), ['Route', 'ApiRoute', 'FrontpageRoute'], true)) {
296296
continue;
297297
}
298298

@@ -397,7 +397,7 @@
397397
// This is very ugly, but since we do not parse the entire source code we can not say with certainty which controller type is used.
398398
// To still allow apps to use custom controllers that extend OCSController, we only check the suffix and have the warning if the controller type can not be detected.
399399
$isOCS = str_ends_with($parentControllerClass, 'OCSController');
400-
if ($parentControllerClass !== 'Controller' && $parentControllerClass !== 'ApiController' && $parentControllerClass !== 'OCSController' && !$isOCS) {
400+
if (!in_array($parentControllerClass, ['Controller', 'ApiController', 'OCSController'], true) && !$isOCS) {
401401
Logger::warning($routeName, 'You are extending a custom controller class. Make sure that it ends with "OCSController" if it extends "OCSController" itself.');
402402
} elseif ($isOCS !== ($pathPrefix === '/ocs/v2.php')) {
403403
Logger::warning($routeName, 'Do not mix OCS/non-OCS routes and non-OCS/OCS controllers!');
@@ -422,9 +422,9 @@
422422
Logger::panic($routeName, "Controller '" . $controllerName . "' is marked as ignore but also has other scopes");
423423
}
424424

425-
$tagName = implode('_', array_map(fn (string $s) => strtolower($s), Helpers::splitOnUppercaseFollowedByNonUppercase(str_replace('\\', '', $controllerName))));
425+
$tagName = implode('_', array_map(strtolower(...), Helpers::splitOnUppercaseFollowedByNonUppercase(str_replace('\\', '', $controllerName))));
426426
$doc = $controllerClass->getDocComment()?->getText();
427-
if ($doc != null && count(array_filter($tags, fn (array $tag): bool => $tag['name'] === $tagName)) == 0) {
427+
if ($doc != null && count(array_filter($tags, fn (array $tag): bool => $tag['name'] === $tagName)) === 0) {
428428
$classDescription = [];
429429

430430
$docNodes = $phpDocParser->parse(new TokenIterator($lexer->tokenize($doc)))->children;
@@ -520,7 +520,7 @@
520520
}
521521

522522
$classMethodInfo = ControllerMethod::parse($routeName, $definitions, $methodFunction, $isPublic, $isAdmin, $isDeprecated, $isPasswordConfirmation, $isCORS, $isOCS);
523-
if (count($classMethodInfo->responses) == 0) {
523+
if (count($classMethodInfo->responses) === 0) {
524524
Logger::error($routeName, 'Returns no responses');
525525
continue;
526526
}
@@ -612,7 +612,7 @@
612612
foreach ($urlParameters as $urlParameter) {
613613
$matchingParameters = array_filter($route->controllerMethod->parameters, fn (ControllerMethodParameter $param): bool => $param->name == $urlParameter);
614614
$requirement = $route->requirements[$urlParameter] ?? null;
615-
if (count($matchingParameters) == 1) {
615+
if (count($matchingParameters) === 1) {
616616
$parameter = $matchingParameters[array_keys($matchingParameters)[0]];
617617

618618
$schema = $parameter->type->toArray(true);
@@ -640,7 +640,7 @@
640640
continue;
641641
}
642642
preg_match("/^\^\(([v0-9-.|]*)\)\\$$/m", (string)$requirement, $matches);
643-
if (count($matches) == 2) {
643+
if (count($matches) === 2) {
644644
$enum = explode('|', $matches[1]);
645645
} else {
646646
Logger::error($route->name, 'Invalid requirement for apiVersion');
@@ -714,7 +714,7 @@
714714

715715
$hasEmpty = array_filter($contentTypeResponses, fn (ControllerMethodResponse $response): bool => $response->type == null) !== [];
716716
$uniqueResponses = array_values(array_intersect_key($contentTypeResponses, array_unique(array_map(fn (ControllerMethodResponse $response): array|\stdClass => $response->type->toArray(), array_filter($contentTypeResponses, fn (ControllerMethodResponse $response): bool => $response->type != null)), SORT_REGULAR)));
717-
if (count($uniqueResponses) == 1) {
717+
if (count($uniqueResponses) === 1) {
718718
if ($hasEmpty) {
719719
$mergedContentTypeResponses[$contentType] = [];
720720
} else {
@@ -946,7 +946,7 @@
946946
];
947947
}
948948

949-
if (count($schemas) == 0 && count($routes) == 0) {
949+
if (count($schemas) === 0 && count($routes) === 0) {
950950
Logger::error('app', 'No spec generated');
951951
}
952952

@@ -972,7 +972,7 @@
972972

973973
$usedSchemas = ['Capabilities', 'PublicCapabilities'];
974974

975-
foreach (glob(dirname($out) . '/openapi*.json') as $path) {
975+
foreach (glob(dirname((string)$out) . '/openapi*.json') as $path) {
976976
unlink($path);
977977
}
978978

@@ -1060,12 +1060,12 @@
10601060
$openapiScope['paths'] = new stdClass();
10611061
}
10621062

1063-
$startExtension = strrpos($out, '.');
1063+
$startExtension = strrpos((string)$out, '.');
10641064
if ($startExtension !== false) {
10651065
// Path + filename (without extension)
1066-
$path = substr($out, 0, $startExtension);
1066+
$path = substr((string)$out, 0, $startExtension);
10671067
// Extension
1068-
$extension = substr($out, $startExtension);
1068+
$extension = substr((string)$out, $startExtension);
10691069
$scopeOut = $path . $scopeSuffix . $extension;
10701070
} else {
10711071
$scopeOut = $out . $scopeSuffix;

rector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@
1717
deadCode: true,
1818
codeQuality: true,
1919
typeDeclarations: true,
20-
strictBooleans: true,
2120
);

src/ControllerMethod.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ public static function parse(string $context,
397397
}
398398

399399
if (str_starts_with($type->name, 'OCS') && str_ends_with($type->name, 'Exception')) {
400-
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, 'application/json', new OpenApiType(context: $context, type: 'array', maxItems: 0), null);
400+
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, 'application/json', new OpenApiType(context: $context, type: 'array', maxItems: 0));
401401
} else {
402-
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, 'text/plain', new OpenApiType(context: $context, type: 'string'), null);
402+
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, 'text/plain', new OpenApiType(context: $context, type: 'string'));
403403
}
404404
}
405405
}
@@ -566,7 +566,7 @@ public static function parse(string $context,
566566
$parameters[] = $param;
567567
}
568568

569-
if (!$allowMissingDocs && count($methodDescription) == 0) {
569+
if (!$allowMissingDocs && count($methodDescription) === 0) {
570570
Logger::error($context, 'Missing method description');
571571
}
572572

@@ -582,7 +582,7 @@ public static function parse(string $context,
582582
$methodDescription[] = 'This endpoint allows CORS requests';
583583
}
584584

585-
if (count($methodDescription) == 1) {
585+
if (count($methodDescription) === 1) {
586586
$methodSummary = $methodDescription[0];
587587
$methodDescription = [];
588588
} elseif (count($methodDescription) > 1) {

src/Helpers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Helpers {
2626
public const OPENAPI_ATTRIBUTE_CLASSNAME = 'OpenAPI';
2727

2828
public static function generateReadableAppID(string $appID): string {
29-
return implode('', array_map(fn (string $s): string => ucfirst($s), explode('_', $appID)));
29+
return implode('', array_map(ucfirst(...), explode('_', $appID)));
3030
}
3131

3232
public static function securitySchemes(): array {
@@ -75,7 +75,7 @@ public static function splitOnUppercaseFollowedByNonUppercase(string $str): arra
7575
}
7676

7777
public static function mergeSchemas(array $schemas): mixed {
78-
if (!in_array(true, array_map(fn ($schema): bool => is_array($schema), $schemas))) {
78+
if (!in_array(true, array_map(is_array(...), $schemas))) {
7979
$results = array_values(array_unique($schemas));
8080
if (count($results) > 1) {
8181
throw new Exception('Incompatibles types: ' . implode(', ', $results));
@@ -271,7 +271,7 @@ public static function getOpenAPIAttributeTagsByScope(ClassMethod|Class_|Node $n
271271
}
272272

273273
if ($foundTags !== []) {
274-
$tags[$foundScopeName !== null && $foundScopeName !== '' && $foundScopeName !== '0' ? $foundScopeName : $defaultScope] = $foundTags;
274+
$tags[in_array($foundScopeName, [null, '', '0'], true) ? $defaultScope : $foundScopeName] = $foundTags;
275275
}
276276
}
277277
}

src/OpenApiType.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,14 @@ public function __construct(
6464
}
6565

6666
public function toArray(bool $isParameter = false): array|stdClass {
67-
if ($isParameter) {
68-
if ($this->type === 'object' || $this->ref !== null || $this->anyOf !== null || $this->allOf !== null) {
69-
Logger::warning($this->context, 'Complex types can not be part of query or URL parameters. Falling back to string due to undefined serialization!');
70-
71-
return (new OpenApiType(
72-
context: $this->context,
73-
type: 'string',
74-
nullable: $this->nullable,
75-
description: $this->description,
76-
))->toArray($isParameter);
77-
}
67+
if ($isParameter && ($this->type === 'object' || $this->ref !== null || $this->anyOf !== null || $this->allOf !== null)) {
68+
Logger::warning($this->context, 'Complex types can not be part of query or URL parameters. Falling back to string due to undefined serialization!');
69+
return (new OpenApiType(
70+
context: $this->context,
71+
type: 'string',
72+
nullable: $this->nullable,
73+
description: $this->description,
74+
))->toArray($isParameter);
7875
}
7976

8077
$values = [];
@@ -173,7 +170,7 @@ public static function resolve(string $context, array $definitions, ParamTagValu
173170
items: self::resolve($context . ': items', $definitions, $node->type),
174171
);
175172
}
176-
if ($node instanceof GenericTypeNode && ($node->type->name === 'array' || $node->type->name === 'list' || $node->type->name === 'non-empty-list') && count($node->genericTypes) === 1) {
173+
if ($node instanceof GenericTypeNode && (in_array($node->type->name, ['array', 'list', 'non-empty-list'], true)) && count($node->genericTypes) === 1) {
177174
if ($node->type->name === 'array') {
178175
Logger::error($context, "The 'array<TYPE>' syntax for arrays is forbidden due to ambiguities. Use 'list<TYPE>' for JSON arrays or 'array<string, TYPE>' for JSON objects instead.");
179176
}
@@ -235,7 +232,7 @@ public static function resolve(string $context, array $definitions, ParamTagValu
235232
Logger::panic($context, "JSON objects can only be indexed by '" . implode("', '", $allowedTypes) . "' but got '" . $node->genericTypes[0]->name . "'");
236233
}
237234

238-
if ($node instanceof GenericTypeNode && $node->type->name === 'int' && count($node->genericTypes) == 2) {
235+
if ($node instanceof GenericTypeNode && $node->type->name === 'int' && count($node->genericTypes) === 2) {
239236
$min = null;
240237
$max = null;
241238
if ($node->genericTypes[0] instanceof ConstTypeNode) {
@@ -331,7 +328,7 @@ enum: $values,
331328
$items = array_unique($items, SORT_REGULAR);
332329
$items = self::mergeEnums($context, $items);
333330

334-
if (count($items) == 1) {
331+
if (count($items) === 1) {
335332
$type = $items[0];
336333
if ($type->ref !== null) {
337334
// https://github.com/OAI/OpenAPI-Specification/issues/1368#issuecomment-354037150

src/ResponseType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,15 @@ public static function resolve(string $context, TypeNode $obj): array {
201201
if ($className == 'void') {
202202
$responses[] = null;
203203
} else {
204-
if (count(array_filter($responseTypes, fn ($responseType): bool => $responseType->className == $className)) == 0) {
204+
if (count(array_filter($responseTypes, fn (\OpenAPIExtractor\ResponseType $responseType): bool => $responseType->className == $className)) === 0) {
205205
Logger::error($context, "Invalid return type '" . $obj . "'");
206206
return [];
207207
}
208208
foreach ($responseTypes as $responseType) {
209209
if ($responseType->className == $className) {
210210
// +2 for status code and headers which are always present
211-
$expectedArgs = count(array_filter([$responseType->hasContentTypeTemplate, $responseType->hasTypeTemplate], fn ($value): bool => $value)) + 2;
212-
if (count($args) != $expectedArgs) {
211+
$expectedArgs = count(array_filter([$responseType->hasContentTypeTemplate, $responseType->hasTypeTemplate], fn (bool $value): bool => $value)) + 2;
212+
if (count($args) !== $expectedArgs) {
213213
Logger::error($context, "'" . $className . "' needs " . $expectedArgs . ' parameters');
214214
continue;
215215
}
@@ -223,7 +223,7 @@ public static function resolve(string $context, TypeNode $obj): array {
223223
} elseif ($args[$i] instanceof IdentifierTypeNode && $args[$i]->name === 'string') {
224224
$contentTypes = ['*/*'];
225225
} elseif ($args[$i] instanceof UnionTypeNode) {
226-
$contentTypes = array_map(fn ($arg) => $arg->constExpr->value, $args[$i]->types);
226+
$contentTypes = array_map(fn (\PHPStan\PhpDocParser\Ast\Type\TypeNode $arg) => $arg->constExpr->value, $args[$i]->types);
227227
} else {
228228
Logger::panic($context, 'Unable to parse content type from ' . $args[$i]::class);
229229
}

0 commit comments

Comments
 (0)