Skip to content

Commit b551095

Browse files
committed
For @var above throw and return, change the type of expr only for the stmt callback
1 parent 1f421d4 commit b551095

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/Analyser/NodeScopeResolver.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,6 @@ private function processStmtNode(
355355
): StatementResult
356356
{
357357
if (
358-
$stmt instanceof Throw_
359-
|| $stmt instanceof Return_
360-
) {
361-
$scope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr, $nodeCallback);
362-
} elseif (
363358
!$stmt instanceof Static_
364359
&& !$stmt instanceof Foreach_
365360
&& !$stmt instanceof Node\Stmt\Global_
@@ -392,7 +387,12 @@ private function processStmtNode(
392387
}
393388
}
394389

395-
$nodeCallback($stmt, $scope);
390+
$stmtScope = $scope;
391+
if ($stmt instanceof Throw_ || $stmt instanceof Return_) {
392+
$stmtScope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr, $nodeCallback);
393+
}
394+
395+
$nodeCallback($stmt, $stmtScope);
396396

397397
$overridingThrowPoints = $this->getOverridingThrowPoints($stmt, $scope);
398398

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,14 @@ public function testBug5091(): void
11761176
$this->assertNoErrors($errors);
11771177
}
11781178

1179+
public function testBug9459(): void
1180+
{
1181+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-9459.php');
1182+
$this->assertCount(1, $errors);
1183+
$this->assertSame('PHPDoc tag @var with type callable(): array is not subtype of native type Closure(): array{}.', $errors[0]->getMessage());
1184+
$this->assertSame(10, $errors[0]->getLine());
1185+
}
1186+
11791187
public function testDiscussion9053(): void
11801188
{
11811189
if (PHP_VERSION_ID < 80000) {
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug9459;
4+
5+
class HelloWorld
6+
{
7+
public function sayHello(): callable
8+
{
9+
/** @var callable(): mixed[] */
10+
return function (): array { return []; };
11+
}
12+
}

0 commit comments

Comments
 (0)