psalm-plugin-symfony icon indicating copy to clipboard operation
psalm-plugin-symfony copied to clipboard

DenormalizerInterface stub doesn't handle nulls

Open bendavies opened this issue 4 years ago • 0 comments

Hi there,

The following:

    public function denormalize($data, $type, $format = null, array $context = []): ?Money
    {
        if (null === $data) {
            return null;
        }

        if (!isset($data['amount'], $data['currency'])) {
            return null;
        }

        return $this->parser->parse((string) $data['amount'], new Currency($data['currency']));
    }

will trigger:

ERROR: InvalidNullableReturnType - MoneyNormalizer.php:45:85 - The declared return type 'Money\Money' for MoneyNormalizer::denormalize is not nullable, but 'Money\Money|null' contains null (see https://psalm.dev/144)
    public function denormalize($data, $type, $format = null, array $context = []): ?Money


ERROR: NullableReturnStatement - MoneyNormalizer.php:48:20 - The declared return type 'Money\Money' for MoneyNormalizer::denormalize is not nullable, but the function returns 'null' (see https://psalm.dev/139)
            return null;


ERROR: NullableReturnStatement - MoneyNormalizer.php:52:20 - The declared return type 'Money\Money' for MoneyNormalizer::denormalize is not nullable, but the function returns 'null' (see https://psalm.dev/139)
            return null;

Thanks

bendavies avatar Sep 03 '21 10:09 bendavies