Skip to content
Closed
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
Fix phpstan/phpstan#14455: missing has-offset via conditional type
- Added processBooleanNotSureSureConditionalTypes method to TypeSpecifier
  that creates conditional expression holders with sure-NOT-type conditions
  and sure-type results (cross-product of existing methods)
- Called the new method in both BooleanAnd (falsey) and BooleanOr (truthy)
  conditional expression holder creation
- New regression test in tests/PHPStan/Analyser/nsrt/bug-14455.php
- Root cause: empty($arr['key']) && $type === 'filter' in an early return
  produced sure-NOT-types for $type and sure-types for $arr, but no existing
  method combined these to create the conditional holder needed for narrowing
  • Loading branch information
phpstan-bot committed Apr 13, 2026
commit d57d06882072c3bc91122b95a12b8202929e89d4
71 changes: 71 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ public function specifyTypesInCondition(
$this->processBooleanNotSureConditionalTypes($scope, $rightTypesForHolders, $leftTypesForHolders),
$this->processBooleanSureConditionalTypes($scope, $leftTypesForHolders, $rightTypesForHolders),
$this->processBooleanSureConditionalTypes($scope, $rightTypesForHolders, $leftTypesForHolders),
$this->processBooleanNotSureSureConditionalTypes($scope, $leftTypesForHolders, $rightTypesForHolders),
$this->processBooleanNotSureSureConditionalTypes($scope, $rightTypesForHolders, $leftTypesForHolders),
))->setRootExpr($expr);
}

Expand Down Expand Up @@ -790,6 +792,8 @@ public function specifyTypesInCondition(
$this->processBooleanNotSureConditionalTypes($scope, $rightTypes, $leftTypes),
$this->processBooleanSureConditionalTypes($scope, $leftTypes, $rightTypes),
$this->processBooleanSureConditionalTypes($scope, $rightTypes, $leftTypes),
$this->processBooleanNotSureSureConditionalTypes($scope, $leftTypes, $rightTypes),
$this->processBooleanNotSureSureConditionalTypes($scope, $rightTypes, $leftTypes),
))->setRootExpr($expr);
}

Expand Down Expand Up @@ -1998,6 +2002,73 @@ private function processBooleanSureConditionalTypes(Scope $scope, SpecifiedTypes
return [];
}

/**
* @return array<string, ConditionalExpressionHolder[]>
*/
private function processBooleanNotSureSureConditionalTypes(Scope $scope, SpecifiedTypes $conditionTypes, SpecifiedTypes $resultTypes): array
{
$conditionExpressionTypes = [];
foreach ($conditionTypes->getSureNotTypes() as $exprString => [$expr, $type]) {
if (!$expr instanceof Expr\Variable) {
continue;
}
if (!is_string($expr->name)) {
continue;
}

$conditionExpressionTypes[$exprString] = ExpressionTypeHolder::createYes(
$expr,
TypeCombinator::intersect($scope->getType($expr), $type),
);
}

if (count($conditionExpressionTypes) > 0) {
$holders = [];
foreach ($resultTypes->getSureTypes() as $exprString => [$expr, $type]) {
if (!$expr instanceof Expr\Variable) {
continue;
}
if (!is_string($expr->name)) {
continue;
}

if (!isset($holders[$exprString])) {
$holders[$exprString] = [];
}

$conditions = $conditionExpressionTypes;
foreach ($conditions as $conditionExprString => $conditionExprTypeHolder) {
$conditionExpr = $conditionExprTypeHolder->getExpr();
if (!$conditionExpr instanceof Expr\Variable) {
continue;
}
if (!is_string($conditionExpr->name)) {
continue;
}
if ($conditionExpr->name !== $expr->name) {
continue;
}

unset($conditions[$conditionExprString]);
}

if (count($conditions) === 0) {
continue;
}

$holder = new ConditionalExpressionHolder(
$conditions,
ExpressionTypeHolder::createYes($expr, TypeCombinator::intersect($scope->getType($expr), $type)),
);
$holders[$exprString][$holder->getKey()] = $holder;
}

return $holders;
}

return [];
}

/**
* Flatten a deep BooleanOr chain into leaf expressions and process them
* without recursive filterByFalseyValue calls. This reduces O(n^2) to O(n)
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14455.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bug14455;

use function PHPStan\Testing\assertType;

/**
* @param array<string, mixed> $aggregation
* @param non-falsy-string $type
*/
function testTriviallyTrueConditionSkipped(array $aggregation, string $type): void
{
if (empty($aggregation['field']) && $type === 'filter') {
return;
}

assertType("array<string, mixed>", $aggregation);
assertType('non-falsy-string', $type);

if ($type === 'filter') {
assertType("non-empty-array<string, mixed>&hasOffset('field')", $aggregation);
} else {
assertType("array<string, mixed>", $aggregation);
}

assertType('non-falsy-string', $type);
}
Loading