Skip to content

Wrong type when passing constants #10

Description

@rieschl

When passing a (class) constant to the get function of the container, it's impossible to derive a class from it, so we add a type hint to the variable.
But apparently PHPStan (in conjunction with your package) thinks that the value of the constant is a class and complains that the manually added type is not a subtype of it.
E.g.:

<?php

declare(strict_types=1);

use Psr\Container\ContainerInterface;

final readonly class SomeServiceFactory
{
    private const string TAX_RATE = 'TaxRate';

    public function __invoke(ContainerInterface $container): SomeService
    {
        /** @var float $taxRate */
        $taxRate = $container->get(self::TAX_RATE);

        return new SomeService($taxRate);
    }
}

PHPStan emits the error PHPDoc tag @var with type float is not subtype of type TaxRate..

Instead, if I use the string literal directly ($container->get('TaxRate')), there are no errors.

Thanks for your help!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions