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
4 changes: 4 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@

public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
$originalOffsetType = $offsetType;
$offsetArrayKeyType = $offsetType->toArrayKey();
if ($offsetArrayKeyType instanceof ErrorType) {
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
Expand All @@ -279,6 +280,7 @@
$offsetType = $offsetArrayKeyType;

if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
&& $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no()

Check warning on line 283 in src/Type/ArrayType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $offsetType = $offsetArrayKeyType; if ($this->getKeyType()->isSuperTypeOf($offsetType)->no() - && $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no() + && !$this->getKeyType()->isSuperTypeOf($originalOffsetType)->yes() && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no()) ) { return TrinaryLogic::createNo();

Check warning on line 283 in src/Type/ArrayType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $offsetType = $offsetArrayKeyType; if ($this->getKeyType()->isSuperTypeOf($offsetType)->no() - && $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no() + && $originalOffsetType->isSuperTypeOf($this->getKeyType())->no() && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no()) ) { return TrinaryLogic::createNo();

Check warning on line 283 in src/Type/ArrayType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $offsetType = $offsetArrayKeyType; if ($this->getKeyType()->isSuperTypeOf($offsetType)->no() - && $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no() + && !$this->getKeyType()->isSuperTypeOf($originalOffsetType)->yes() && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no()) ) { return TrinaryLogic::createNo();

Check warning on line 283 in src/Type/ArrayType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $offsetType = $offsetArrayKeyType; if ($this->getKeyType()->isSuperTypeOf($offsetType)->no() - && $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no() + && $originalOffsetType->isSuperTypeOf($this->getKeyType())->no() && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no()) ) { return TrinaryLogic::createNo();
&& ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
) {
return TrinaryLogic::createNo();
Expand All @@ -289,8 +291,10 @@

public function getOffsetValueType(Type $offsetType): Type
{
$originalOffsetType = $offsetType;
$offsetType = $offsetType->toArrayKey();
if ($this->getKeyType()->isSuperTypeOf($offsetType)->no()
&& $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no()

Check warning on line 297 in src/Type/ArrayType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $originalOffsetType = $offsetType; $offsetType = $offsetType->toArrayKey(); if ($this->getKeyType()->isSuperTypeOf($offsetType)->no() - && $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no() + && !$this->getKeyType()->isSuperTypeOf($originalOffsetType)->yes() && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no()) ) { return new ErrorType();

Check warning on line 297 in src/Type/ArrayType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $originalOffsetType = $offsetType; $offsetType = $offsetType->toArrayKey(); if ($this->getKeyType()->isSuperTypeOf($offsetType)->no() - && $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no() + && $originalOffsetType->isSuperTypeOf($this->getKeyType())->no() && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no()) ) { return new ErrorType();

Check warning on line 297 in src/Type/ArrayType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $originalOffsetType = $offsetType; $offsetType = $offsetType->toArrayKey(); if ($this->getKeyType()->isSuperTypeOf($offsetType)->no() - && $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no() + && !$this->getKeyType()->isSuperTypeOf($originalOffsetType)->yes() && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no()) ) { return new ErrorType();

Check warning on line 297 in src/Type/ArrayType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $originalOffsetType = $offsetType; $offsetType = $offsetType->toArrayKey(); if ($this->getKeyType()->isSuperTypeOf($offsetType)->no() - && $this->getKeyType()->isSuperTypeOf($originalOffsetType)->no() + && $originalOffsetType->isSuperTypeOf($this->getKeyType())->no() && ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no()) ) { return new ErrorType();
&& ($offsetType->isString()->no() || !$offsetType->isConstantScalarValue()->no())
) {
return new ErrorType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ public function testRule(): void
'Offset 0 does not exist on array<string, string>.',
111,
],
[
'Offset \'0\' does not exist on array<string, string>.',
112,
],
[
'Offset int does not exist on array<string, string>.',
114,
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,11 @@ public function testBug9503(): void
$this->analyse([__DIR__ . '/data/bug-9503.php'], []);
}

public function testBug4296(): void
{
$this->treatPhpDocTypesAsCertain = true;

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

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

namespace Bug4296;

class Test {
private string $id;

public function __construct(string $id)
{
$this->id = $id;
}

public function getId(): string
{
return $this->id;
}
}

function (): void {
$map = [];
foreach ([new Test('1234')] as $test) {
$map[$test->getId()] = $test;
}

foreach (['1234'] as $value) {
if (isset($map[$value])) {
$found = 1;
}
}
};
Loading