Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support mysqli_fetch* type inference #416

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
110 changes: 110 additions & 0 deletions src/Extensions/MysqliFetchFnsDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba\Extensions;

use mysqli;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use staabm\PHPStanDba\MysqliReflection\MysqliResultObjectType;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use staabm\PHPStanDba\QueryReflection\QueryReflector;
use staabm\PHPStanDba\UnresolvableQueryException;

final class MysqliQueryDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
/**
* @var PhpVersion
*/
private $phpVersion;

public function __construct(PhpVersion $phpVersion)
{
$this->phpVersion = $phpVersion;
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array(strtolower($functionReflection->getName()), ['mysqli_fetch_assoc', 'mysqli_fetch_row', 'mysqli_fetch_object', 'mysqli_fetch_array'], true);
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$args = $functionCall->getArgs();
$defaultReturn = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();

if (QueryReflection::getRuntimeConfiguration()->throwsMysqliExceptions($this->phpVersion)) {
$defaultReturn = TypeCombinator::remove($defaultReturn, new ConstantBooleanType(false));
}

if (\count($args) < 1) {
return $defaultReturn;
}

try {
$resultType = $this->inferResultType($args[1]->value, $scope);
if (null !== $resultType) {
return $resultType;
}
} catch (UnresolvableQueryException $e) {
// simulation not possible.. use default value
}

return $defaultReturn;
}

/**
* @throws UnresolvableQueryException
*/
private function inferResultType(Expr $mysqliExpr, Scope $scope): ?Type
{
$mysqliType = $scope->getType($mysqliExpr);

if (!$mysqliType instanceof MysqliResultObjectType) {
return null;
}

$queryReflection = new QueryReflection();
$queryStrings = $queryReflection->resolveQueryStrings($mysqliExpr, $scope);

$genericObjects = [];
foreach ($queryStrings as $queryString) {
$resultType = $queryReflection->getResultType($queryString, QueryReflector::FETCH_TYPE_ASSOC);

if (null === $resultType) {
return null;
}

$genericObjects[] = new MysqliResultObjectType($resultType);
}

if (0 === \count($genericObjects)) {
return null;
}

$resultType = TypeCombinator::union(...$genericObjects);

if (!QueryReflection::getRuntimeConfiguration()->throwsMysqliExceptions($this->phpVersion)) {
return TypeCombinator::union(
$resultType,
new ConstantBooleanType(false),
);
}

return $resultType;
}
}
9 changes: 9 additions & 0 deletions src/MysqliReflection/MysqliResultReflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba\MysqliReflection;

final class MysqliResultReflection {

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

public function fnFetchTypes(mysqli $mysqli)
{
$result = mysqli_query($mysqli, 'SELECT email, adaid FROM ada');
assertType('mysqli_result<array{email: string, adaid: int<-32768, 32767>}>', $result);

while ($row = mysqli_fetch_assoc($result)) {
assertType('array{email: string, adaid: int<-32768, 32767>}', $row);
}

while ($row = mysqli_fetch_row($result)) {
assertType('array{string, int<-32768, 32767>}', $row);
}

while ($row = mysqli_fetch_object($result)) {
assertType('int<-32768, 32767>', $row->adaid);
assertType('string', $row->email);
}

while ($row = mysqli_fetch_array($result)) {
assertType('array{0: string, 1: int<-32768, 32767>, email: string, adaid: int<-32768, 32767>}', $row);
}
}
}