Skip to content

ExtDs Map::get() dynamic throws #9066

@simPod

Description

@simPod

Feature request

I wrote this extension to have better throws inference from \Ds\Map::get(). I wonder if there's a way to somehow make it into phpstan's core so I don't have to copy this extension into every project?

use Ds\Map;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodThrowTypeExtension;
use PHPStan\Type\Type;

use function count;

final class ExtDsThrowExtension implements DynamicMethodThrowTypeExtension
{
    public function isMethodSupported(MethodReflection $methodReflection): bool
    {
        return $methodReflection->getDeclaringClass()->getName() === Map::class
            && $methodReflection->getName() === 'get';
    }

    public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type|null
    {
        if (count($methodCall->args) < 2) {
            return $methodReflection->getThrowType();
        }

        return null;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions