Skip to content

Commit e0612d8

Browse files
phpstan-botondrejmirtes
authored andcommitted
Use genuine falsey narrowing (not the inverted truthy narrowing) for the consequent of BooleanAnd false-context conditional holders
- In `BooleanAndHandler::specifyTypes()` false context, split the per-arm narrowings used to build conditional expression holders into a condition (antecedent) set and a holder (consequent) set. - The truthy-narrowing-swap fallback (added for `isset()` on array dim fetches) is now only applied to the antecedent set. `processBooleanConditionalTypes` inverts that set back to the truthy narrowing, so the swap is sound there. - The consequent set keeps the genuine falsey narrowing of each arm (including the mixed truthy-and-false re-derivation). When an arm has no sound falsey narrowing it contributes no consequent, instead of fabricating one by inverting its truthy narrowing. - This fixes over-narrowing where `$a !== null && $b === $a` falling through and then re-testing `$a !== null` wrongly narrowed `$b` to `null`: inverting the `===` truthy narrowing (`$b` to `$a`'s broad `string`) removed `string` from `$b`. The same fault affected every comparison value type (int/string) and both variables and property fetches; all are fixed by the single change. - The `BooleanOr` truthy path does not use the swap and was already correct.
1 parent 240121d commit e0612d8

2 files changed

Lines changed: 81 additions & 16 deletions

File tree

‎src/Analyser/ExprHandler/BooleanAndHandler.php‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,29 +109,39 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
109109
$types = $this->conditionalExpressionHolderHelper->augmentDisjunctionTypes($scope, $rightScope, $leftNormalized, $rightNormalized, $expr->left, $expr->right, false, $types);
110110
}
111111
if ($context->false()) {
112-
$leftTypesForHolders = $leftTypes;
113-
$rightTypesForHolders = $rightTypes;
112+
// Consequent (holder) narrowings projected by each holder: these must be
113+
// the genuine falsey narrowing of the arm. When that is empty, the arm
114+
// has no sound falsey narrowing and must not contribute a consequent.
115+
$leftHolderTypes = $leftTypes;
116+
$rightHolderTypes = $rightTypes;
114117
// In a mixed truthy-and-false context, re-derive empty holders from the falsey narrowing.
115118
if ($context->truthy()) {
116-
if ($leftTypesForHolders->getSureTypes() === [] && $leftTypesForHolders->getSureNotTypes() === []) {
117-
$leftTypesForHolders = $typeSpecifier->specifyTypesInCondition($scope, $expr->left, TypeSpecifierContext::createFalsey())->setRootExpr($expr);
119+
if ($leftHolderTypes->getSureTypes() === [] && $leftHolderTypes->getSureNotTypes() === []) {
120+
$leftHolderTypes = $typeSpecifier->specifyTypesInCondition($scope, $expr->left, TypeSpecifierContext::createFalsey())->setRootExpr($expr);
118121
}
119-
if ($rightTypesForHolders->getSureTypes() === [] && $rightTypesForHolders->getSureNotTypes() === []) {
120-
$rightTypesForHolders = $typeSpecifier->specifyTypesInCondition($rightScope, $expr->right, TypeSpecifierContext::createFalsey())->setRootExpr($expr);
122+
if ($rightHolderTypes->getSureTypes() === [] && $rightHolderTypes->getSureNotTypes() === []) {
123+
$rightHolderTypes = $typeSpecifier->specifyTypesInCondition($rightScope, $expr->right, TypeSpecifierContext::createFalsey())->setRootExpr($expr);
121124
}
122125
}
123-
// For arms still empty (e.g. isset() on an array dim fetch), derive conditions
124-
// from the truthy narrowing instead, swapping sure/sureNot types.
125-
if ($leftTypesForHolders->getSureTypes() === [] && $leftTypesForHolders->getSureNotTypes() === []) {
126+
// Condition (antecedent) narrowings: when an arm has no falsey narrowing
127+
// (e.g. isset() on an array dim fetch), derive the condition from the truthy
128+
// narrowing by swapping sure/sureNot types. This swap is only sound for the
129+
// antecedent — processBooleanConditionalTypes inverts it back to the truthy
130+
// narrowing. It must NOT feed the consequent: inverting a comparison's truthy
131+
// narrowing (e.g. `$a === $b` narrowing `$a` to `$b`'s broad type) would
132+
// over-narrow the consequent (see regression for `$x === $nonConstantString`).
133+
$leftCondTypes = $leftHolderTypes;
134+
$rightCondTypes = $rightHolderTypes;
135+
if ($leftCondTypes->getSureTypes() === [] && $leftCondTypes->getSureNotTypes() === []) {
126136
$truthyLeftTypes = $typeSpecifier->specifyTypesInCondition($scope, $expr->left, TypeSpecifierContext::createTruthy());
127137
if ($this->allExpressionsTrackable($truthyLeftTypes)) {
128-
$leftTypesForHolders = new SpecifiedTypes($truthyLeftTypes->getSureNotTypes(), $truthyLeftTypes->getSureTypes());
138+
$leftCondTypes = new SpecifiedTypes($truthyLeftTypes->getSureNotTypes(), $truthyLeftTypes->getSureTypes());
129139
}
130140
}
131-
if ($rightTypesForHolders->getSureTypes() === [] && $rightTypesForHolders->getSureNotTypes() === []) {
141+
if ($rightCondTypes->getSureTypes() === [] && $rightCondTypes->getSureNotTypes() === []) {
132142
$truthyRightTypes = $typeSpecifier->specifyTypesInCondition($rightScope, $expr->right, TypeSpecifierContext::createTruthy());
133143
if ($this->allExpressionsTrackable($truthyRightTypes)) {
134-
$rightTypesForHolders = new SpecifiedTypes($truthyRightTypes->getSureNotTypes(), $truthyRightTypes->getSureTypes());
144+
$rightCondTypes = new SpecifiedTypes($truthyRightTypes->getSureNotTypes(), $truthyRightTypes->getSureTypes());
135145
}
136146
}
137147
$result = new SpecifiedTypes(
@@ -142,10 +152,10 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
142152
$result = $result->setAlwaysOverwriteTypes();
143153
}
144154
return $result->setNewConditionalExpressionHolders($this->conditionalExpressionHolderHelper->mergeConditionalHolders([
145-
$this->conditionalExpressionHolderHelper->processBooleanConditionalTypes($scope, $leftTypesForHolders, $rightTypesForHolders, false, true, $rightScope, $expr->right),
146-
$this->conditionalExpressionHolderHelper->processBooleanConditionalTypes($scope, $rightTypesForHolders, $leftTypesForHolders, false, true, $scope, $expr->left),
147-
$this->conditionalExpressionHolderHelper->processBooleanConditionalTypes($scope, $leftTypesForHolders, $rightTypesForHolders, true, true, $rightScope, $expr->right),
148-
$this->conditionalExpressionHolderHelper->processBooleanConditionalTypes($scope, $rightTypesForHolders, $leftTypesForHolders, true, true, $scope, $expr->left),
155+
$this->conditionalExpressionHolderHelper->processBooleanConditionalTypes($scope, $leftCondTypes, $rightHolderTypes, false, true, $rightScope, $expr->right),
156+
$this->conditionalExpressionHolderHelper->processBooleanConditionalTypes($scope, $rightCondTypes, $leftHolderTypes, false, true, $scope, $expr->left),
157+
$this->conditionalExpressionHolderHelper->processBooleanConditionalTypes($scope, $leftCondTypes, $rightHolderTypes, true, true, $rightScope, $expr->right),
158+
$this->conditionalExpressionHolderHelper->processBooleanConditionalTypes($scope, $rightCondTypes, $leftHolderTypes, true, true, $scope, $expr->left),
149159
]))->setRootExpr($expr);
150160
}
151161

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14828;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
10+
public function brokenWithGuard(?string $pendingValue, ?string $submitted): void
11+
{
12+
if ($pendingValue !== null && $submitted === $pendingValue) {
13+
return;
14+
}
15+
16+
// After `!(A && B)` the only sound consequent of "A is true" is `!B`,
17+
// i.e. `$submitted !== $pendingValue`, which does not narrow $submitted
18+
// because $pendingValue is a non-constant string.
19+
if ($pendingValue !== null) {
20+
assertType('string|null', $submitted);
21+
}
22+
}
23+
24+
public function intCase(?int $a, ?int $b): void
25+
{
26+
if ($a !== null && $b === $a) {
27+
return;
28+
}
29+
30+
if ($a !== null) {
31+
assertType('int|null', $b);
32+
}
33+
}
34+
35+
}
36+
37+
class WithProperties
38+
{
39+
40+
public ?string $p = null;
41+
42+
public ?string $q = null;
43+
44+
public function propertyCase(self $c): void
45+
{
46+
if ($c->p !== null && $c->q === $c->p) {
47+
return;
48+
}
49+
50+
if ($c->p !== null) {
51+
assertType('string|null', $c->q);
52+
}
53+
}
54+
55+
}

0 commit comments

Comments
 (0)