Skip to content

Commit

Permalink
Fix MixedType::hasOffsetValueType() for subtracted types
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Dec 21, 2022
1 parent 4a72d15 commit 506a4d9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Type/MixedType.php
Expand Up @@ -514,6 +514,10 @@ public function isOffsetAccessible(): TrinaryLogic

public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
if ($this->isOffsetAccessible()->no()) {
return TrinaryLogic::createNo();
}

return TrinaryLogic::createMaybe();
}

Expand Down
58 changes: 58 additions & 0 deletions tests/PHPStan/Type/MixedTypeTest.php
Expand Up @@ -1030,4 +1030,62 @@ public function testSubstractedIsOffsetAccessible(MixedType $mixedType, Type $ty
);
}

public function dataSubtractedHasOffsetValueType(): array
{
return [
[
new MixedType(),
new ArrayType(new MixedType(), new MixedType()),
new StringType(),
TrinaryLogic::createMaybe(),
],
[
new MixedType(),
new StringType(),
new StringType(),
TrinaryLogic::createMaybe(),
],
[
new MixedType(),
new ObjectType(ArrayAccess::class),
new StringType(),
TrinaryLogic::createMaybe(),
],
[
new MixedType(),
new UnionType([
new ArrayType(new MixedType(), new MixedType()),
new StringType(),
new ObjectType(ArrayAccess::class),
]),
new StringType(),
TrinaryLogic::createNo(),
],
[
new MixedType(),
new UnionType([
new ArrayType(new MixedType(), new MixedType()),
new StringType(),
new ObjectType(ArrayAccess::class),
new FloatType(),
]),
new StringType(),
TrinaryLogic::createNo(),
],
];
}

/** @dataProvider dataSubtractedHasOffsetValueType */
public function testSubtractedHasOffsetValueType(MixedType $mixedType, Type $typeToSubtract, Type $offsetType, TrinaryLogic $expectedResult): void
{
$subtracted = $mixedType->subtract($typeToSubtract);
$actualResult = $subtracted->hasOffsetValueType($offsetType);

$this->assertSame(
$expectedResult->describe(),
$actualResult->describe(),
sprintf('%s -> hasOffsetValueType()', $subtracted->describe(VerbosityLevel::precise())),
);
}

}

0 comments on commit 506a4d9

Please sign in to comment.