Skip to content

Commit

Permalink
Inline @var - support overriding variables on right side of =
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 14, 2021
1 parent 4b2854d commit 22d363c
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,6 @@ private function processStmtNode(
} elseif (
!$stmt instanceof Static_
&& !$stmt instanceof Foreach_
&& (
!$stmt instanceof Node\Stmt\Expression
|| !$stmt->expr instanceof Assign && !$stmt->expr instanceof AssignRef
)
) {
$scope = $this->processStmtVarAnnotation($scope, $stmt, null);
}
Expand Down Expand Up @@ -2734,12 +2730,27 @@ private function processStmtVarAnnotation(MutatingScope $scope, Node\Stmt $stmt,
$function !== null ? $function->getName() : null,
$comment->getText()
);

$assignedVariable = null;
if (
$stmt instanceof Node\Stmt\Expression
&& ($stmt->expr instanceof Assign || $stmt->expr instanceof AssignRef)
&& $stmt->expr->var instanceof Variable
&& is_string($stmt->expr->var->name)
) {
$assignedVariable = $stmt->expr->var->name;
}

foreach ($resolvedPhpDoc->getVarTags() as $name => $varTag) {
if (is_int($name)) {
$variableLessTags[] = $varTag;
continue;
}

if ($name === $assignedVariable) {
continue;
}

$certainty = $scope->hasVariableType($name);
if ($certainty->no()) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@ public function testBug2964(): void
$this->analyse([__DIR__ . '/data/bug2964.php'], []);
}

public function testBug3515(): void
{
$this->analyse([__DIR__ . '/data/bug-3515.php'], []);
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Operators/data/bug-3515.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$foo = 'foo';

/** @var int $foo */
$bar = $foo + 1;

function (): void {
$foo = 'foo';

/** @var int $foo */
$bar = $foo + 1;
};
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@ public function testAboveDeclare(): void
$this->analyse([__DIR__ . '/data/var-above-declare.php'], []);
}

public function testBug3515(): void
{
$this->analyse([__DIR__ . '/data/bug-3515.php'], []);
}

}
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-3515.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

/** @var int $foo */
$bar = $foo + 1;
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/wrong-variable-name-var.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,12 @@ public function thisInVar2()
$demo = $this->demo();
}

public function overrideDifferentVariableAboveAssign()
{
$foo = 'foo';

/** @var int $foo */
$bar = $foo + 1;
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,4 +865,23 @@ public function testBug1306(): void
$this->analyse([__DIR__ . '/data/bug-1306.php'], []);
}

public function testBug3515(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->polluteCatchScopeWithTryAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-3515.php'], [
[
'Undefined variable: $anArray',
19,
],
[
'Undefined variable: $anArray',
20,
],
]);
}

}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-3515.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Bug3515;

/**
* @var mixed[] $anArray
*/
$value1 = $anArray[0];
$value2 = $anArray[1];

/** @var int $foo */
$bar = $foo + 1;

function (): void
{
/**
* @var mixed[] $anArray
*/
$value1 = $anArray[0];
$value2 = $anArray[1];
};

0 comments on commit 22d363c

Please sign in to comment.