Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Type object cannot be called as a class" after reflection assertion #5257

Closed
dbrekelmans opened this issue Feb 20, 2021 · 1 comment
Closed

Comments

@dbrekelmans
Copy link

I'm using Reflection to assert that a method exists on an object.
Therefor it should be completely safe to call the method on this object dynamically.

https://psalm.dev/r/6e92bd1315

Expected result
No error

Actual result
ERROR: UndefinedClass - 26:20 - Type object cannot be called as a class

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6e92bd1315
<?php

use ReflectionException;
use ReflectionMethod;
use RuntimeException;

use function get_class;
use function sprintf;

final class MethodExtractor
{
    /** @return mixed */
    public function extract(object $source, string $data)
    {
        try {
            $reflectionMethod = new ReflectionMethod(get_class($source), $data);
        } catch (ReflectionException $e) {
            throw new RuntimeException(sprintf('Method "%s" does not exist.', $data));
        }

        if (!$reflectionMethod->isPublic()) {
            throw new RuntimeException(sprintf('Method "%s" is not public.', $data));
        }

        if ($reflectionMethod->isStatic()) {
            return $source::$data();
        }

        return $source->$data();
    }
}
Psalm output (using commit 3106635):

ERROR: UndefinedClass - 26:20 - Type object cannot be called as a class

INFO: MixedMethodCall - 29:25 - Cannot determine the type of $source

@muglug muglug closed this as completed in 78577fd Feb 23, 2021
This was referenced Mar 15, 2021
This was referenced Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant