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
3 changes: 3 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic
if ($offsetArrayKeyType instanceof ErrorType) {
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
$offsetArrayKeyType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
if ($offsetArrayKeyType instanceof NeverType) {
return TrinaryLogic::createNo();
}
}
$offsetType = $offsetArrayKeyType;

Expand Down
3 changes: 3 additions & 0 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic
if ($offsetArrayKeyType instanceof ErrorType) {
$allowedArrayKeys = AllowedArrayKeysTypes::getType();
$offsetArrayKeyType = TypeCombinator::intersect($allowedArrayKeys, $offsetType)->toArrayKey();
if ($offsetArrayKeyType instanceof NeverType) {
return TrinaryLogic::createNo();
}
}

return $this->recursiveHasOffsetValueType($offsetArrayKeyType);
Expand Down
7 changes: 6 additions & 1 deletion tests/PHPStan/Levels/data/arrayOffsetAccess-3.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"message": "Invalid array key type DateTimeImmutable.",
"line": 17,
"ignorable": true
},
{
"message": "Offset DateTimeImmutable does not exist on array.",
"line": 17,
"ignorable": true
}
]
]
25 changes: 25 additions & 0 deletions tests/PHPStan/Levels/data/arrayOffsetAccess-7.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
[
{
"message": "Offset int|object might not exist on array.",
"line": 19,
"ignorable": true
},
{
"message": "Possibly invalid array key type int|object.",
"line": 19,
"ignorable": true
},
{
"message": "Offset object|null might not exist on array.",
"line": 20,
"ignorable": true
},
{
"message": "Possibly invalid array key type object|null.",
"line": 20,
"ignorable": true
},
{
"message": "Offset DateTimeImmutable might not exist on array|ArrayAccess.",
"line": 26,
"ignorable": true
},
{
"message": "Possibly invalid array key type DateTimeImmutable.",
"line": 26,
"ignorable": true
},
{
"message": "Offset int|object might not exist on array|ArrayAccess.",
"line": 28,
"ignorable": true
},
{
"message": "Possibly invalid array key type int|object.",
"line": 28,
"ignorable": true
},
{
"message": "Offset object|null might not exist on array|ArrayAccess.",
"line": 29,
"ignorable": true
},
{
"message": "Possibly invalid array key type object|null.",
"line": 29,
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Levels/data/stringOffsetAccess-3.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
"message": "Invalid array key type stdClass.",
"line": 59,
"ignorable": true
},
{
"message": "Offset stdClass does not exist on array{baz: 21}|array{foo: 17, bar: 19}.",
"line": 59,
"ignorable": true
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,10 @@ public function testBugObject(): void
'Offset int|object does not exist on array{baz: 21}|array{foo: 17, bar: 19}.',
12,
],
[
'Offset object does not exist on array<string, int>.',
21,
],
]);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Type/ArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,36 @@ public function testResolveTemplateTypes(Type $received, Type $template, array $
);
}

public static function dataHasOffsetValueType(): array
{
return [
[
new ArrayType(new BenevolentUnionType([
new IntegerType(),
new StringType(),
]), new IntegerType()),
new ArrayType(new BenevolentUnionType([
new IntegerType(),
new StringType(),
]), new IntegerType()),
TrinaryLogic::createNo(),
],
];
}

#[DataProvider('dataHasOffsetValueType')]
public function testHasOffsetValueType(
ArrayType $type,
Type $offsetType,
TrinaryLogic $expectedResult,
): void
{
$actualResult = $type->hasOffsetValueType($offsetType);
$this->assertSame(
$expectedResult->describe(),
$actualResult->describe(),
sprintf('%s -> hasOffsetValueType(%s)', $type->describe(VerbosityLevel::precise()), $offsetType->describe(VerbosityLevel::precise())),
);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Type/Constant/ConstantArrayTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,4 +1044,30 @@ public function testValuesArray(ConstantArrayType $type, ConstantArrayType $expe
$this->assertSame($expectedType->getNextAutoIndexes(), $actualType->getNextAutoIndexes());
}

public static function dataHasOffsetValueType(): array
{
return [
[
new ConstantArrayType([new ConstantIntegerType(0)], [new ConstantStringType('a')]),
new ConstantArrayType([new ConstantIntegerType(0)], [new ConstantStringType('a')]),
TrinaryLogic::createNo(),
],
];
}

#[DataProvider('dataHasOffsetValueType')]
public function testHasOffsetValueType(
ConstantArrayType $type,
Type $offsetType,
TrinaryLogic $expectedResult,
): void
{
$actualResult = $type->hasOffsetValueType($offsetType);
$this->assertSame(
$expectedResult->describe(),
$actualResult->describe(),
sprintf('%s -> hasOffsetValueType(%s)', $type->describe(VerbosityLevel::precise()), $offsetType->describe(VerbosityLevel::precise())),
);
}

}
Loading