Skip to content

Incorrectly inferred return type for Psl\Iter\first(...) #12

@evang522

Description

@evang522

Describe the bug
When provided an array with type list<string>, the output of Psl\Iter\first($array) is considered by the plugin's FunctionReturnTypeProvider to be never null, which is presumably not true.

To Reproduce

Run Psalm with this plugin enabled on the following code:

/** @var list<string> $series  **/
$series = ['val1', 'val2'];

$firstInSeries = \Psl\Iter\first($series);

\assert($firstInSeries !== null); // assert with any assertion module you like, it's a similar output for all.

Output:

ERROR: RedundantConditionGivenDocblockType - ../../test.php:32:9 - Docblock-defined type string can never contain null (see https://psalm.dev/156)
\assert($firstInSeries !== null);

When duplicating the code of \Psl\Iter\first so that the plugin is not applied, this problem does not occur:

/**
 * @template T
 *
 * @param iterable<T> $iterable
 *
 * @return T|null
 */
function first(iterable $iterable)
{
    foreach ($iterable as $v) {
        return $v;
    }

    return null;
}



/** @var list<string> $series  **/
$series = ['val1', 'val2'];

$firstInSeries = first($series);

\assert($firstInSeries !== null);

Output: No errors found!

Expected behavior
\Psl\Iter\first return type is considered to be possibly null as long as the array is not non-empty-list<*>

Environment (please complete the following information):

  • OS: fedora
  • PHP version: 8.1
  • Version: 2.2.1

Thanks a lot!

Metadata

Metadata

Assignees

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