Skip to content

Commit

Permalink
Fix filtering with first class callable
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon committed Sep 26, 2024
1 parent 7689d95 commit 6aff6fe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Type/PhpOption/FilterReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
Expand Down Expand Up @@ -69,6 +70,17 @@ public function getTypeFromMethodCall(
},
[new Arg($var)],
);
} elseif (
(
$callbackArg instanceof FuncCall
|| $callbackArg instanceof MethodCall
|| $callbackArg instanceof StaticCall
)
&& $callbackArg->isFirstClassCallable()
) {
$var = new Variable('value');
$expr = clone $callbackArg;
$expr->args = [new Arg($var)];
} else {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Type/PhpOption/data/option-filter-not.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
/**
* @var \PhpOption\Option<string|null> $option
*/
assertType('PhpOption\Option<null>', $option->filterNot('is_string'));
assertType('PhpOption\Option<null>', $option->filterNot(static fn ($value) => is_string($value)));
assertType('PhpOption\Option<string>', $option->filterNot('is_null'));
assertType('PhpOption\Option<string>', $option->filterNot(static fn ($value) => is_null($value)));
assertType('PhpOption\Option<string>', $option->filterNot(is_null(...)));
1 change: 1 addition & 0 deletions tests/Type/PhpOption/data/option-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
*/
assertType('PhpOption\Option<string>', $option->filter('is_string'));
assertType('PhpOption\Option<string>', $option->filter(static fn ($value) => is_string($value)));
assertType('PhpOption\Option<string>', $option->filter(is_string(...)));

0 comments on commit 6aff6fe

Please sign in to comment.