-
Notifications
You must be signed in to change notification settings - Fork 668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
psalm-if-this-is
enhancements
#7258
Comments
I found these snippets: https://psalm.dev/r/8f7f7985a6<?php
/**
* @template T
*/
final class Option
{
/**
* @return T|null
*/
public function unwrap()
{
throw new RuntimeException('???');
}
}
/**
* @template L
* @template R
*/
final class Either
{
/**
* @return R|null
*/
public function unwrap()
{
throw new RuntimeException('???');
}
}
/**
* @template T
*/
final class ArrayList
{
/** @var list<T> */
private $items;
/**
* @param list<T> $items
*/
public function __construct(array $items)
{
$this->items = $items;
}
/**
* @template A
* @template B
* @template TOption of Option<A>
* @template TEither of Either<mixed, B>
* @psalm-if-this-is ArrayList<TOption|TEither>
* @return ArrayList<A|B>
*/
public function compact(): ArrayList
{
$values = [];
foreach ($this->items as $item) {
$value = $item->unwrap();
if (null !== $value) {
$values[] = $value;
}
}
return new self($values);
}
}
/** @var ArrayList<Either<Exception, int>|Option<int>> $list */
$list = new ArrayList([]);
/** @psalm-trace $numbers */
$numbers = $list->compact();
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example raises many issues: https://psalm.dev/r/8f7f7985a6
All issues can be fixed if:
$this
variables will be inferred frompsalm-if-this-is
psalm-if-this-is
The text was updated successfully, but these errors were encountered: