Skip to content

Commit 2281019

Browse files
committed
Fix union result with mysqli
1 parent 436e2e9 commit 2281019

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/MysqliReflection/MysqliResultObjectType.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace staabm\PHPStanDba\MysqliReflection;
66

7+
use PHPStan\TrinaryLogic;
78
use PHPStan\Type\ObjectType;
89
use PHPStan\Type\Type;
910

@@ -24,4 +25,31 @@ public function getIterableValueType(): \PHPStan\Type\Type
2425

2526
return parent::getIterableValueType();
2627
}
28+
29+
// differentiate objects based on the local properties,
30+
// to make sure TypeCombinator::union() will not normalize separate objects away.
31+
// this means we need to implement equals() and isSuperTypeOf().
32+
public function equals(Type $type): bool
33+
{
34+
if ($type instanceof self) {
35+
return $type->rowType !== null
36+
&& $this->rowType !== null
37+
&& $type->rowType->equals($this->rowType);
38+
}
39+
40+
return parent::equals($type);
41+
}
42+
43+
public function isSuperTypeOf(Type $type): TrinaryLogic
44+
{
45+
if ($type instanceof self) {
46+
return TrinaryLogic::createFromBoolean(
47+
$type->rowType !== null
48+
&& $this->rowType !== null
49+
&& $type->rowType->equals($this->rowType)
50+
);
51+
}
52+
53+
return parent::isSuperTypeOf($type);
54+
}
2755
}

tests/default/data/mysqli.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,17 @@ public function fnQuery(mysqli $mysqli, string $query)
4444
$result = mysqli_query($mysqli, $query);
4545
assertType('mysqli_result|true', $result);
4646
}
47+
48+
public function unionResult(mysqli $mysqli)
49+
{
50+
$queries = ['SELECT adaid FROM ada', 'SELECT email FROM ada'];
51+
52+
foreach ($queries as $query) {
53+
$result = $mysqli->query($query);
54+
55+
foreach ($result as $row) {
56+
assertType('array{adaid: int<-32768, 32767>}|array{email: string}', $row);
57+
}
58+
}
59+
}
4760
}

0 commit comments

Comments
 (0)