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
90 changes: 88 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public function specifyTypesInCondition(
if ($types->shouldOverwrite()) {
$result = $result->setAlwaysOverwriteTypes();
}
return $result->setNewConditionalExpressionHolders(array_merge(
return $result->setNewConditionalExpressionHolders($this->mergeConditionalHolderArrays(
$this->processBooleanNotSureConditionalTypes($scope, $leftTypesForHolders, $rightTypesForHolders, $rightScope),
$this->processBooleanNotSureConditionalTypes($scope, $rightTypesForHolders, $leftTypesForHolders, $scope),
$this->processBooleanSureConditionalTypes($scope, $leftTypesForHolders, $rightTypesForHolders, $rightScope),
Expand Down Expand Up @@ -795,7 +795,7 @@ public function specifyTypesInCondition(
if ($types->shouldOverwrite()) {
$result = $result->setAlwaysOverwriteTypes();
}
return $result->setNewConditionalExpressionHolders(array_merge(
return $result->setNewConditionalExpressionHolders($this->mergeConditionalHolderArrays(
$this->processBooleanNotSureConditionalTypes($scope, $leftTypes, $rightTypes, $rightScope),
$this->processBooleanNotSureConditionalTypes($scope, $rightTypes, $leftTypes, $scope),
$this->processBooleanSureConditionalTypes($scope, $leftTypes, $rightTypes, $rightScope),
Expand Down Expand Up @@ -1110,6 +1110,15 @@ public function specifyTypesInCondition(

return $result;
}

if ($constantArrays === [] && $varType->isArray()->yes() && $varType->getIterableValueType()->isNull()->no()) {
return $this->create(
$issetExpr->var,
new HasOffsetType($dimType),
$context,
$scope,
)->setRootExpr($expr);
}
}
}
}
Expand Down Expand Up @@ -2117,6 +2126,35 @@ private function processBooleanSureConditionalTypes(Scope $scope, SpecifiedTypes
$holders[$exprString][$holder->getKey()] = $holder;
}

foreach ($rightTypes->getSureNotTypes() as $exprString => [$expr, $type]) {
if (!$this->isTrackableExpression($expr)) {
continue;
}

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

$conditions = $conditionExpressionTypes;
foreach (array_keys($conditions) as $conditionExprString) {
if ($conditionExprString !== $exprString) {
continue;
}
unset($conditions[$conditionExprString]);
}

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

$targetScope = $expr instanceof Expr\Variable ? $scope : $rightScope;
$holder = new ConditionalExpressionHolder(
$conditions,
ExpressionTypeHolder::createYes($expr, TypeCombinator::remove($targetScope->getType($expr), $type)),
);
$holders[$exprString][$holder->getKey()] = $holder;
}

return $holders;
}

Expand All @@ -2134,6 +2172,25 @@ private function isTrackableExpression(Expr $expr): bool
|| $expr instanceof Expr\StaticPropertyFetch;
}

/**
* @param array<string, ConditionalExpressionHolder[]> ...$arrays
* @return array<string, ConditionalExpressionHolder[]>
*/
private function mergeConditionalHolderArrays(array ...$arrays): array
{
$result = [];
foreach ($arrays as $array) {
foreach ($array as $exprString => $holders) {
if (!isset($result[$exprString])) {
$result[$exprString] = $holders;
} else {
$result[$exprString] = array_merge($result[$exprString], $holders);
}
}
}
return $result;
}

/**
* 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 Expand Up @@ -2309,6 +2366,35 @@ private function processBooleanNotSureConditionalTypes(Scope $scope, SpecifiedTy
$holders[$exprString][$holder->getKey()] = $holder;
}

foreach ($rightTypes->getSureTypes() as $exprString => [$expr, $type]) {
if (!$this->isTrackableExpression($expr)) {
continue;
}

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

$conditions = $conditionExpressionTypes;
foreach (array_keys($conditions) as $conditionExprString) {
if ($conditionExprString !== $exprString) {
continue;
}
unset($conditions[$conditionExprString]);
}

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

$targetScope = $expr instanceof Expr\Variable ? $scope : $rightScope;
$holder = new ConditionalExpressionHolder(
$conditions,
ExpressionTypeHolder::createYes($expr, TypeCombinator::intersect($targetScope->getType($expr), $type)),
);
$holders[$exprString][$holder->getKey()] = $holder;
}

return $holders;
}

Expand Down
69 changes: 69 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11918.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types = 1);

namespace Bug11918;

use function PHPStan\Testing\assertType;

/**
* @param array<string, list<mixed>|string|false> $options
*/
function narrowMaybeSetArrayKey(array $options): void
{
if (array_key_exists('a', $options) && !is_string($options['a'])) {
exit(1);
}

// At this point: either 'a' doesn't exist in $options, or it's a string
assertType("array<string, list<mixed>|string|false>", $options);
assertType('string', $options['a'] ?? 'fallback');
}

/**
* @param array<string, list<mixed>|string|false> $options
*/
function narrowMaybeSetArrayKeyIsInt(array $options): void
{
if (array_key_exists('b', $options) && !is_int($options['b'])) {
exit(1);
}

assertType("array<string, list<mixed>|string|false>", $options);
}

/**
* @param array<string, int|string|bool> $data
*/
function narrowWithIsset(array $data): void
{
if (isset($data['key']) && !is_string($data['key'])) {
exit(1);
}

// After: 'key' either doesn't exist or is string (possibly non-falsy substring)
assertType('string', $data['key'] ?? 'default');
}

/**
* @param array<string, int|string|bool> $data
*/
function narrowWithNegatedOr(array $data): void
{
if (!array_key_exists('x', $data) || is_string($data['x'])) {
// Inside here: either 'x' doesn't exist or it's string
assertType('string', $data['x'] ?? 'default');
}
}

/**
* @param array<string, mixed> $data
*/
function narrowWithInstanceof(array $data): void
{
if (array_key_exists('obj', $data) && !$data['obj'] instanceof \stdClass) {
exit(1);
}

assertType('stdClass', $data['obj'] ?? new \stdClass());
}
Loading