Skip to content
Open
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 @@ -2819,7 +2819,17 @@ static function (): void {
}
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 @@ -226,6 +226,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
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,15 @@ 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'], []);
}

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

namespace Bug12364;

use function PHPStan\Testing\assertType;

/** @return array{x: string, y?: 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
var_dump($x);
var_dump($y); // <-- does exist
Loading