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
62 changes: 60 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,13 @@
$leftTypes = $this->specifyTypesInCondition($scope, $expr->left, $context)->setRootExpr($expr);
$rightScope = $scope->filterByTruthyValue($expr->left);
$rightTypes = $this->specifyTypesInCondition($rightScope, $expr->right, $context)->setRootExpr($expr);
$types = $context->true() ? $leftTypes->unionWith($rightTypes) : $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope));
if ($context->true()) {
$types = $leftTypes->unionWith($rightTypes);
} else {
$leftNormalized = $this->propagateArrayDimFetchToParentArray($leftTypes->normalize($scope), $scope);
$rightNormalized = $this->propagateArrayDimFetchToParentArray($rightTypes->normalize($rightScope), $rightScope);
$types = $leftNormalized->intersectWith($rightNormalized);
}
if ($context->false()) {
$leftTypesForHolders = $leftTypes;
$rightTypesForHolders = $rightTypes;
Expand Down Expand Up @@ -788,7 +794,9 @@
) {
$types = $leftTypes->normalize($scope);
} else {
$types = $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($rightScope));
$leftNormalized = $this->propagateArrayDimFetchToParentArray($leftTypes->normalize($scope), $scope);
$rightNormalized = $this->propagateArrayDimFetchToParentArray($rightTypes->normalize($rightScope), $rightScope);
$types = $leftNormalized->intersectWith($rightNormalized);
$types = $this->augmentBooleanOrTruthyWithConditionalHolders($scope, $rightScope, $expr, $types);
}
} else {
Expand Down Expand Up @@ -2076,6 +2084,56 @@
return $types;
}

private function propagateArrayDimFetchToParentArray(SpecifiedTypes $normalizedTypes, Scope $scope): SpecifiedTypes
{
$additionalSureTypes = [];
$sureTypes = $normalizedTypes->getSureTypes();
foreach ($sureTypes as $exprString => [$expr, $type]) {
if (
!$expr instanceof Expr\ArrayDimFetch
|| $expr->dim === null
|| !$expr->var instanceof Expr\Variable
) {
continue;
}
$dimType = $scope->getType($expr->dim)->toArrayKey();
$constantDimType = null;
if ($dimType instanceof ConstantIntegerType) {
$constantDimType = $dimType;
} else {
$constantStrings = $dimType->getConstantStrings();
if (count($constantStrings) === 1) {
$constantDimType = $constantStrings[0];
}
}
if ($constantDimType === null) {
continue;
}
$varExprString = $this->exprPrinter->printExpr($expr->var);
if (isset($sureTypes[$varExprString]) || isset($additionalSureTypes[$varExprString])) {
continue;
}
$varType = $scope->getType($expr->var);
if ($varType instanceof MixedType || $varType->isArray()->no()) {

Check warning on line 2117 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } $varType = $scope->getType($expr->var); - if ($varType instanceof MixedType || $varType->isArray()->no()) { + if ($varType instanceof MixedType || !$varType->isArray()->yes()) { continue; } $narrowedVarType = TypeCombinator::intersect($varType, new HasOffsetValueType($constantDimType, $type));

Check warning on line 2117 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ continue; } $varType = $scope->getType($expr->var); - if ($varType instanceof MixedType || $varType->isArray()->no()) { + if ($varType instanceof MixedType || !$varType->isArray()->yes()) { continue; } $narrowedVarType = TypeCombinator::intersect($varType, new HasOffsetValueType($constantDimType, $type));
continue;
}
$narrowedVarType = TypeCombinator::intersect($varType, new HasOffsetValueType($constantDimType, $type));
if ($narrowedVarType instanceof NeverType) {
continue;
}
$additionalSureTypes[$varExprString] = [$expr->var, $narrowedVarType];
}

if ($additionalSureTypes === []) {
return $normalizedTypes;
}

return new SpecifiedTypes(
$sureTypes + $additionalSureTypes,
$normalizedTypes->getSureNotTypes(),
);
}

/**
* @return array<string, ConditionalExpressionHolder[]>
*/
Expand Down
80 changes: 80 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14566.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php declare(strict_types = 1);

namespace Bug14566;

use function PHPStan\Testing\assertType;

/**
* @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test
*/
function foo(array $test): void {
if (isset($test['hi']) && is_string($test['hi'])) {
return;
}
assertType("array{}|array{hi: array{0: 42, 1?: 42}}", $test);
}

/**
* @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test
*/
function fooElse(array $test): void {
if (isset($test['hi']) && is_string($test['hi'])) {
assertType("array{hi: 'hello'}", $test);
} else {
assertType("array{}|array{hi: array{0: 42, 1?: 42}}", $test);
}
}

/**
* @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test
*/
function fooIsArray(array $test): void {
if (isset($test['hi']) && is_array($test['hi'])) {
return;
}
assertType("array{}|array{hi: 'hello'}", $test);
}

/**
* @param array{}|array{hi: 'hello'}|array{hi: int} $test
*/
function fooIsInt(array $test): void {
if (isset($test['hi']) && is_int($test['hi'])) {
return;
}
assertType("array{}|array{hi: 'hello'}", $test);
}

class Foo {}

/**
* @param array{}|array{hi: Foo}|array{hi: 'hello'} $test
*/
function fooInstanceof(array $test): void {
if (isset($test['hi']) && $test['hi'] instanceof Foo) {
return;
}
assertType("array{}|array{hi: 'hello'}", $test);
}

/**
* @param array{}|array{hi: 'hello'}|array{hi: 'world'} $test
*/
function fooStrictComparison(array $test): void {
if (isset($test['hi']) && $test['hi'] === 'hello') {
return;
}
assertType("array{}|array{hi: 'world'}", $test);
}

/**
* BooleanOr truthy: logically equivalent to falsy branch of && version
* @param array{}|array{hi: 'hello'}|array{hi: array{0: 42, 1?: 42}} $test
*/
function fooBooleanOr(array $test): void {
if (!isset($test['hi']) || !is_string($test['hi'])) {
assertType("array{}|array{hi: array{0: 42, 1?: 42}}", $test);
} else {
assertType("array{hi: 'hello'}", $test);
}
}
Loading