Skip to content

Type\nonnull() is not narrowed by phpstan #486

@bendavies

Description

@bendavies

Describe the bug
Type\nonnull() is not narrowed by phpstan

To Reproduce

<?php

require __DIR__.'/../vendor/autoload.php';

use Psl\Type;

/**
 * @template T
 *
 * @param T|null $value
 *
 * @ara-assert nonnull $value
 *
 * @psalm-assert T $value
 *
 * @ara-return nonnull
 *
 * @return ($value is null ? never : T)
 */
function assertNotNull(mixed $value): mixed
{

}

function a(?int $i): void
{
    \PHPStan\dumpType($i);
    \PHPStan\dumpType(assertNotNull($i));
}

function b(?int $i): void
{
    \PHPStan\dumpType($i);
    \PHPStan\dumpType(Type\nonnull()->assert($i));
}

Expected behavior
First method shows a way (provided by @ondrejmirtes) that this can work in phpstan.
The second method use Type\nonnull().

The dumped types here should be
int|null followed by int for both dumpTypes. but we get:

  27     Dumped type: int|null
  28     Dumped type: int
  33     Dumped type: int|null
  34     Dumped type: mixed

The Psl version of the function "narrows" int|null to mixed, not int

Metadata

Metadata

Assignees

Labels

Type: BugInconsistencies or issues which will cause an issue or problem for users or implementors.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions