Skip to content

Commit

Permalink
Fix internal error with first-class callable in array_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 17, 2023
1 parent 3bb222c commit 0228643
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function findSpecifiedType(
): ?bool
{
if ($node instanceof FuncCall) {
if ($node->isFirstClassCallable()) {
return null;
}
$argsCount = count($node->getArgs());
if ($node->name instanceof Node\Name) {
$functionName = strtolower((string) $node->name);
Expand Down Expand Up @@ -361,6 +364,10 @@ private function determineContext(Scope $scope, Expr $node): TypeSpecifierContex
return TypeSpecifierContext::createTruthy();
}

if ($node instanceof Expr\CallLike && $node->isFirstClassCallable()) {
return TypeSpecifierContext::createTruthy();
}

if ($node instanceof FuncCall && $node->name instanceof Node\Name) {
if ($this->reflectionProvider->hasFunction($node->name, $scope)) {
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,18 @@ public function testBug9690(): void
$this->assertNoErrors($errors);
}

public function testBug9994(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$errors = $this->runAnalyse(__DIR__ . '/data/bug-9994.php');
$this->assertCount(2, $errors);
$this->assertSame('Negated boolean expression is always false.', $errors[0]->getMessage());
$this->assertSame('Parameter #2 $callback of function array_filter expects callable(1|2|3|null): mixed, false given.', $errors[1]->getMessage());
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/bug-9994.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1); // lint >= 8.1

namespace Bug9994;

function (): void {

$arr = [
1,
2,
3,
null,
];


var_dump(array_filter($arr, !is_null(...)));
};

0 comments on commit 0228643

Please sign in to comment.