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
22 changes: 17 additions & 5 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4092,13 +4092,25 @@
) {
$resultArrayBuilder = ConstantArrayTypeBuilder::createEmpty();
foreach (TypeUtils::flattenTypes($constantArraysA->getIterableKeyType()) as $keyType) {
$aValueType = $constantArraysA->getOffsetValueType($keyType);
$bValueType = $constantArraysB->getOffsetValueType($keyType);

$canPreserve = $aValueType->isInteger()->no()

Check warning on line 4098 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": @@ @@ $aValueType = $constantArraysA->getOffsetValueType($keyType); $bValueType = $constantArraysB->getOffsetValueType($keyType); - $canPreserve = $aValueType->isInteger()->no() + $canPreserve = !$aValueType->isInteger()->yes() && $bValueType->isInteger()->no() && $aValueType->isArray()->no() && $bValueType->isArray()->no();

Check warning on line 4098 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": @@ @@ $aValueType = $constantArraysA->getOffsetValueType($keyType); $bValueType = $constantArraysB->getOffsetValueType($keyType); - $canPreserve = $aValueType->isInteger()->no() + $canPreserve = !$aValueType->isInteger()->yes() && $bValueType->isInteger()->no() && $aValueType->isArray()->no() && $bValueType->isArray()->no();
&& $bValueType->isInteger()->no()

Check warning on line 4099 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": @@ @@ $bValueType = $constantArraysB->getOffsetValueType($keyType); $canPreserve = $aValueType->isInteger()->no() - && $bValueType->isInteger()->no() + && !$bValueType->isInteger()->yes() && $aValueType->isArray()->no() && $bValueType->isArray()->no();

Check warning on line 4099 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": @@ @@ $bValueType = $constantArraysB->getOffsetValueType($keyType); $canPreserve = $aValueType->isInteger()->no() - && $bValueType->isInteger()->no() + && !$bValueType->isInteger()->yes() && $aValueType->isArray()->no() && $bValueType->isArray()->no();
&& $aValueType->isArray()->no()

Check warning on line 4100 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": @@ @@ $canPreserve = $aValueType->isInteger()->no() && $bValueType->isInteger()->no() - && $aValueType->isArray()->no() + && !$aValueType->isArray()->yes() && $bValueType->isArray()->no(); if ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) {

Check warning on line 4100 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": @@ @@ $canPreserve = $aValueType->isInteger()->no() && $bValueType->isInteger()->no() - && $aValueType->isArray()->no() + && !$aValueType->isArray()->yes() && $bValueType->isArray()->no(); if ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) {
&& $bValueType->isArray()->no();

Check warning on line 4101 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": @@ @@ $canPreserve = $aValueType->isInteger()->no() && $bValueType->isInteger()->no() && $aValueType->isArray()->no() - && $bValueType->isArray()->no(); + && !$bValueType->isArray()->yes(); if ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) { $generalizedValue = $aValueType;

Check warning on line 4101 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": @@ @@ $canPreserve = $aValueType->isInteger()->no() && $bValueType->isInteger()->no() && $aValueType->isArray()->no() - && $bValueType->isArray()->no(); + && !$bValueType->isArray()->yes(); if ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) { $generalizedValue = $aValueType;

if ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) {

Check warning on line 4103 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": @@ @@ && $aValueType->isArray()->no() && $bValueType->isArray()->no(); - if ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) { + if ($canPreserve && !$aValueType->isSuperTypeOf($bValueType)->no()) { $generalizedValue = $aValueType; } elseif ($canPreserve && $bValueType->isSuperTypeOf($aValueType)->yes()) { $generalizedValue = $bValueType;

Check warning on line 4103 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": @@ @@ && $aValueType->isArray()->no() && $bValueType->isArray()->no(); - if ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) { + if ($canPreserve && $bValueType->isSuperTypeOf($aValueType)->yes()) { $generalizedValue = $aValueType; } elseif ($canPreserve && $bValueType->isSuperTypeOf($aValueType)->yes()) { $generalizedValue = $bValueType;

Check warning on line 4103 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": @@ @@ && $aValueType->isArray()->no() && $bValueType->isArray()->no(); - if ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) { + if ($canPreserve && !$aValueType->isSuperTypeOf($bValueType)->no()) { $generalizedValue = $aValueType; } elseif ($canPreserve && $bValueType->isSuperTypeOf($aValueType)->yes()) { $generalizedValue = $bValueType;

Check warning on line 4103 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": @@ @@ && $aValueType->isArray()->no() && $bValueType->isArray()->no(); - if ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) { + if ($canPreserve && $bValueType->isSuperTypeOf($aValueType)->yes()) { $generalizedValue = $aValueType; } elseif ($canPreserve && $bValueType->isSuperTypeOf($aValueType)->yes()) { $generalizedValue = $bValueType;
$generalizedValue = $aValueType;
} elseif ($canPreserve && $bValueType->isSuperTypeOf($aValueType)->yes()) {

Check warning on line 4105 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 ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) { $generalizedValue = $aValueType; - } elseif ($canPreserve && $bValueType->isSuperTypeOf($aValueType)->yes()) { + } elseif ($canPreserve && !$bValueType->isSuperTypeOf($aValueType)->no()) { $generalizedValue = $bValueType; } else { $generalizedValue = $this->generalizeType($aValueType, $bValueType, $depth + 1);

Check warning on line 4105 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 ($canPreserve && $aValueType->isSuperTypeOf($bValueType)->yes()) { $generalizedValue = $aValueType; - } elseif ($canPreserve && $bValueType->isSuperTypeOf($aValueType)->yes()) { + } elseif ($canPreserve && !$bValueType->isSuperTypeOf($aValueType)->no()) { $generalizedValue = $bValueType; } else { $generalizedValue = $this->generalizeType($aValueType, $bValueType, $depth + 1);
$generalizedValue = $bValueType;
} else {
$generalizedValue = $this->generalizeType($aValueType, $bValueType, $depth + 1);
}

$resultArrayBuilder->setOffsetValueType(
$keyType,
$this->generalizeType(
$constantArraysA->getOffsetValueType($keyType),
$constantArraysB->getOffsetValueType($keyType),
$depth + 1,
),
$generalizedValue,
!$constantArraysA->hasOffsetValueType($keyType)->and($constantArraysB->hasOffsetValueType($keyType))->negate()->no(),
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/array-keys-branches.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function (array $generalArray) {
assertType('mixed~null', $generalArray['key']);
assertType('array{0: \'foo\', 1: \'bar\', 2?: \'baz\'}', $arrayAppendedInIf);
assertType('non-empty-list<\'bar\'|\'baz\'|\'foo\'>', $arrayAppendedInForeach);
assertType('non-empty-array<int<0, max>, literal-string&lowercase-string&non-falsy-string>', $anotherArrayAppendedInForeach);
assertType("non-empty-array<int<0, max>, 'bar'|'baz'|'foo'>", $anotherArrayAppendedInForeach);
assertType('\'str\'', $array['n']);
assertType('int<0, max>', $incremented);
assertType('0|1', $setFromZeroToOne);
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12653.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Bug12653;

use function PHPStan\Testing\assertType;

class Reproduction
{
const TYPE_XXX = 'xxx';
const TYPE_YYY = 'yyy';
const TYPE_ZZZ = 'zzz';

/**
* @return array<'a'|'b'|'c'|'d',Reproduction::TYPE_*>
*/
public function main()
{
$list = [
'a' => Reproduction::TYPE_XXX,
'b' => Reproduction::TYPE_YYY,
'c' => Reproduction::TYPE_ZZZ,
'd' => Reproduction::TYPE_XXX,
];

$keys = ['a', 'b', 'c', 'd'];
$found = false;
foreach ($keys as $key) {
if ($list[$key] === Reproduction::TYPE_XXX) {
// The first matched key is kept and subsequent matched keys are rewritten.
if (!$found) {
$found = true;
} else {
$list[$key] = Reproduction::TYPE_ZZZ;
}
}
}

assertType("array{a: 'xxx'|'zzz', b: 'yyy'|'zzz', c: 'zzz', d: 'xxx'|'zzz'}", $list);

return $list;
}
}
111 changes: 111 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12653b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php declare(strict_types = 1);

namespace Bug12653b;

use function PHPStan\Testing\assertType;

class WhileLoopTest
{
const TYPE_XXX = 'xxx';
const TYPE_YYY = 'yyy';
const TYPE_ZZZ = 'zzz';

/**
* @return array<'a'|'b'|'c'|'d', self::TYPE_*>
*/
public function whileLoop(): array
{
$list = [
'a' => self::TYPE_XXX,
'b' => self::TYPE_YYY,
'c' => self::TYPE_ZZZ,
'd' => self::TYPE_XXX,
];

$keys = ['a', 'b', 'c', 'd'];
$found = false;
$i = 0;
while ($i < count($keys)) {
$key = $keys[$i];
if ($list[$key] === self::TYPE_XXX) {
if (!$found) {
$found = true;
} else {
$list[$key] = self::TYPE_ZZZ;
}
}
$i++;
}
assertType("array{a: 'xxx'|'zzz', b: 'yyy'|'zzz', c: 'zzz', d: 'xxx'|'zzz'}", $list);

return $list;
}
}

class ForLoopTest
{
const TYPE_XXX = 'xxx';
const TYPE_YYY = 'yyy';
const TYPE_ZZZ = 'zzz';

/**
* @return array<'a'|'b'|'c'|'d', self::TYPE_*>
*/
public function forLoop(): array
{
$list = [
'a' => self::TYPE_XXX,
'b' => self::TYPE_YYY,
'c' => self::TYPE_ZZZ,
'd' => self::TYPE_XXX,
];

$keys = ['a', 'b', 'c', 'd'];
$found = false;
for ($i = 0; $i < count($keys); $i++) {
$key = $keys[$i];
if ($list[$key] === self::TYPE_XXX) {
if (!$found) {
$found = true;
} else {
$list[$key] = self::TYPE_ZZZ;
}
}
}
assertType("array{a: 'xxx'|'zzz', b: 'yyy'|'zzz', c: 'zzz', d: 'xxx'|'zzz'}", $list);

return $list;
}
}

class FloatConstantArrayTest
{
const RATE_LOW = 0.5;
const RATE_MED = 1.0;
const RATE_HIGH = 1.5;

/**
* @param list<'x'|'y'|'z'> $keys
*/
public function floatConstantsInArray(array $keys): void
{
$rates = [
'x' => self::RATE_LOW,
'y' => self::RATE_MED,
'z' => self::RATE_HIGH,
];

$found = false;
foreach ($keys as $key) {
if ($rates[$key] === self::RATE_LOW) {
if (!$found) {
$found = true;
} else {
$rates[$key] = self::RATE_HIGH;
}
}
}

assertType("array{x: 0.5|1.5, y: 1.0|1.5, z: 1.5}", $rates);
}
}
Loading