Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MixedType::hasOffsetValueType() for subtracted types #2135

Merged
merged 1 commit into from Dec 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()) {
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
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())),
);
}

}