Skip to content

Commit 0a3a968

Browse files
committed
Any variable can exist after include/require
1 parent 1b126c9 commit 0a3a968

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2609,7 +2609,7 @@ static function (): void {
26092609
$throwPoints = $result->getThrowPoints();
26102610
$throwPoints[] = ThrowPoint::createImplicit($scope, $expr);
26112611
$hasYield = $result->hasYield();
2612-
$scope = $result->getScope();
2612+
$scope = $result->getScope()->afterExtractCall();
26132613
} elseif (
26142614
$expr instanceof Expr\BitwiseNot
26152615
|| $expr instanceof Cast

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,23 @@ public function testDefinedVariables(): void
215215
360,
216216
],
217217
[
218-
'Undefined variable: $variableInWhileIsset',
219-
365,
220-
],
221-
[
222-
'Undefined variable: $unknownVariablePassedToReset',
218+
'Variable $unknownVariablePassedToReset might not be defined.',
223219
368,
224220
],
225221
[
226-
'Undefined variable: $unknownVariablePassedToReset',
222+
'Variable $unknownVariablePassedToReset might not be defined.',
227223
369,
228224
],
229225
[
230-
'Undefined variable: $variableInAssign',
226+
'Variable $variableInAssign might not be defined.',
231227
384,
232228
],
233229
[
234-
'Undefined variable: $undefinedArrayIndex',
230+
'Variable $undefinedArrayIndex might not be defined.',
235231
409,
236232
],
237233
[
238-
'Undefined variable: $anotherUndefinedArrayIndex',
234+
'Variable $anotherUndefinedArrayIndex might not be defined.',
239235
409,
240236
],
241237
[
@@ -1024,4 +1020,13 @@ public function testIsStringNarrowsCertainty(): void
10241020
]);
10251021
}
10261022

1023+
public function testDiscussion10252(): void
1024+
{
1025+
$this->cliArgumentsVariablesRegistered = true;
1026+
$this->polluteScopeWithLoopInitialAssignments = true;
1027+
$this->checkMaybeUndefinedVariables = true;
1028+
$this->polluteScopeWithAlwaysIterableForeach = true;
1029+
$this->analyse([__DIR__ . '/data/discussion-10252.php'], []);
1030+
}
1031+
10271032
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Discussion10252;
4+
5+
function sayIt(): void
6+
{
7+
require 'who-to-love.php';
8+
9+
/** @var string $name */
10+
echo 'I love you, ' . $name .'!';
11+
}

0 commit comments

Comments
 (0)