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
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 @@ -2119,6 +2123,73 @@ private function processBooleanNotSureConditionalTypes(Scope $scope, SpecifiedTy
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 [];
}

/**
* @return array{Expr, ConstantScalarType, Type}|null
*/
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