Skip to content

Type-hinting abstract classes doesn't work as expected #4471

@Norgul

Description

@Norgul

Bug report

I am trying to type hint an abstract class in the code.

Code snippet that reproduces the problem

I am using PHPStan with --level=0 in my configuration. The part of code in question:

protected function instantiateRequestParameter(string $requestParameter): AbstractParameter
{
    /**
     * @var AbstractParameter $requestParameter
     */
    $input = $this->wrapInput($requestParameter::getParameterName());

    return new $requestParameter($input, $this->builder, $this->modelConfig);
}

Input comes from doing a foreach loop on registered parameters from config file. Each of these is an exact class which extends abstract class AbstractParameter.

The error I'm getting is:

Instantiated class Asseco\JsonQueryBuilder\RequestParameters\AbstractParameter is abstract

Once I remove the type hint, it all goes well. Can this be somehow mitigated not to show while at the same time having a type hint? I tried replacing it with interface, however in that case I am getting:

Cannot instantiate interface Asseco\JsonQueryBuilder\RequestParameters\Parameter

So in both cases it is the same. Any ideas?

Expected output

I expect it not to fail as I am never forwarding an abstract class to the method.

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