Skip to content

Commit

Permalink
Any variable can exist after include/require
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 7, 2023
1 parent 1b126c9 commit 0a3a968
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2609,7 +2609,7 @@ static function (): void {
$throwPoints = $result->getThrowPoints();
$throwPoints[] = ThrowPoint::createImplicit($scope, $expr);
$hasYield = $result->hasYield();
$scope = $result->getScope();
$scope = $result->getScope()->afterExtractCall();
} elseif (
$expr instanceof Expr\BitwiseNot
|| $expr instanceof Cast
Expand Down
23 changes: 14 additions & 9 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,27 +215,23 @@ public function testDefinedVariables(): void
360,
],
[
'Undefined variable: $variableInWhileIsset',
365,
],
[
'Undefined variable: $unknownVariablePassedToReset',
'Variable $unknownVariablePassedToReset might not be defined.',
368,
],
[
'Undefined variable: $unknownVariablePassedToReset',
'Variable $unknownVariablePassedToReset might not be defined.',
369,
],
[
'Undefined variable: $variableInAssign',
'Variable $variableInAssign might not be defined.',
384,
],
[
'Undefined variable: $undefinedArrayIndex',
'Variable $undefinedArrayIndex might not be defined.',
409,
],
[
'Undefined variable: $anotherUndefinedArrayIndex',
'Variable $anotherUndefinedArrayIndex might not be defined.',
409,
],
[
Expand Down Expand Up @@ -1024,4 +1020,13 @@ public function testIsStringNarrowsCertainty(): void
]);
}

public function testDiscussion10252(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/discussion-10252.php'], []);
}

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

namespace Discussion10252;

function sayIt(): void
{
require 'who-to-love.php';

/** @var string $name */
echo 'I love you, ' . $name .'!';
}

0 comments on commit 0a3a968

Please sign in to comment.