Skip to content
Merged
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/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ public function isOffsetAccessible(): TrinaryLogic

public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
if ($this->isOffsetAccessible()->no()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes we wonder whether the API is meant in a way that the caller is expected to check with isOffsetAccessible before calling hasOffsetValueType

return TrinaryLogic::createNo();
}

return TrinaryLogic::createMaybe();
}

Expand Down
58 changes: 58 additions & 0 deletions tests/PHPStan/Type/MixedTypeTest.php
Original file line number Diff line number Diff line change
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())),
);
}

}