Skip to content

Commit 22d363c

Browse files
committed
Inline @var - support overriding variables on right side of =
1 parent 4b2854d commit 22d363c

8 files changed

Lines changed: 90 additions & 4 deletions

File tree

src/Analyser/NodeScopeResolver.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,6 @@ private function processStmtNode(
343343
} elseif (
344344
!$stmt instanceof Static_
345345
&& !$stmt instanceof Foreach_
346-
&& (
347-
!$stmt instanceof Node\Stmt\Expression
348-
|| !$stmt->expr instanceof Assign && !$stmt->expr instanceof AssignRef
349-
)
350346
) {
351347
$scope = $this->processStmtVarAnnotation($scope, $stmt, null);
352348
}
@@ -2734,12 +2730,27 @@ private function processStmtVarAnnotation(MutatingScope $scope, Node\Stmt $stmt,
27342730
$function !== null ? $function->getName() : null,
27352731
$comment->getText()
27362732
);
2733+
2734+
$assignedVariable = null;
2735+
if (
2736+
$stmt instanceof Node\Stmt\Expression
2737+
&& ($stmt->expr instanceof Assign || $stmt->expr instanceof AssignRef)
2738+
&& $stmt->expr->var instanceof Variable
2739+
&& is_string($stmt->expr->var->name)
2740+
) {
2741+
$assignedVariable = $stmt->expr->var->name;
2742+
}
2743+
27372744
foreach ($resolvedPhpDoc->getVarTags() as $name => $varTag) {
27382745
if (is_int($name)) {
27392746
$variableLessTags[] = $varTag;
27402747
continue;
27412748
}
27422749

2750+
if ($name === $assignedVariable) {
2751+
continue;
2752+
}
2753+
27432754
$certainty = $scope->hasVariableType($name);
27442755
if ($certainty->no()) {
27452756
continue;

tests/PHPStan/Rules/Operators/InvalidBinaryOperationRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,9 @@ public function testBug2964(): void
117117
$this->analyse([__DIR__ . '/data/bug2964.php'], []);
118118
}
119119

120+
public function testBug3515(): void
121+
{
122+
$this->analyse([__DIR__ . '/data/bug-3515.php'], []);
123+
}
124+
120125
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$foo = 'foo';
4+
5+
/** @var int $foo */
6+
$bar = $foo + 1;
7+
8+
function (): void {
9+
$foo = 'foo';
10+
11+
/** @var int $foo */
12+
$bar = $foo + 1;
13+
};

tests/PHPStan/Rules/PhpDoc/WrongVariableNameInVarTagRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,9 @@ public function testAboveDeclare(): void
100100
$this->analyse([__DIR__ . '/data/var-above-declare.php'], []);
101101
}
102102

103+
public function testBug3515(): void
104+
{
105+
$this->analyse([__DIR__ . '/data/bug-3515.php'], []);
106+
}
107+
103108
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
/** @var int $foo */
4+
$bar = $foo + 1;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,12 @@ public function thisInVar2()
188188
$demo = $this->demo();
189189
}
190190

191+
public function overrideDifferentVariableAboveAssign()
192+
{
193+
$foo = 'foo';
194+
195+
/** @var int $foo */
196+
$bar = $foo + 1;
197+
}
198+
191199
}

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,4 +865,23 @@ public function testBug1306(): void
865865
$this->analyse([__DIR__ . '/data/bug-1306.php'], []);
866866
}
867867

868+
public function testBug3515(): void
869+
{
870+
$this->cliArgumentsVariablesRegistered = true;
871+
$this->polluteScopeWithLoopInitialAssignments = false;
872+
$this->polluteCatchScopeWithTryAssignments = false;
873+
$this->checkMaybeUndefinedVariables = true;
874+
$this->polluteScopeWithAlwaysIterableForeach = true;
875+
$this->analyse([__DIR__ . '/data/bug-3515.php'], [
876+
[
877+
'Undefined variable: $anArray',
878+
19,
879+
],
880+
[
881+
'Undefined variable: $anArray',
882+
20,
883+
],
884+
]);
885+
}
886+
868887
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Bug3515;
4+
5+
/**
6+
* @var mixed[] $anArray
7+
*/
8+
$value1 = $anArray[0];
9+
$value2 = $anArray[1];
10+
11+
/** @var int $foo */
12+
$bar = $foo + 1;
13+
14+
function (): void
15+
{
16+
/**
17+
* @var mixed[] $anArray
18+
*/
19+
$value1 = $anArray[0];
20+
$value2 = $anArray[1];
21+
};

0 commit comments

Comments
 (0)