Skip to content

Commit adb41f9

Browse files
committed
Support @var above echo for the statement itself
1 parent f4583a2 commit adb41f9

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

src/Analyser/NodeScopeResolver.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ private function processStmtNode(
301301
\Closure $nodeCallback
302302
): StatementResult
303303
{
304+
if ($stmt instanceof Echo_) {
305+
$scope = $this->processStmtVarAnnotation($scope, $stmt);
306+
}
307+
304308
$nodeCallback($stmt, $scope);
305309

306310
if ($stmt instanceof Node\Stmt\Declare_) {
@@ -415,7 +419,6 @@ private function processStmtNode(
415419
), $methodScope);
416420
}
417421
} elseif ($stmt instanceof Echo_) {
418-
$scope = $this->processStmtVarAnnotation($scope, $stmt);
419422
$hasYield = false;
420423
foreach ($stmt->exprs as $echoExpr) {
421424
$result = $this->processExprNode($echoExpr, $scope, $nodeCallback, ExpressionContext::createDeep());

tests/PHPStan/Rules/Cast/EchoRuleTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ public function testEchoRule(): void
2424
$this->analyse([__DIR__ . '/data/echo.php'], [
2525
[
2626
'Parameter #1 (array()) of echo cannot be converted to string.',
27-
5,
27+
7,
2828
],
2929
[
3030
'Parameter #1 (stdClass) of echo cannot be converted to string.',
31-
7,
31+
9,
3232
],
3333
[
3434
'Parameter #1 (array()) of echo cannot be converted to string.',
35-
9,
35+
11,
3636
],
3737
[
3838
'Parameter #2 (stdClass) of echo cannot be converted to string.',
39-
9,
39+
11,
4040
],
4141
[
4242
'Parameter #1 (Closure(): mixed) of echo cannot be converted to string.',
43-
11,
43+
13,
4444
],
4545
[
4646
'Parameter #1 (\'string\'|array(\'string\')) of echo cannot be converted to string.',
47-
15,
47+
17,
4848
],
4949
]);
5050
}

tests/PHPStan/Rules/Cast/data/echo.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
declare(strict_types=1);
44

5-
echo [];
5+
function ()
6+
{
7+
echo [];
68

7-
echo new stdClass();
9+
echo new stdClass();
810

9-
echo [], new stdClass();
11+
echo [], new stdClass();
1012

11-
echo function () {};
13+
echo function () {};
1214

13-
echo 13132, 'string';
15+
echo 13132, 'string';
1416

15-
echo random_int(0, 1) ? ['string'] : 'string';
17+
echo random_int(0, 1) ? ['string'] : 'string';
18+
19+
};
20+
21+
function (array $test)
22+
{
23+
/** @var string $test */
24+
echo $test;
25+
};

tests/PHPStan/Rules/PhpDoc/WrongVariableNameInVarTagRuleTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function testRule(): void
7070
'Multiple PHPDoc @var tags above single variable assignment are not supported.',
7171
125,
7272
],
73+
[
74+
'Variable $b in PHPDoc tag @var does not exist.',
75+
134,
76+
],
7377
]);
7478
}
7579

tests/PHPStan/Rules/PhpDoc/data/wrong-variable-name-var.php

+9
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,13 @@ public function multiplePrefixedTagsAreFine()
125125
$test = doFoo(); // error
126126
}
127127

128+
public function testEcho($a)
129+
{
130+
/** @var string $a */
131+
echo $a;
132+
133+
/** @var string $b */
134+
echo $a;
135+
}
136+
128137
}

0 commit comments

Comments
 (0)