Skip to content

Allow type extensions to not give a specific answer to getTypeFrom*Call() #5855

@matthiasnoback

Description

@matthiasnoback

Feature request

I'm working on my first type extension, and one thing that I run into, just like described in the docs (https://phpstan.org/developing-extensions/dynamic-return-type-extensions) is that I can't always give a good answer to the question "what is the return type here". In this case I don't want to give the standard answer, i.e..

        return \PHPStan\Reflection\ParametersAcceptorSelector::selectFromArgs(
            $scope,
            $methodCall->getArgs(),
            $methodReflection->getVariants()
        )->getReturnType();

As an alternative I just want to return null if my extension doesn't know it. I can imagine this saves some duplication in type extensions as well, and it makes it easier to implement one using early returns, e.g.

    public function getTypeFromMethodCall(
        MethodReflection $methodReflection,
        MethodCall $methodCall,
        Scope $scope
    ): Type {
        if (count($methodCall->getArgs()) === 0)) {
            return null;
        }
        
        $firstArgumentType = $scope->getType($methodCall->args[0]->value);

        if (!$firstArgumentType instanceof ObjectType) {
            return null;
        }
        
        $handlerClass = $firstArgumentType->getClassName() . 'Handler';
        try {
            $class = $this->reflectionProvider->getClass($handlerClass);
        } catch (ClassNotFoundException $exception) {
            return null;
        }
        
        // return the actual type
   }

Did PHPStan help you today? Did it make you happy in any way?

Absolutely, I'm having a lot of fun with it, and am in awe of its power and level of understanding of PHP code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions