Type\not_null()
Is your feature request related to a problem? Please describe.
Quite often I find myself using Type\instance_of to assert values are not null, for example...
/** @var null|Foo $x */
$x = getSomeValueOrAccessAPropertyOrMethodEtc();
$x = Type\instance_of(Foo::class)->assert($x);
Here I'm essentially duplicating the expected type (Foo::class), because it's usually already declared on the property/method etc... but is nullable.
Describe the solution you'd like
It would be more concise to be able to say...
$x = Type\not_null()->assert($x);
Which would assert that $x is Foo and not null|Foo.
Describe alternatives you've considered
I've presented my current solution for this above, I'm not aware of any other features of PSL which could be used here (apologies if I've missed something).
There is Psl\invariant:
invariant($x !== null, 'must not be null');
And indeed - I also personally prefer to use not null assertion other than it should be of this type.
this won't be possible, as psalm doesn't have a type that includes everything but not null 🤔
The not_null type exists - yet there is no type narrowing possible in psalm (without a plugin). Is that sufficient or should we keep this open @azjezz ?
we could close this issue, and open a new one in the plugin repository to add support for type narrowing when Type\not_null() is used?
wdyt @veewee ?