Skip to content

Commit

Permalink
QueryResultTypeWalker: support pdo_pgsql float fetches on PHP 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored and ondrejmirtes committed Sep 9, 2024
1 parent 8badde6 commit a103cfe
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 150 deletions.
8 changes: 1 addition & 7 deletions src/Type/Doctrine/Descriptors/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,12 @@ public function getDatabaseInternalTypeForDriver(Connection $connection): Type
{
$driverType = $this->driverDetector->detect($connection);

if ($driverType === DriverDetector::PDO_PGSQL) {
return new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
]);
}

if (in_array($driverType, [
DriverDetector::SQLITE3,
DriverDetector::PDO_SQLITE,
DriverDetector::MYSQLI,
DriverDetector::PDO_MYSQL,
DriverDetector::PDO_PGSQL,
DriverDetector::PGSQL,
], true)) {
return new \PHPStan\Type\FloatType();
Expand Down
38 changes: 13 additions & 25 deletions src/Type/Doctrine/Query/QueryResultTypeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,6 @@ public function walkFunction($function): string
}

if ($this->containsOnlyNumericTypes($exprTypeNoNull)) {
if ($this->driverType === DriverDetector::PDO_PGSQL) {
return $this->marshalType($this->createNumericString($nullable));
}

return $this->marshalType($exprType); // retains underlying type
}

Expand Down Expand Up @@ -627,13 +623,7 @@ public function walkFunction($function): string
$type = TypeCombinator::addNull($type);
}

} elseif ($this->driverType === DriverDetector::PDO_PGSQL) {
$type = new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
]);

} elseif ($this->driverType === DriverDetector::PGSQL) {
} elseif ($this->driverType === DriverDetector::PGSQL || $this->driverType === DriverDetector::PDO_PGSQL) {
$castedExprType = $this->castStringLiteralForNumericExpression($exprTypeNoNull);

if ($castedExprType->isInteger()->yes() || $castedExprType->isFloat()->yes()) {
Expand Down Expand Up @@ -1771,12 +1761,6 @@ private function inferPlusMinusTimesType(array $termTypes): Type
return $this->createInteger($nullable);
}

if ($this->driverType === DriverDetector::PDO_PGSQL) {
if ($this->containsOnlyNumericTypes($unionWithoutNull)) {
return $this->createNumericString($nullable);
}
}

if ($this->driverType === DriverDetector::SQLITE3 || $this->driverType === DriverDetector::PDO_SQLITE) {
if (!$this->containsOnlyNumericTypes(...$typesNoNull)) {
return new MixedType();
Expand All @@ -1791,7 +1775,7 @@ private function inferPlusMinusTimesType(array $termTypes): Type
return $this->createFloatOrInt($nullable);
}

if ($this->driverType === DriverDetector::MYSQLI || $this->driverType === DriverDetector::PDO_MYSQL || $this->driverType === DriverDetector::PGSQL) {
if ($this->driverType === DriverDetector::MYSQLI || $this->driverType === DriverDetector::PDO_MYSQL || $this->driverType === DriverDetector::PGSQL || $this->driverType === DriverDetector::PDO_PGSQL) {
if ($this->containsOnlyTypes($unionWithoutNull, [new IntegerType(), new FloatType()])) {
return $this->createFloat($nullable);
}
Expand Down Expand Up @@ -1857,12 +1841,6 @@ private function inferDivisionType(array $termTypes): Type
return new MixedType();
}

if ($this->driverType === DriverDetector::PDO_PGSQL) {
if ($this->containsOnlyTypes($unionWithoutNull, [new IntegerType(), new FloatType(), $this->createNumericString(false)])) {
return $this->createNumericString($nullable);
}
}

if ($this->driverType === DriverDetector::SQLITE3 || $this->driverType === DriverDetector::PDO_SQLITE) {
if (!$this->containsOnlyNumericTypes(...$typesNoNull)) {
return new MixedType();
Expand All @@ -1877,7 +1855,7 @@ private function inferDivisionType(array $termTypes): Type
return $this->createFloatOrInt($nullable);
}

if ($this->driverType === DriverDetector::MYSQLI || $this->driverType === DriverDetector::PDO_MYSQL || $this->driverType === DriverDetector::PGSQL) {
if ($this->driverType === DriverDetector::MYSQLI || $this->driverType === DriverDetector::PDO_MYSQL || $this->driverType === DriverDetector::PGSQL || $this->driverType === DriverDetector::PDO_PGSQL) {
if ($this->containsOnlyTypes($unionWithoutNull, [new IntegerType(), new FloatType()])) {
return $this->createFloat($nullable);
}
Expand Down Expand Up @@ -2100,6 +2078,9 @@ private function hasAggregateWithoutGroupBy(): bool
* - pdo_sqlite: https://github.com/php/php-src/commit/438b025a28cda2935613af412fc13702883dd3a2
* - pdo_pgsql: https://github.com/php/php-src/commit/737195c3ae6ac53b9501cfc39cc80fd462909c82
*
* Notable 8.4 changes:
* - pdo_pgsql: https://github.com/php/php-src/commit/6d10a6989897e9089d62edf939344437128e93ad
*
* @param IntegerType|FloatType|BooleanType $type
*/
private function shouldStringifyExpressions(Type $type): TrinaryLogic
Expand Down Expand Up @@ -2144,7 +2125,14 @@ private function shouldStringifyExpressions(Type $type): TrinaryLogic
}

return TrinaryLogic::createNo();
}

if ($type->isFloat()->yes()) {
if ($this->phpVersion->getVersionId() >= 80400) {
return TrinaryLogic::createFromBoolean($stringifyFetches);
}

return TrinaryLogic::createYes();
}

return TrinaryLogic::createFromBoolean($stringifyFetches);
Expand Down
Loading

0 comments on commit a103cfe

Please sign in to comment.