Skip to content

Commit 964b59d

Browse files
committed
Use ObjectType instead of GenericObjectType
1 parent 08a068d commit 964b59d

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/Extensions/MysqliQueryDynamicReturnTypeExtension.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,25 @@ private function inferResultType(Expr $queryExpr, Scope $scope): ?Type
121121
$queryReflection = new QueryReflection();
122122
$queryStrings = $queryReflection->resolveQueryStrings($queryExpr, $scope);
123123

124-
$genericObjects = [];
124+
$objects = [];
125125
foreach ($queryStrings as $queryString) {
126126
$resultType = $queryReflection->getResultType($queryString, QueryReflector::FETCH_TYPE_ASSOC);
127127

128128
if (null === $resultType) {
129129
return null;
130130
}
131131

132-
$genericObjects[] = new MysqliResultObjectType($resultType);
132+
$resultObjectType = new MysqliResultObjectType(\mysqli_result::class);
133+
$resultObjectType->setRowType($resultType);
134+
135+
$objects[] = $resultObjectType;
133136
}
134137

135-
if (0 === \count($genericObjects)) {
138+
if (0 === \count($objects)) {
136139
return null;
137140
}
138141

139-
$resultType = TypeCombinator::union(...$genericObjects);
142+
$resultType = TypeCombinator::union(...$objects);
140143

141144
if (! QueryReflection::getRuntimeConfiguration()->throwsMysqliExceptions($this->phpVersion)) {
142145
return TypeCombinator::union(

src/MysqliReflection/MysqliResultObjectType.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,32 @@
44

55
namespace staabm\PHPStanDba\MysqliReflection;
66

7-
use mysqli_result;
8-
use PHPStan\Type\Generic\GenericObjectType;
7+
use PHPStan\ShouldNotHappenException;
8+
use PHPStan\Type\ObjectType;
99
use PHPStan\Type\Type;
1010

11-
final class MysqliResultObjectType extends GenericObjectType
11+
final class MysqliResultObjectType extends ObjectType
1212
{
13-
public function __construct(Type $rowType)
13+
private Type $rowType;
14+
15+
public function __construct(
16+
string $className,
17+
?Type $subtractedType = null
18+
)
1419
{
15-
parent::__construct(mysqli_result::class, [$rowType]);
20+
parent::__construct($className, $subtractedType);
21+
}
22+
23+
public function setRowType(Type $rowType): void {
24+
$this->rowType = $rowType;
25+
}
26+
27+
public function getRowType(): Type
28+
{
29+
if ($this->rowType === null) {
30+
throw new ShouldNotHappenException();
31+
}
32+
33+
return $this->rowType;
1634
}
1735
}

0 commit comments

Comments
 (0)