Skip to content
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
6 changes: 3 additions & 3 deletions generate-spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
$controllerFiles = [];
foreach ($iterator as $file) {
$filePath = $file->getPathname();
if (!str_ends_with($filePath, 'Controller.php')) {
if (!str_ends_with((string)$filePath, 'Controller.php')) {
continue;
}
$controllerFiles[] = $filePath;
Expand All @@ -263,7 +263,7 @@

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

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

/** @var AttributeGroup $attrGroup */
foreach ($classMethod->attrGroups as $attrGroup) {
Expand Down
4 changes: 2 additions & 2 deletions merge-specs.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function loadSpec(string $path): object {
}

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

function rewriteRefs(object $spec): object {
Expand Down Expand Up @@ -155,7 +155,7 @@ function rewriteSchemaNames(object $spec): array {
}

function rewriteTags(object $spec): array {
return array_map(static function (object $tag) use ($spec) {
return array_map(static function (object $tag) use ($spec): object {
$tag->name = getAppID($spec) . '/' . $tag->name;
return $tag;
}, $spec->tags);
Expand Down
18 changes: 9 additions & 9 deletions src/ControllerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ public static function parse(string $context,

foreach ($docNodes as $docNode) {
if ($docNode instanceof PhpDocTextNode) {
$nodeDescription = (string)$docNode->text;
$nodeDescription = $docNode->text;
} elseif ($docNode->value instanceof GenericTagValueNode) {
$nodeDescription = (string)$docNode->value;
} else {
$nodeDescription = (string)$docNode->value->description;
}

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

// Parse in blocks (separate by double newline) to preserve newlines within a block.
$nodeDescriptionBlocks = preg_split("/\n\s*\n/", $nodeDescription);
foreach ($nodeDescriptionBlocks as $nodeDescriptionBlock) {
$methodDescriptionBlockLines = [];
foreach (array_filter(explode("\n", $nodeDescriptionBlock), static fn (string $line) => trim($line) !== '') as $line) {
foreach (array_filter(explode("\n", $nodeDescriptionBlock), static fn (string $line): bool => trim($line) !== '') as $line) {
if (preg_match(self::STATUS_CODE_DESCRIPTION_PATTERN, $line)) {
$parts = preg_split(self::STATUS_CODE_DESCRIPTION_PATTERN, $line, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$responseDescriptions[(int)$parts[0]] = trim($parts[1]);
Expand Down Expand Up @@ -133,7 +133,7 @@ public static function parse(string $context,
Logger::error($context, "Missing description for exception '" . $type . "'");
} else {
// Only add lines that don't match the status code pattern to the description
$responseDescriptions[$statusCode] = implode("\n", array_filter($nodeDescriptionLines, static fn (string $line) => !preg_match(self::STATUS_CODE_DESCRIPTION_PATTERN, $line)));
$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)));
}

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

if ($docParameterName == $methodParameterName) {
if ($docParameterType == '@param') {
if ($docParameterType === '@param') {
$paramTag = $docParameter;
} elseif ($docParameterType == '@psalm-param') {
} elseif ($docParameterType === '@psalm-param') {
$psalmParamTag = $docParameter;
} else {
Logger::panic($context . ': @param', 'Unknown param type ' . $docParameterType);
Expand All @@ -190,7 +190,7 @@ public static function parse(string $context,
$description = '';
}
// Only keep lines that don't match the status code pattern in the description
$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))));
$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))));

if ($paramTag instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode && $psalmParamTag instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode) {
try {
Expand Down Expand Up @@ -250,7 +250,7 @@ public static function parse(string $context,
continue;
}

if (str_contains($param->type->description, '@deprecated')) {
if (str_contains((string)$param->type->description, '@deprecated')) {
$param->type->deprecated = true;
$param->type->description = str_replace('@deprecated', 'Deprecated:', $param->type->description);
}
Expand Down Expand Up @@ -298,7 +298,7 @@ public static function parse(string $context,
if ($methodCall->name->name === 'getParam') {
$name = $methodCall->args[0]->value->value;

if (preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name)) {
if (preg_match('/^[a-zA-Z]\w*$/', (string)$name)) {
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.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static function cleanSchemaName(string $name): string {
}

protected static function getScopeNameFromAttributeArgument(Arg $arg, int $key, string $routeName): ?string {
if ($arg->name?->name === 'scope' || ($arg->name === null && $key === 0)) {
if ($arg->name?->name === 'scope' || (!$arg->name instanceof \PhpParser\Node\Identifier && $key === 0)) {
if ($arg->value instanceof ClassConstFetch) {
if ($arg->value->class->getLast() === self::OPENAPI_ATTRIBUTE_CLASSNAME) {
return self::getScopeNameFromConst($arg->value);
Expand Down
10 changes: 5 additions & 5 deletions src/OpenApiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static function resolve(string $context, array $definitions, ParamTagValu
foreach ($node->items as $item) {
$name = $item->keyName instanceof ConstExprStringNode ? $item->keyName->value : $item->keyName->name;
$type = self::resolve($context . ': ' . $name, $definitions, $item->valueType);
$comments = array_map(static fn (Comment $comment) => preg_replace('/^\/\/\s*/', '', $comment->text), $item->keyName->getAttribute(Attribute::COMMENTS) ?? []);
$comments = array_map(static fn (Comment $comment): ?string => preg_replace('/^\/\/\s*/', '', $comment->text), $item->keyName->getAttribute(Attribute::COMMENTS) ?? []);
if ($comments !== []) {
$type->description = implode("\n", $comments);
}
Expand Down Expand Up @@ -245,7 +245,7 @@ public static function resolve(string $context, array $definitions, ParamTagValu
Logger::panic($context, "JSON objects can only be indexed by '" . implode("', '", $allowedTypes) . "' but got '" . $node->genericTypes[0]->name . "'");
}

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

foreach ($node->types as $type) {
if (($type instanceof IdentifierTypeNode || $type instanceof Identifier) && $type->name == 'null') {
if (($type instanceof IdentifierTypeNode || $type instanceof Identifier) && $type->name === 'null') {
$nullable = true;
continue;
}
if (($type instanceof IdentifierTypeNode || $type instanceof Identifier) && $type->name == 'mixed') {
if (($type instanceof IdentifierTypeNode || $type instanceof Identifier) && $type->name === 'mixed') {
Logger::error($context, "Unions and intersections should not contain 'mixed'");
}
$items[] = self::resolve($context, $definitions, $type);
Expand Down Expand Up @@ -372,7 +372,7 @@ enum: $values,

if ($node instanceof ConstTypeNode && $node->constExpr instanceof ConstExprStringNode) {
$value = $node->constExpr->value;
if ($value == '') {
if ($value === '') {
// Not a valid enum
return new OpenApiType(
context: $context,
Expand Down
2 changes: 1 addition & 1 deletion src/ResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static function resolve(string $context, TypeNode $obj): array {
if ($responseType->hasContentTypeTemplate) {
if ($args[$i] instanceof ConstTypeNode) {
$contentTypes = [$args[$i]->constExpr->value];
} elseif ($args[$i] instanceof IdentifierTypeNode && $args[$i]->name == 'string') {
} elseif ($args[$i] instanceof IdentifierTypeNode && $args[$i]->name === 'string') {
$contentTypes = ['*/*'];
} elseif ($args[$i] instanceof UnionTypeNode) {
$contentTypes = array_map(fn ($arg) => $arg->constExpr->value, $args[$i]->types);
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ public function samePathPost(): DataResponse {
* 200: Admin settings updated
*/
public function requestHeader(): DataResponse {
$value = $this->request->getHeader('X-Custom-Header');
$this->request->getHeader('X-Custom-Header');

return new DataResponse();
}
Expand All @@ -773,8 +773,8 @@ public function requestHeader(): DataResponse {
* 200: Admin settings updated
*/
public function requestParams(): DataResponse {
$value = $this->request->getParam('some-param');
$value = $this->request->getParam('some-param-with-explicit-default-value', 'abc');
$this->request->getParam('some-param');
$this->request->getParam('some-param-with-explicit-default-value', 'abc');

return new DataResponse();
}
Expand Down
Loading