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
56 changes: 53 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4086,12 +4086,14 @@
} else {
$constantArraysA = TypeCombinator::union(...$constantArrays['a']);
$constantArraysB = TypeCombinator::union(...$constantArrays['b']);
$aKeyType = $constantArraysA->getIterableKeyType();
$bKeyType = $constantArraysB->getIterableKeyType();
if (
$constantArraysA->getIterableKeyType()->equals($constantArraysB->getIterableKeyType())
$aKeyType->equals($bKeyType)
&& $constantArraysA->getArraySize()->getGreaterOrEqualType($this->phpVersion)->isSuperTypeOf($constantArraysB->getArraySize())->yes()
) {
$resultArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
foreach (TypeUtils::flattenTypes($constantArraysA->getIterableKeyType()) as $keyType) {
foreach (TypeUtils::flattenTypes($aKeyType) as $keyType) {
$resultArrayBuilder->setOffsetValueType(
$keyType,
$this->generalizeType(
Expand All @@ -4103,10 +4105,40 @@
);
}

$resultTypes[] = $resultArrayBuilder->getArray();
} elseif (
$bKeyType->isSuperTypeOf($aKeyType)->yes()

Check warning on line 4110 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $resultTypes[] = $resultArrayBuilder->getArray(); } elseif ( - $bKeyType->isSuperTypeOf($aKeyType)->yes() + !$bKeyType->isSuperTypeOf($aKeyType)->no() && !$aKeyType->equals($bKeyType) && $this->hasStructurallyMixedValueTypes($constantArraysB) ) {

Check warning on line 4110 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $resultTypes[] = $resultArrayBuilder->getArray(); } elseif ( - $bKeyType->isSuperTypeOf($aKeyType)->yes() + $aKeyType->isSuperTypeOf($bKeyType)->yes() && !$aKeyType->equals($bKeyType) && $this->hasStructurallyMixedValueTypes($constantArraysB) ) {

Check warning on line 4110 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $resultTypes[] = $resultArrayBuilder->getArray(); } elseif ( - $bKeyType->isSuperTypeOf($aKeyType)->yes() + !$bKeyType->isSuperTypeOf($aKeyType)->no() && !$aKeyType->equals($bKeyType) && $this->hasStructurallyMixedValueTypes($constantArraysB) ) {

Check warning on line 4110 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $resultTypes[] = $resultArrayBuilder->getArray(); } elseif ( - $bKeyType->isSuperTypeOf($aKeyType)->yes() + $aKeyType->isSuperTypeOf($bKeyType)->yes() && !$aKeyType->equals($bKeyType) && $this->hasStructurallyMixedValueTypes($constantArraysB) ) {
&& !$aKeyType->equals($bKeyType)
&& $this->hasStructurallyMixedValueTypes($constantArraysB)
) {
$resultArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
foreach (TypeUtils::flattenTypes($bKeyType) as $keyType) {
$hasInA = $constantArraysA->hasOffsetValueType($keyType);
$hasInB = $constantArraysB->hasOffsetValueType($keyType);

if ($hasInA->no()) {

Check warning on line 4119 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $hasInA = $constantArraysA->hasOffsetValueType($keyType); $hasInB = $constantArraysB->hasOffsetValueType($keyType); - if ($hasInA->no()) { + if (!$hasInA->yes()) { $valueType = $constantArraysB->getOffsetValueType($keyType); } elseif ($hasInB->no()) { $valueType = $constantArraysA->getOffsetValueType($keyType);

Check warning on line 4119 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $hasInA = $constantArraysA->hasOffsetValueType($keyType); $hasInB = $constantArraysB->hasOffsetValueType($keyType); - if ($hasInA->no()) { + if (!$hasInA->yes()) { $valueType = $constantArraysB->getOffsetValueType($keyType); } elseif ($hasInB->no()) { $valueType = $constantArraysA->getOffsetValueType($keyType);
$valueType = $constantArraysB->getOffsetValueType($keyType);
} elseif ($hasInB->no()) {

Check warning on line 4121 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($hasInA->no()) { $valueType = $constantArraysB->getOffsetValueType($keyType); - } elseif ($hasInB->no()) { + } elseif (!$hasInB->yes()) { $valueType = $constantArraysA->getOffsetValueType($keyType); } else { $valueType = $this->generalizeType(

Check warning on line 4121 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($hasInA->no()) { $valueType = $constantArraysB->getOffsetValueType($keyType); - } elseif ($hasInB->no()) { + } elseif (!$hasInB->yes()) { $valueType = $constantArraysA->getOffsetValueType($keyType); } else { $valueType = $this->generalizeType(
$valueType = $constantArraysA->getOffsetValueType($keyType);
} else {
$valueType = $this->generalizeType(
$constantArraysA->getOffsetValueType($keyType),
$constantArraysB->getOffsetValueType($keyType),
$depth + 1,
);
}

$resultArrayBuilder->setOffsetValueType(
$keyType,
$valueType,
!$hasInA->and($hasInB)->negate()->no(),

Check warning on line 4134 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $resultArrayBuilder->setOffsetValueType( $keyType, $valueType, - !$hasInA->and($hasInB)->negate()->no(), + $hasInA->and($hasInB)->negate()->yes(), ); }

Check warning on line 4134 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $resultArrayBuilder->setOffsetValueType( $keyType, $valueType, - !$hasInA->and($hasInB)->negate()->no(), + $hasInA->and($hasInB)->negate()->yes(), ); }
);
}

$resultTypes[] = $resultArrayBuilder->getArray();
} else {
$resultType = new ArrayType(
TypeCombinator::union($this->generalizeType($constantArraysA->getIterableKeyType(), $constantArraysB->getIterableKeyType(), $depth + 1)),
TypeCombinator::union($this->generalizeType($aKeyType, $bKeyType, $depth + 1)),
TypeCombinator::union($this->generalizeType($constantArraysA->getIterableValueType(), $constantArraysB->getIterableValueType(), $depth + 1)),
);
$accessories = [];
Expand Down Expand Up @@ -4349,6 +4381,24 @@
return $depth;
}

private function hasStructurallyMixedValueTypes(Type $arrayType): bool
{
$hasArrayValuePart = false;
$hasNonArrayValuePart = false;
foreach (TypeUtils::flattenTypes($arrayType->getIterableValueType()) as $innerType) {
if ($innerType->isArray()->yes()) {

Check warning on line 4389 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $hasArrayValuePart = false; $hasNonArrayValuePart = false; foreach (TypeUtils::flattenTypes($arrayType->getIterableValueType()) as $innerType) { - if ($innerType->isArray()->yes()) { + if (!$innerType->isArray()->no()) { $hasArrayValuePart = true; } if (!$innerType->isArray()->no()) {

Check warning on line 4389 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $hasArrayValuePart = false; $hasNonArrayValuePart = false; foreach (TypeUtils::flattenTypes($arrayType->getIterableValueType()) as $innerType) { - if ($innerType->isArray()->yes()) { + if (!$innerType->isArray()->no()) { $hasArrayValuePart = true; } if (!$innerType->isArray()->no()) {
$hasArrayValuePart = true;
}
if (!$innerType->isArray()->no()) {

Check warning on line 4392 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($innerType->isArray()->yes()) { $hasArrayValuePart = true; } - if (!$innerType->isArray()->no()) { + if ($innerType->isArray()->yes()) { continue; }

Check warning on line 4392 in src/Analyser/MutatingScope.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if ($innerType->isArray()->yes()) { $hasArrayValuePart = true; } - if (!$innerType->isArray()->no()) { + if ($innerType->isArray()->yes()) { continue; }
continue;
}

$hasNonArrayValuePart = true;
}

return $hasArrayValuePart && $hasNonArrayValuePart;
}

public function equals(self $otherScope): bool
{
if (!$this->context->equals($otherScope->context)) {
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9691.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types = 1);

namespace Bug9691;

use function PHPStan\Testing\assertType;

function (): void {
$issues = [];
$previousValue = 1;

for ($i = 0; $i < 2; $i++) {
if ($previousValue === $i) {
$issues[0] = 0;
}
$issues[1]['abc'] = 'def';

assertType("array{abc: 'def'}", $issues[1]);

$previousValue = $i;
}
};
Loading