php-standard-library / psalm-plugin

Psalm integration for the PHP Standard Library
MIT License
24 stars 6 forks source link

support for non-empty-array/non-empty-list #2

Closed azjezz closed 3 years ago

azjezz commented 3 years ago

first reported in https://github.com/azjezz/psl/issues/145 by @bendavies

Hi there!

Is your feature request related to a problem? Please describe.

Is is feasible to

  1. add non-empty-array and non-empty-list support to the type system, such that:
  2. the return Psl\Iter\first and other functions can be T and not T|null?

Describe the solution you'd like I want to be able to do things like:

<?php

declare(strict_types=1);

use Psl;

class FooExtractor
{
   /** @param list<Foo> $foos */
   public function __invoke(array $foos): Foo
   {
       $foosFiltered = Vec\filter(
           $foos,
           function (Foo $foo): bool {
               ...
           }
       );

       //assert $foosFiltered type is non-empty-list/array here.

       //this will error as the return type is `T|null`
       return Iter\first($foosFiltered);
   }
}

Is there a better way to achieve what I'm trying to do?

Thanks!