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
11 changes: 9 additions & 2 deletions src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,8 @@

$offsetValueTypeStack = [$offsetValueType];
$generalizeOnWrite = $offsetTypes[array_key_last($offsetTypes)][0] !== null;
foreach (array_slice($offsetTypes, 0, -1) as [$offsetType, $dimFetch]) {
$slicedOffsets = array_slice($offsetTypes, 0, -1);
foreach ($slicedOffsets as $k => [$offsetType, $dimFetch]) {
if ($offsetType === null) {
$offsetValueType = new ConstantArrayType([], []);
$generalizeOnWrite = false;
Expand All @@ -959,7 +960,13 @@
} elseif ($has->maybe()) {
if ($scope->hasExpressionType($dimFetch)->yes()) {
$generalizeOnWrite = false;
$offsetValueType = $offsetValueType->getOffsetValueType($offsetType);
$scopeType = $scope->getType($dimFetch);
$nextOffsetType = $offsetTypes[$k + 1][0] ?? null;
if ($nextOffsetType !== null && $scopeType->hasOffsetValueType($nextOffsetType)->yes()) {

Check warning on line 965 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $generalizeOnWrite = false; $scopeType = $scope->getType($dimFetch); $nextOffsetType = $offsetTypes[$k + 1][0] ?? null; - if ($nextOffsetType !== null && $scopeType->hasOffsetValueType($nextOffsetType)->yes()) { + if ($nextOffsetType !== null && !$scopeType->hasOffsetValueType($nextOffsetType)->no()) { $offsetValueType = $scopeType; } else { $offsetValueType = $offsetValueType->getOffsetValueType($offsetType);

Check warning on line 965 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $generalizeOnWrite = false; $scopeType = $scope->getType($dimFetch); $nextOffsetType = $offsetTypes[$k + 1][0] ?? null; - if ($nextOffsetType !== null && $scopeType->hasOffsetValueType($nextOffsetType)->yes()) { + if ($nextOffsetType !== null && !$scopeType->hasOffsetValueType($nextOffsetType)->no()) { $offsetValueType = $scopeType; } else { $offsetValueType = $offsetValueType->getOffsetValueType($offsetType);
$offsetValueType = $scopeType;
} else {
$offsetValueType = $offsetValueType->getOffsetValueType($offsetType);
}
} else {
$offsetValueType = TypeCombinator::union($offsetValueType->getOffsetValueType($offsetType), new ConstantArrayType([], []));
}
Expand Down
12 changes: 12 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3852,6 +3852,18 @@
continue;
}

if (isset($otherVariableTypeHolders[$variableExprString])) {
$thisType = $variableTypeHolder->getType();
$otherType = $otherVariableTypeHolders[$variableExprString]->getType();
if (
$thisType->isConstantArray()->yes()

Check warning on line 3859 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": @@ @@ $thisType = $variableTypeHolder->getType(); $otherType = $otherVariableTypeHolders[$variableExprString]->getType(); if ( - $thisType->isConstantArray()->yes() + !$thisType->isConstantArray()->no() && $otherType->isConstantArray()->yes() && $thisType->getIterableKeyType()->equals($otherType->getIterableKeyType()) ) {

Check warning on line 3859 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": @@ @@ $thisType = $variableTypeHolder->getType(); $otherType = $otherVariableTypeHolders[$variableExprString]->getType(); if ( - $thisType->isConstantArray()->yes() + !$thisType->isConstantArray()->no() && $otherType->isConstantArray()->yes() && $thisType->getIterableKeyType()->equals($otherType->getIterableKeyType()) ) {
&& $otherType->isConstantArray()->yes()

Check warning on line 3860 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": @@ @@ $otherType = $otherVariableTypeHolders[$variableExprString]->getType(); if ( $thisType->isConstantArray()->yes() - && $otherType->isConstantArray()->yes() + && !$otherType->isConstantArray()->no() && $thisType->getIterableKeyType()->equals($otherType->getIterableKeyType()) ) { break;

Check warning on line 3860 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": @@ @@ $otherType = $otherVariableTypeHolders[$variableExprString]->getType(); if ( $thisType->isConstantArray()->yes() - && $otherType->isConstantArray()->yes() + && !$otherType->isConstantArray()->no() && $thisType->getIterableKeyType()->equals($otherType->getIterableKeyType()) ) { break;
&& $thisType->getIterableKeyType()->equals($otherType->getIterableKeyType())
) {
break;
}
}

continue 2;
}
if (!isset($otherVariableTypeHolders[$variableExprString])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1263,4 +1263,11 @@ public function testBug14308(): void
$this->analyse([__DIR__ . '/data/bug-14308.php'], []);
}

public function testBug13669(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;

$this->analyse([__DIR__ . '/data/bug-13669.php'], []);
}

}
48 changes: 48 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-13669.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types = 1);

namespace Bug13669;

final class Foo
{
/**
* @var array<int, array<MailStatus::CODE_*, int>>
*/
private array $mailCounts;

/** @var array<int, array<MailStatus::CODE_*>> */
private array $sources;

/** @param array<int, array<MailStatus::CODE_*>> $sources */
private function __construct(array $sources)
{
$this->mailCounts = [];
$this->sources = $sources;
}

public function countMailStates(): void
{
foreach ($this->sources as $templateId => $mails) {
$this->mailCounts[$templateId] = [
MailStatus::CODE_DELETED => 0,
MailStatus::CODE_NOT_ACTIVE => 0,
MailStatus::CODE_ACTIVE => 0,
MailStatus::CODE_SIMULATION => 0,
];

foreach ($mails as $mail) {
++$this->mailCounts[$templateId][$mail];
}
}
}
}

final class MailStatus
{
public const CODE_DELETED = -1;

public const CODE_NOT_ACTIVE = 0;

public const CODE_SIMULATION = 1;

public const CODE_ACTIVE = 2;
}
Loading