Skip to content

Commit 50391ff

Browse files
Merge pull request #239 from nextcloud/refactor/rector
2 parents 34b1a9a + 89db03e commit 50391ff

File tree

7 files changed

+24
-24
lines changed

7 files changed

+24
-24
lines changed

generate-spec.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@
254254
$controllerFiles = [];
255255
foreach ($iterator as $file) {
256256
$filePath = $file->getPathname();
257-
if (!str_ends_with($filePath, 'Controller.php')) {
257+
if (!str_ends_with((string)$filePath, 'Controller.php')) {
258258
continue;
259259
}
260260
$controllerFiles[] = $filePath;
@@ -263,7 +263,7 @@
263263

264264
foreach ($controllerFiles as $filePath) {
265265
$offset = strlen($controllersDir . '/');
266-
$name = substr($filePath, $offset, strlen($filePath) - $offset - strlen('Controller.php'));
266+
$name = substr((string)$filePath, $offset, strlen((string)$filePath) - $offset - strlen('Controller.php'));
267267
$name = str_replace('/', '\\', $name);
268268
$controllers[$name] = $nodeTraverser->traverse($astParser->parse(file_get_contents($filePath)));
269269
}
@@ -286,7 +286,7 @@
286286

287287
/** @var ClassMethod $classMethod */
288288
foreach ($nodeFinder->findInstanceOf($controllerClass->stmts, ClassMethod::class) as $classMethod) {
289-
$name = substr($class->name->name, 0, -strlen('Controller')) . '#' . $classMethod->name->name;
289+
$name = substr((string)$class->name->name, 0, -strlen('Controller')) . '#' . $classMethod->name->name;
290290

291291
/** @var AttributeGroup $attrGroup */
292292
foreach ($classMethod->attrGroups as $attrGroup) {

merge-specs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function loadSpec(string $path): object {
104104
}
105105

106106
function getAppID(object $spec): string {
107-
return explode('-', $spec->info->title)[0];
107+
return explode('-', (string)$spec->info->title)[0];
108108
}
109109

110110
function rewriteRefs(object $spec): object {
@@ -155,7 +155,7 @@ function rewriteSchemaNames(object $spec): array {
155155
}
156156

157157
function rewriteTags(object $spec): array {
158-
return array_map(static function (object $tag) use ($spec) {
158+
return array_map(static function (object $tag) use ($spec): object {
159159
$tag->name = getAppID($spec) . '/' . $tag->name;
160160
return $tag;
161161
}, $spec->tags);

src/ControllerMethod.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,20 @@ public static function parse(string $context,
7171

7272
foreach ($docNodes as $docNode) {
7373
if ($docNode instanceof PhpDocTextNode) {
74-
$nodeDescription = (string)$docNode->text;
74+
$nodeDescription = $docNode->text;
7575
} elseif ($docNode->value instanceof GenericTagValueNode) {
7676
$nodeDescription = (string)$docNode->value;
7777
} else {
7878
$nodeDescription = (string)$docNode->value->description;
7979
}
8080

81-
$nodeDescriptionLines = array_filter(explode("\n", $nodeDescription), static fn (string $line) => trim($line) !== '');
81+
$nodeDescriptionLines = array_filter(explode("\n", $nodeDescription), static fn (string $line): bool => trim($line) !== '');
8282

8383
// Parse in blocks (separate by double newline) to preserve newlines within a block.
8484
$nodeDescriptionBlocks = preg_split("/\n\s*\n/", $nodeDescription);
8585
foreach ($nodeDescriptionBlocks as $nodeDescriptionBlock) {
8686
$methodDescriptionBlockLines = [];
87-
foreach (array_filter(explode("\n", $nodeDescriptionBlock), static fn (string $line) => trim($line) !== '') as $line) {
87+
foreach (array_filter(explode("\n", $nodeDescriptionBlock), static fn (string $line): bool => trim($line) !== '') as $line) {
8888
if (preg_match(self::STATUS_CODE_DESCRIPTION_PATTERN, $line)) {
8989
$parts = preg_split(self::STATUS_CODE_DESCRIPTION_PATTERN, $line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
9090
$responseDescriptions[(int)$parts[0]] = trim($parts[1]);
@@ -133,7 +133,7 @@ public static function parse(string $context,
133133
Logger::error($context, "Missing description for exception '" . $type . "'");
134134
} else {
135135
// Only add lines that don't match the status code pattern to the description
136-
$responseDescriptions[$statusCode] = implode("\n", array_filter($nodeDescriptionLines, static fn (string $line) => !preg_match(self::STATUS_CODE_DESCRIPTION_PATTERN, $line)));
136+
$responseDescriptions[$statusCode] = implode("\n", array_filter($nodeDescriptionLines, static fn (string $line): bool => in_array(preg_match(self::STATUS_CODE_DESCRIPTION_PATTERN, $line), [0, false], true)));
137137
}
138138

139139
if (str_starts_with($type->name, 'OCS') && str_ends_with($type->name, 'Exception')) {
@@ -169,9 +169,9 @@ public static function parse(string $context,
169169
$docParameterName = substr($docParameter->parameterName, 1);
170170

171171
if ($docParameterName == $methodParameterName) {
172-
if ($docParameterType == '@param') {
172+
if ($docParameterType === '@param') {
173173
$paramTag = $docParameter;
174-
} elseif ($docParameterType == '@psalm-param') {
174+
} elseif ($docParameterType === '@psalm-param') {
175175
$psalmParamTag = $docParameter;
176176
} else {
177177
Logger::panic($context . ': @param', 'Unknown param type ' . $docParameterType);
@@ -190,7 +190,7 @@ public static function parse(string $context,
190190
$description = '';
191191
}
192192
// Only keep lines that don't match the status code pattern in the description
193-
$description = Helpers::cleanDocComment(implode("\n", array_filter(array_filter(explode("\n", $description), static fn (string $line) => trim($line) !== ''), static fn (string $line) => !preg_match(self::STATUS_CODE_DESCRIPTION_PATTERN, $line))));
193+
$description = Helpers::cleanDocComment(implode("\n", array_filter(array_filter(explode("\n", $description), static fn (string $line): bool => trim($line) !== ''), static fn (string $line): bool => in_array(preg_match(self::STATUS_CODE_DESCRIPTION_PATTERN, $line), [0, false], true))));
194194

195195
if ($paramTag instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode && $psalmParamTag instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode) {
196196
try {
@@ -250,7 +250,7 @@ public static function parse(string $context,
250250
continue;
251251
}
252252

253-
if (str_contains($param->type->description, '@deprecated')) {
253+
if (str_contains((string)$param->type->description, '@deprecated')) {
254254
$param->type->deprecated = true;
255255
$param->type->description = str_replace('@deprecated', 'Deprecated:', $param->type->description);
256256
}
@@ -298,7 +298,7 @@ public static function parse(string $context,
298298
if ($methodCall->name->name === 'getParam') {
299299
$name = $methodCall->args[0]->value->value;
300300

301-
if (preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name)) {
301+
if (preg_match('/^[a-zA-Z]\w*$/', (string)$name)) {
302302
Logger::error($context . ': getParam: ' . $name, 'Do not use getParam() when a controller method parameter also works. With getParam() it is not possible to add a comment and specify the parameter type, therefore it should be avoided whenever possible.');
303303
}
304304

src/Helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public static function cleanSchemaName(string $name): string {
177177
}
178178

179179
protected static function getScopeNameFromAttributeArgument(Arg $arg, int $key, string $routeName): ?string {
180-
if ($arg->name?->name === 'scope' || ($arg->name === null && $key === 0)) {
180+
if ($arg->name?->name === 'scope' || (!$arg->name instanceof \PhpParser\Node\Identifier && $key === 0)) {
181181
if ($arg->value instanceof ClassConstFetch) {
182182
if ($arg->value->class->getLast() === self::OPENAPI_ATTRIBUTE_CLASSNAME) {
183183
return self::getScopeNameFromConst($arg->value);

src/OpenApiType.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public static function resolve(string $context, array $definitions, ParamTagValu
213213
foreach ($node->items as $item) {
214214
$name = $item->keyName instanceof ConstExprStringNode ? $item->keyName->value : $item->keyName->name;
215215
$type = self::resolve($context . ': ' . $name, $definitions, $item->valueType);
216-
$comments = array_map(static fn (Comment $comment) => preg_replace('/^\/\/\s*/', '', $comment->text), $item->keyName->getAttribute(Attribute::COMMENTS) ?? []);
216+
$comments = array_map(static fn (Comment $comment): ?string => preg_replace('/^\/\/\s*/', '', $comment->text), $item->keyName->getAttribute(Attribute::COMMENTS) ?? []);
217217
if ($comments !== []) {
218218
$type->description = implode("\n", $comments);
219219
}
@@ -245,7 +245,7 @@ public static function resolve(string $context, array $definitions, ParamTagValu
245245
Logger::panic($context, "JSON objects can only be indexed by '" . implode("', '", $allowedTypes) . "' but got '" . $node->genericTypes[0]->name . "'");
246246
}
247247

248-
if ($node instanceof GenericTypeNode && $node->type->name == 'int' && count($node->genericTypes) == 2) {
248+
if ($node instanceof GenericTypeNode && $node->type->name === 'int' && count($node->genericTypes) == 2) {
249249
$min = null;
250250
$max = null;
251251
if ($node->genericTypes[0] instanceof ConstTypeNode) {
@@ -321,11 +321,11 @@ enum: $values,
321321
$items = [];
322322

323323
foreach ($node->types as $type) {
324-
if (($type instanceof IdentifierTypeNode || $type instanceof Identifier) && $type->name == 'null') {
324+
if (($type instanceof IdentifierTypeNode || $type instanceof Identifier) && $type->name === 'null') {
325325
$nullable = true;
326326
continue;
327327
}
328-
if (($type instanceof IdentifierTypeNode || $type instanceof Identifier) && $type->name == 'mixed') {
328+
if (($type instanceof IdentifierTypeNode || $type instanceof Identifier) && $type->name === 'mixed') {
329329
Logger::error($context, "Unions and intersections should not contain 'mixed'");
330330
}
331331
$items[] = self::resolve($context, $definitions, $type);
@@ -372,7 +372,7 @@ enum: $values,
372372

373373
if ($node instanceof ConstTypeNode && $node->constExpr instanceof ConstExprStringNode) {
374374
$value = $node->constExpr->value;
375-
if ($value == '') {
375+
if ($value === '') {
376376
// Not a valid enum
377377
return new OpenApiType(
378378
context: $context,

src/ResponseType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public static function resolve(string $context, TypeNode $obj): array {
220220
if ($responseType->hasContentTypeTemplate) {
221221
if ($args[$i] instanceof ConstTypeNode) {
222222
$contentTypes = [$args[$i]->constExpr->value];
223-
} elseif ($args[$i] instanceof IdentifierTypeNode && $args[$i]->name == 'string') {
223+
} elseif ($args[$i] instanceof IdentifierTypeNode && $args[$i]->name === 'string') {
224224
$contentTypes = ['*/*'];
225225
} elseif ($args[$i] instanceof UnionTypeNode) {
226226
$contentTypes = array_map(fn ($arg) => $arg->constExpr->value, $args[$i]->types);

tests/lib/Controller/SettingsController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ public function samePathPost(): DataResponse {
760760
* 200: Admin settings updated
761761
*/
762762
public function requestHeader(): DataResponse {
763-
$value = $this->request->getHeader('X-Custom-Header');
763+
$this->request->getHeader('X-Custom-Header');
764764

765765
return new DataResponse();
766766
}
@@ -773,8 +773,8 @@ public function requestHeader(): DataResponse {
773773
* 200: Admin settings updated
774774
*/
775775
public function requestParams(): DataResponse {
776-
$value = $this->request->getParam('some-param');
777-
$value = $this->request->getParam('some-param-with-explicit-default-value', 'abc');
776+
$this->request->getParam('some-param');
777+
$this->request->getParam('some-param-with-explicit-default-value', 'abc');
778778

779779
return new DataResponse();
780780
}

0 commit comments

Comments
 (0)