phpstan-beberlei-assert icon indicating copy to clipboard operation
phpstan-beberlei-assert copied to clipboard

Chainable types do not work

Open bendavies opened this issue 5 years ago • 0 comments

the following code:

<?php

declare(strict_types=1);

use Assert\Assert;

class Result
{
    private ?float $percent;

    private function __construct()
    {
    }

    /** @param array<string, mixed> $payload */
    public static function fromQueryResults(array $payload): self
    {
        Assert::that($payload['percent'])
            ->nullOr()
            ->string()
            ->numeric();

        $floatOrNull = static fn (?string $value): ?float => null === $value ? null : (float) $value;

        $instance = new self();
        $instance->percent = $floatOrNull($payload['percent']);

        return $instance;
    }
}

Produces the error: Parameter #1 $value of closure expects string|null, float|int|string|null given.

The type of $payload['percent'] should be infered to string|null, not float|int|string|null

bendavies avatar Jun 07 '20 19:06 bendavies