Skip to content

Commit 0228643

Browse files
committed
Fix internal error with first-class callable in array_filter
1 parent 3bb222c commit 0228643

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Rules/Comparison/ImpossibleCheckTypeHelper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public function findSpecifiedType(
5959
): ?bool
6060
{
6161
if ($node instanceof FuncCall) {
62+
if ($node->isFirstClassCallable()) {
63+
return null;
64+
}
6265
$argsCount = count($node->getArgs());
6366
if ($node->name instanceof Node\Name) {
6467
$functionName = strtolower((string) $node->name);
@@ -361,6 +364,10 @@ private function determineContext(Scope $scope, Expr $node): TypeSpecifierContex
361364
return TypeSpecifierContext::createTruthy();
362365
}
363366

367+
if ($node instanceof Expr\CallLike && $node->isFirstClassCallable()) {
368+
return TypeSpecifierContext::createTruthy();
369+
}
370+
364371
if ($node instanceof FuncCall && $node->name instanceof Node\Name) {
365372
if ($this->reflectionProvider->hasFunction($node->name, $scope)) {
366373
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,18 @@ public function testBug9690(): void
12301230
$this->assertNoErrors($errors);
12311231
}
12321232

1233+
public function testBug9994(): void
1234+
{
1235+
if (PHP_VERSION_ID < 80100) {
1236+
$this->markTestSkipped('Test requires PHP 8.1.');
1237+
}
1238+
1239+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-9994.php');
1240+
$this->assertCount(2, $errors);
1241+
$this->assertSame('Negated boolean expression is always false.', $errors[0]->getMessage());
1242+
$this->assertSame('Parameter #2 $callback of function array_filter expects callable(1|2|3|null): mixed, false given.', $errors[1]->getMessage());
1243+
}
1244+
12331245
/**
12341246
* @param string[]|null $allAnalysedFiles
12351247
* @return Error[]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1); // lint >= 8.1
2+
3+
namespace Bug9994;
4+
5+
function (): void {
6+
7+
$arr = [
8+
1,
9+
2,
10+
3,
11+
null,
12+
];
13+
14+
15+
var_dump(array_filter($arr, !is_null(...)));
16+
};

0 commit comments

Comments
 (0)