Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3072,7 +3072,17 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto
}
foreach ($properties as $name => $type) {
$optional = in_array($name, $optionalProperties, true) || $refCount[$name] < count($constantArrays);
$scope = $scope->assignVariable($name, $type, $type, $optional ? TrinaryLogic::createMaybe() : TrinaryLogic::createYes());

if (!$optional) {
$scope = $scope->assignVariable($name, $type, $type, TrinaryLogic::createYes());
} else {
$hasVariable = $scope->hasVariableType($name);
if (!$hasVariable->no()) {
$type = TypeCombinator::union($scope->getVariableType($name), $type);
}

$scope = $scope->assignVariable($name, $type, $type, $scope->hasVariableType($name)->or(TrinaryLogic::createMaybe()));
}
}
} else {
$scope = $scope->afterExtractCall();
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ private static function findTestFiles(): iterable
yield __DIR__ . '/../Rules/Comparison/data/bug-5365.php';
yield __DIR__ . '/../Rules/Comparison/data/bug-6551.php';
yield __DIR__ . '/../Rules/Variables/data/bug-9403.php';
yield __DIR__ . '/../Rules/Variables/data/bug-12364.php';
yield __DIR__ . '/../Rules/Methods/data/bug-9542.php';
yield __DIR__ . '/../Rules/Functions/data/bug-9803.php';
yield __DIR__ . '/../Rules/PhpDoc/data/bug-10594.php';
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,24 @@ public function testIsStringNarrowsCertainty(): void
]);
}

public function testBug12364(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-12364.php'], [
[
'Variable $z might not be defined.',
20,
],
[
'Variable $z might not be defined.',
23,
],
]);
}

public function testDiscussion10252(): void
{
$this->cliArgumentsVariablesRegistered = true;
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-12364.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Bug12364;

use PHPStan\TrinaryLogic;
use function PHPStan\Testing\assertType;
use function PHPStan\Testing\assertVariableCertainty;

/** @return array{x: string, y?: string, z?: string} */
function foo(): array {
return [ 'x' => 'foo' ];
}

$x = $y = null;
assertType('null', $x);
assertType('null', $y);
extract(foo());
assertType('string', $x);
assertType('string|null', $y); // <-- should be: null|string
assertType('mixed', $z);
Comment on lines +15 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would also make sense to assertVariableCertainty on the 3 vars

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

assertVariableCertainty(TrinaryLogic::createYes(), $x);
assertVariableCertainty(TrinaryLogic::createYes(), $y);
assertVariableCertainty(TrinaryLogic::createMaybe(), $z);
var_dump($x);
var_dump($y); // <-- does exist

/** @return array{xx: string, yy?: string} */
function foo2(): array {
return [ 'xx' => 'foo' ];
}

function testUndefined()
{
if (rand(0, 1)) {
$xx = $yy = 0;
assertType('0', $xx);
assertType('0', $yy);
}

extract(foo2());
assertType('string', $xx);

if (isset($yy)) {
assertType('0|string', $yy);
}
}
Loading