Skip to content

Commit

Permalink
Fix union result with mysqli (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Nov 7, 2024
1 parent 436e2e9 commit 236aa94
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/MysqliReflection/MysqliResultObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace staabm\PHPStanDba\MysqliReflection;

use PHPStan\TrinaryLogic;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

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

return parent::getIterableValueType();
}

// differentiate objects based on the local properties,
// to make sure TypeCombinator::union() will not normalize separate objects away.
// this means we need to implement equals() and isSuperTypeOf().
public function equals(Type $type): bool
{
if ($type instanceof self) {
return $type->rowType !== null
&& $this->rowType !== null
&& $type->rowType->equals($this->rowType);
}

return parent::equals($type);
}

public function isSuperTypeOf(Type $type): TrinaryLogic
{
if ($type instanceof self) {
return TrinaryLogic::createFromBoolean(
$type->rowType !== null
&& $this->rowType !== null
&& $type->rowType->equals($this->rowType)
);
}

return parent::isSuperTypeOf($type);
}
}
13 changes: 13 additions & 0 deletions tests/default/data/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,17 @@ public function fnQuery(mysqli $mysqli, string $query)
$result = mysqli_query($mysqli, $query);
assertType('mysqli_result|true', $result);
}

public function unionResult(mysqli $mysqli)
{
$queries = ['SELECT adaid FROM ada', 'SELECT email FROM ada'];

foreach ($queries as $query) {
$result = $mysqli->query($query);

foreach ($result as $row) {
assertType('array{adaid: int<-32768, 32767>}|array{email: string}', $row);
}
}
}
}

0 comments on commit 236aa94

Please sign in to comment.