Skip to content

Commit 29c49f2

Browse files
ondrejmirtesclaude
andcommitted
Apply the falsey isset() narrowing of the left side to the ?? right scope
The right side of `$a ?? $b` only evaluates when the left side is null or unset, but its scope was built from the null pin alone - the falsey isset() narrowing composed from the left read (offset removals, certainty reductions) only reached the value scope of `??=`. Apply it to the plain coalesce too, except when the left side is surely set and surely non-null: there the right side cannot evaluate at all, and the counterfactual certainty reductions would survive the scope merge after the coalesce. An always-initialized nullable property read on the left now pins to null on the right side, so `$a->noDefault ?? $b->noDefault` composes with earlier not-both-null narrowing - the updated isset-property-default-value expectation matches the defaulted-property case above it. Closes phpstan/phpstan#6379 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CGhnJQpUWRRpg6H5LuWJkA
1 parent 77527df commit 29c49f2

5 files changed

Lines changed: 79 additions & 4 deletions

File tree

‎src/Analyser/ExprHandler/CoalesceHandler.php‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,34 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
5656
$scope = $this->nonNullabilityHelper->revertNonNullability($condResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions());
5757
$scope = $nodeScopeResolver->lookForUnsetAllowedUndefinedExpressions($scope, $expr->left);
5858

59+
$chainResults = [];
60+
$this->defaultNarrowingHelper->captureChainResults($expr->left, $storage, $chainResults);
61+
5962
// the falsey narrowing of this very node - asking the scope about it
6063
// mid-processing would take the on-demand path and recurse
61-
$rightScope = $scope->applySpecifiedTypes($this->coalesceCompositionHelper->getFalseySpecifiedTypes($scope, $scope, $expr->left, $condResult, $expr, TypeSpecifierContext::createFalsey()));
64+
$rightSideSpecifiedTypes = $this->coalesceCompositionHelper->getFalseySpecifiedTypes($scope, $scope, $expr->left, $condResult, $expr, TypeSpecifierContext::createFalsey());
65+
$leftSurelySetNonNull = $condResult->getIssetabilityResolution($scope, false)->isSet(static function (Type $type): ?bool {
66+
$isNull = $type->isNull();
67+
if ($isNull->maybe()) {
68+
return null;
69+
}
70+
71+
return !$isNull->yes();
72+
}) === true;
73+
if (!$leftSurelySetNonNull) {
74+
// the right side only evaluates when the left side is null or unset -
75+
// the falsey isset() narrowing of the left side, like `??=`; skipped
76+
// when the right side cannot evaluate at all, so its counterfactual
77+
// certainty reductions do not survive the merge below
78+
$rightSideSpecifiedTypes = $rightSideSpecifiedTypes->unionWith(
79+
$this->coalesceCompositionHelper->getRightSideScopeSpecifiedTypes($scope, $expr->left, $condResult, $chainResults, $expr),
80+
);
81+
}
82+
$rightScope = $scope->applySpecifiedTypes($rightSideSpecifiedTypes);
6283
$rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $rightScope, $storage, $nodeCallback, $context->enterDeepKeepingValueFlow());
6384
// the left-is-set narrowing, composed from the already-processed chain
6485
// results - the inside-out equivalent of narrowing by isset($expr->left)
6586
// without synthesizing an Isset_ node and re-walking the chain on demand
66-
$chainResults = [];
67-
$this->defaultNarrowingHelper->captureChainResults($expr->left, $storage, $chainResults);
6887
$leftIssetTypes = $this->defaultNarrowingHelper->createIssetTruthyChainTypes(
6988
$scope,
7089
$expr->left,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug6379Types;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
10+
/**
11+
* @param array{
12+
* cr?: string,
13+
* c?: string
14+
* } $params
15+
*/
16+
public static function paramsToString(array $params): string
17+
{
18+
if (isset($params['cr']) === true || isset($params['c']) === true) {
19+
assertType('non-empty-array{cr?: string, c?: string}', $params);
20+
if (!isset($params['cr'])) {
21+
assertType('array{c: string}', $params);
22+
}
23+
24+
return sprintf('-c%s', $params['cr'] ?? $params['c']);
25+
}
26+
27+
return '';
28+
}
29+
30+
}

‎tests/PHPStan/Analyser/nsrt/isset-property-default-value.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ function coalesceWithoutDefault(Holder $a, Holder $b): void
2828
throw new \LogicException();
2929
}
3030

31-
assertType('int|null', $a->noDefault ?? $b->noDefault);
31+
assertType('int', $a->noDefault ?? $b->noDefault);
3232
}

‎tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,11 @@ public function testBug13688(): void
13631363
$this->analyse([__DIR__ . '/data/bug-13688.php'], []);
13641364
}
13651365

1366+
public function testBug6379(): void
1367+
{
1368+
$this->analyse([__DIR__ . '/data/bug-6379.php'], []);
1369+
}
1370+
13661371
public static function dataUnsealedArrayShapes(): iterable
13671372
{
13681373
foreach ([false, true] as $reportPossiblyNonexistentGeneralArrayOffset) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug6379;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @param array{
9+
* cr?: string,
10+
* c?: string
11+
* } $params
12+
*/
13+
public static function paramsToString(array $params): string
14+
{
15+
if (isset($params['cr']) === true || isset($params['c']) === true) {
16+
return sprintf('-c%s', $params['cr'] ?? $params['c']);
17+
}
18+
19+
return '';
20+
}
21+
}

0 commit comments

Comments
 (0)