Skip to content

Wrong scope indent reported in anonymous class #1109

Closed
@ondrejmirtes

Description

@ondrejmirtes

With this code sample:

    public function testDynamicMethodReturnTypeExtensions(
        string $typeClass,
        bool $nullable,
        string $class = null,
        string $expression
    )
    {
        $this->assertTypes(
            __DIR__ . '/data/dynamic-method-return-types.php',
            $typeClass,
            $nullable,
            $class,
            $expression,
            [
                new class() implements DynamicMethodReturnTypeExtension {

                    public static function getClass(): string
                    {
                        return \DynamicMethodReturnTypesNamespace\EntityManager::class;
                    }

                    public function isMethodSupported(MethodReflection $methodReflection): bool
                    {
                        return in_array($methodReflection->getName(), ['getByPrimary'], true);
                    }

                    public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): \PHPStan\Type\Type
                    {
                        $args = $methodCall->args;
                        if (count($args) === 0) {
                            return $methodReflection->getReturnType();
                        }

                        $arg = $args[0]->value;
                        if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
                            return $methodReflection->getReturnType();
                        }

                        if (!($arg->class instanceof \PhpParser\Node\Name)) {
                            return $methodReflection->getReturnType();
                        }

                        return new ObjectType((string) $arg->class, false);
                    }
                },
            ]
        );
    }

CodeSniffer reports wrong indentation on some lines in the anonymous class. It stops complaining when I format the code like this:

    public function testDynamicMethodReturnTypeExtensions(
        string $typeClass,
        bool $nullable,
        string $class = null,
        string $expression
    )
    {
        $this->assertTypes(
            __DIR__ . '/data/dynamic-method-return-types.php',
            $typeClass,
            $nullable,
            $class,
            $expression,
            [
                new class() implements DynamicMethodReturnTypeExtension {

            public static function getClass(): string
            {
                return \DynamicMethodReturnTypesNamespace\EntityManager::class;
            }

            public function isMethodSupported(MethodReflection $methodReflection): bool
            {
                return in_array($methodReflection->getName(), ['getByPrimary'], true);
            }

            public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): \PHPStan\Type\Type
            {
                $args = $methodCall->args;
                if (count($args) === 0) {
                    return $methodReflection->getReturnType();
                }

                $arg = $args[0]->value;
                if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
                    return $methodReflection->getReturnType();
                }

                if (!($arg->class instanceof \PhpParser\Node\Name)) {
                    return $methodReflection->getReturnType();
                }

                return new ObjectType((string) $arg->class, false);
            }
                },
            ]
        );

Which looks wrong to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions