Skip to content

Commit

Permalink
For @var above throw and return, change the type of expr only for t…
Browse files Browse the repository at this point in the history
…he stmt callback
  • Loading branch information
ondrejmirtes committed Jun 18, 2023
1 parent 1f421d4 commit b551095
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,6 @@ private function processStmtNode(
): StatementResult
{
if (
$stmt instanceof Throw_
|| $stmt instanceof Return_
) {
$scope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr, $nodeCallback);
} elseif (
!$stmt instanceof Static_
&& !$stmt instanceof Foreach_
&& !$stmt instanceof Node\Stmt\Global_
Expand Down Expand Up @@ -392,7 +387,12 @@ private function processStmtNode(
}
}

$nodeCallback($stmt, $scope);
$stmtScope = $scope;
if ($stmt instanceof Throw_ || $stmt instanceof Return_) {
$stmtScope = $this->processStmtVarAnnotation($scope, $stmt, $stmt->expr, $nodeCallback);
}

$nodeCallback($stmt, $stmtScope);

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

Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,14 @@ public function testBug5091(): void
$this->assertNoErrors($errors);
}

public function testBug9459(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-9459.php');
$this->assertCount(1, $errors);
$this->assertSame('PHPDoc tag @var with type callable(): array is not subtype of native type Closure(): array{}.', $errors[0]->getMessage());
$this->assertSame(10, $errors[0]->getLine());
}

public function testDiscussion9053(): void
{
if (PHP_VERSION_ID < 80000) {
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/data/bug-9459.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace Bug9459;

class HelloWorld
{
public function sayHello(): callable
{
/** @var callable(): mixed[] */
return function (): array { return []; };
}
}

0 comments on commit b551095

Please sign in to comment.