From 766fb49cec78af8e4606983ff97a1b87e178862a Mon Sep 17 00:00:00 2001 From: Brian Foley Date: Sat, 7 Nov 2026 18:13:24 +0000 Subject: [PATCH 1/6] Get func arg default from the stubs rather than hardcoding them --- ...MysqliResultDynamicReturnTypeExtension.php | 44 ++++++++++++------- .../config/.phpunit-phpstan-dba-mysqli.cache | 15 +++++++ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/Extensions/MysqliResultDynamicReturnTypeExtension.php b/src/Extensions/MysqliResultDynamicReturnTypeExtension.php index 833ac0f9..4185a533 100644 --- a/src/Extensions/MysqliResultDynamicReturnTypeExtension.php +++ b/src/Extensions/MysqliResultDynamicReturnTypeExtension.php @@ -12,7 +12,9 @@ use PHPStan\Analyser\Scope; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\MethodReflection; +use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\ReflectionProvider; +use PHPStan\ShouldNotHappenException; use PHPStan\Type\Accessory\AccessoryArrayListType; use PHPStan\Type\ArrayType; use PHPStan\Type\Constant\ConstantArrayType; @@ -105,10 +107,9 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, // The return type is stdClass w. SQL fields as properties|null|false if ($name === 'mysqli_fetch_object') { - $countExpr = (\count($args) >= 2) - ? $args[1]->value - : new String_('stdClass'); - $classType = $this->getExprObjectType($scope, $countExpr); + // arg[1] is string $class = "stdClass" + $classNameType = $this->getArgOrDefault($functionReflection, $functionCall, $scope, 1); + $classType = $this->getObjectTypeFromName($scope, $classNameType); if ($classType === null) { return null; } @@ -159,10 +160,8 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, if ($name === 'mysqli_fetch_column') { $columnTypes = []; if (count($args) === 1 || count($args) === 2) { - // arg[1] is int $column, is optional and defaults to 0 - $offsetType = count($args) === 2 - ? $scope->getType($args[1]->value) - : new ConstantIntegerType(0); + // arg[1] is int $column = 0 + $offsetType = $this->getArgOrDefault($functionReflection, $functionCall, $scope, 1); // * column arg is a constant OR // * column arg may be one of several possible constants // Occurs with, eg, column=rand() ? 0 : 1 OR @@ -197,17 +196,28 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, return null; } - /*** - * Evaluate an expression for all possible class name strings it may - * produce and generate ObjectTypes for each of those. - * - * @return A union of possible ObjectTypes, or null on failure - */ - private function getExprObjectType(Scope $scope, Expr $expr): ?Type + private function getArgOrDefault(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope, int $i): Type + { + $args = $funcCall->getArgs(); + if (\count($args) > $i) { + return $scope->getType($args[$i]->value); + } + $variant = ParametersAcceptorSelector::selectFromArgs( + $scope, + $args, + $functionReflection->getVariants() + ); + $default = $variant->getParameters()[$i]->getDefaultValue(); + if ($default === null) { + throw new ShouldNotHappenException("Argument $i does not have a default"); + } + return $default; + } + + private function getObjectTypeFromName(Scope $scope, Type $name): ?Type { - $classNameType = $scope->getType($expr); $objectTypes = []; - foreach ($classNameType->getConstantStrings() as $type) { + foreach ($name->getConstantStrings() as $type) { $className = $type->getValue(); // Does PHPStan know about the class? diff --git a/tests/default/config/.phpunit-phpstan-dba-mysqli.cache b/tests/default/config/.phpunit-phpstan-dba-mysqli.cache index f561321c..3026c3fb 100644 --- a/tests/default/config/.phpunit-phpstan-dba-mysqli.cache +++ b/tests/default/config/.phpunit-phpstan-dba-mysqli.cache @@ -77,6 +77,9 @@ FROM ada' => ), 'SELECT adaid FROM ada LIMIT 1 FOR SHARE' => array ( + 'error' => + \staabm\PHPStanDba\Valid::__set_state(array( + )), 'result' => array ( 5 => @@ -127,6 +130,9 @@ FROM ada' => ), 'SELECT adaid FROM ada LIMIT 1 OFFSET 1' => array ( + 'error' => + \staabm\PHPStanDba\Valid::__set_state(array( + )), 'result' => array ( 5 => @@ -137,6 +143,9 @@ FROM ada' => ), 'SELECT adaid FROM ada LIMIT 1 OFFSET 1 FOR UPDATE' => array ( + 'error' => + \staabm\PHPStanDba\Valid::__set_state(array( + )), 'result' => array ( 5 => @@ -227,6 +236,9 @@ FROM ada' => ), 'SELECT akid FROM ak WHERE eadavk>1.0' => array ( + 'error' => + \staabm\PHPStanDba\Valid::__set_state(array( + )), 'result' => array ( 5 => @@ -547,6 +559,9 @@ FROM ada' => ), 'SELECT email, adaid FROM ada WHERE gesperrt = \'1\'' => array ( + 'error' => + \staabm\PHPStanDba\Valid::__set_state(array( + )), 'result' => array ( 5 => From edc304cbb8d63963192103b0e9f5da67b117be1c Mon Sep 17 00:00:00 2001 From: Brian Foley Date: Mon, 9 Nov 2026 22:38:11 +0000 Subject: [PATCH 2/6] Add fetch_column/fetch_object/fetch_row methods & mysqli_fetch_all func --- .phpstan-dba-mysqli.cache | 43 ++- ...MysqliResultDynamicReturnTypeExtension.php | 289 ++++++++++++------ tests/default/data/mysqli-fetch-column.php | 34 +++ tests/default/data/mysqli.php | 64 ++++ 4 files changed, 304 insertions(+), 126 deletions(-) diff --git a/.phpstan-dba-mysqli.cache b/.phpstan-dba-mysqli.cache index 862d8236..6b6b6679 100644 --- a/.phpstan-dba-mysqli.cache +++ b/.phpstan-dba-mysqli.cache @@ -1,6 +1,6 @@ 'v13-valid-query', - 'schemaHash' => NULL, + 'schemaHash' => '62696dcc12b773f626b1f2862c6c4e99', 'records' => array ( 'SELECT @@ -21,39 +21,30 @@ 'SELECT MD5( GROUP_CONCAT( - InnerSelect.columns + CONCAT( + COALESCE(COLUMN_NAME, \'\'), + COALESCE(EXTRA, \'\'), + COLUMN_TYPE, + IS_NULLABLE, + ORDINAL_POSITION + ) + ORDER BY TABLE_NAME, COLUMN_NAME ) - ) AS dbsignature, - 1 AS grouper - FROM ( - SELECT - CONCAT( - COALESCE(COLUMN_NAME, \'\'), - COALESCE(EXTRA, \'\'), - COLUMN_TYPE, - IS_NULLABLE - ) as columns - FROM - information_schema.columns - WHERE - table_schema = DATABASE() - ORDER BY table_name, column_name - ) as InnerSelect - GROUP BY - grouper' => + ) AS dbsignature + FROM + information_schema.columns + WHERE + table_schema = DATABASE()' => array ( - 'error' => - \staabm\PHPStanDba\Valid::__set_state(array( - )), 'result' => array ( 5 => array ( - 'type-description' => 'array{dbsignature: string|null, 0: string|null, grouper: int, 1: int}', + 'type-description' => 'array{dbsignature: string|null, 0: string|null}', ), 3 => array ( - 'type-description' => 'array{dbsignature: string|null, grouper: int}', + 'type-description' => 'array{dbsignature: string|null}', ), ), ), @@ -65,7 +56,7 @@ array ( 5 => array ( - 'type-description' => 'array{COLUMN_NAME: string, 0: string, COLUMN_DEFAULT: string|null, 1: string|null, IS_NULLABLE: string, 2: string}', + 'type-description' => 'array{COLUMN_NAME: string|null, 0: string|null, COLUMN_DEFAULT: string|null, 1: string|null, IS_NULLABLE: string, 2: string}', ), ), ), diff --git a/src/Extensions/MysqliResultDynamicReturnTypeExtension.php b/src/Extensions/MysqliResultDynamicReturnTypeExtension.php index 4185a533..76a069c8 100644 --- a/src/Extensions/MysqliResultDynamicReturnTypeExtension.php +++ b/src/Extensions/MysqliResultDynamicReturnTypeExtension.php @@ -5,13 +5,13 @@ namespace staabm\PHPStanDba\Extensions; use mysqli_result; -use PhpParser\Node\Expr; +use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; -use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\FunctionReflection; use PHPStan\Reflection\MethodReflection; +use PHPStan\Reflection\ParametersAcceptor; use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\ReflectionProvider; use PHPStan\ShouldNotHappenException; @@ -58,18 +58,110 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo { return \in_array( strtolower($functionReflection->getName()), - ['mysqli_fetch_assoc', 'mysqli_fetch_column', 'mysqli_fetch_object', 'mysqli_fetch_row'], + ['mysqli_fetch_all', 'mysqli_fetch_assoc', 'mysqli_fetch_column', 'mysqli_fetch_object', 'mysqli_fetch_row'], true ); } + /** Process mysqli_fetch_all func & fetch_all method + * @param list $modes + **/ + public function fetchAll(array $modes, Type $rowType): ?Type + { + $rows = []; + foreach ($modes as $mode) { + $modeRowType = $this->getRowTypeForMode($rowType, $mode); + if ($modeRowType === null) { + return null; + } + + $rows[] = $modeRowType; + } + + return TypeCombinator::intersect( + new ArrayType(new IntegerType(), TypeCombinator::union(...$rows)), + new AccessoryArrayListType() + ); + } + + /** + * Process mysqli_fetch_object func & fetch_object method + */ + public function fetchObject(Scope $scope, Type $classNameType, Type $rowType): ?Type + { + $classType = $this->getObjectTypeFromName($scope, $classNameType); + if ($classType === null) { + return null; + } + $objectTypes = []; + foreach ($rowType->getConstantArrays() as $assocRow) { + $properties = []; + foreach ($assocRow->getKeyTypes() as $index => $keyType) { + // Only keep string column names + $columnNames = $keyType->getConstantStrings(); + foreach ($columnNames as $columnName) { + $properties[$columnName->getValue()] = $assocRow->getValueTypes()[$index]; + } + } + + $objectTypes[] = TypeCombinator::intersect( + $classType, + new ObjectShapeType($properties, []) + ); + } + + if ($objectTypes === []) { + return null; + } + + return TypeCombinator::union( + TypeCombinator::union(...$objectTypes), + new NullType(), + new ConstantBooleanType(false) + ); + } + + /** + * Process mysqli_fetch_column func & fetch_column method + */ + public function fetchColumn(Type $offsetType, Type $numericRowType): ?Type + { + // * column arg is a constant OR + // * column arg may be one of several possible constants + // Occurs with, eg, column=rand() ? 0 : 1 OR + // * column arg may be one of several possible constants + // Occurs in, eg, a for loop + // If any of the types are non-integral, it isn't a valid + // call to mysqli_fetch_column + $columnTypes = []; + foreach ($offsetType->getFiniteTypes() as $type) { + if (! $type instanceof ConstantIntegerType) { + return null; + } + foreach ($numericRowType->getConstantArrays() as $numericRow) { + $columnTypes[] = + $numericRow->hasOffsetValueType($offsetType)->yes() + ? $numericRow->getOffsetValueType($offsetType) + : new ConstantBooleanType(false); + } + } + + if ($columnTypes === []) { + return null; + } + + // The ...|false on DB connection failure + $columnTypes[] = new ConstantBooleanType(false); + return TypeCombinator::union(...$columnTypes); + } + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type { $args = $functionCall->getArgs(); - $name = strtolower($functionReflection->getName()); + $functionName = strtolower($functionReflection->getName()); // mysqli_fetch_ functions all have at least 1 arg: the mysqli_result - switch ($name) { + switch ($functionName) { case 'mysqli_fetch_assoc': // $result case 'mysqli_fetch_row': // $result if (\count($args) !== 1) { @@ -96,7 +188,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, } // The return type is sealed array w. SQL fields as elements|null|false - if ($name === 'mysqli_fetch_assoc') { + if ($functionName === 'mysqli_fetch_assoc') { return TypeCombinator::union( $rowType, new NullType(), @@ -106,40 +198,20 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, // The return type is stdClass w. SQL fields as properties|null|false - if ($name === 'mysqli_fetch_object') { + if ($functionName === 'mysqli_fetch_object') { // arg[1] is string $class = "stdClass" - $classNameType = $this->getArgOrDefault($functionReflection, $functionCall, $scope, 1); - $classType = $this->getObjectTypeFromName($scope, $classNameType); - if ($classType === null) { - return null; - } - - $objectTypes = []; - foreach ($rowType->getConstantArrays() as $assocRow) { - $properties = []; - foreach ($assocRow->getKeyTypes() as $index => $keyType) { - // Only keep string column names - $columnNames = $keyType->getConstantStrings(); - foreach ($columnNames as $columnName) { - $properties[$columnName->getValue()] = $assocRow->getValueTypes()[$index]; - } - } - - $objectTypes[] = TypeCombinator::intersect( - $classType, - new ObjectShapeType($properties, []) - ); - } + $classNameType = $this->getArgOrDefault($functionReflection->getVariants(), $functionCall, $scope, 1); + return $this->fetchObject($scope, $classNameType, $rowType); + } - if ($objectTypes === []) { + if ($functionName === 'mysqli_fetch_all') { + $modesType = $this->getArgOrDefault($functionReflection->getVariants(), $functionCall, $scope, 1); + $modes = $this->getModes($modesType); + if ($modes === null) { return null; } - return TypeCombinator::union( - TypeCombinator::union(...$objectTypes), - new NullType(), - new ConstantBooleanType(false) - ); + return $this->fetchAll($modes, $rowType); } $numericRowType = $this->getRowTypeForMode($rowType, self::MYSQLI_NUM); @@ -148,7 +220,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, } // The return type is enumerated array of SQL fields|null|false - if ($name === 'mysqli_fetch_row') { + if ($functionName === 'mysqli_fetch_row') { return TypeCombinator::union( $numericRowType, new NullType(), @@ -157,55 +229,28 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, } // The return type is a single SQL field|false - if ($name === 'mysqli_fetch_column') { - $columnTypes = []; - if (count($args) === 1 || count($args) === 2) { - // arg[1] is int $column = 0 - $offsetType = $this->getArgOrDefault($functionReflection, $functionCall, $scope, 1); - // * column arg is a constant OR - // * column arg may be one of several possible constants - // Occurs with, eg, column=rand() ? 0 : 1 OR - // * column arg may be one of several possible constants - // Occurs in, eg, a for loop - // If any of the types are non-integral, it isn't a valid - // call to mysqli_fetch_column - foreach ($offsetType->getFiniteTypes() as $type) { - if (! $type instanceof ConstantIntegerType) { - return null; - } - foreach ($numericRowType->getConstantArrays() as $numericRow) { - $columnTypes[] = - $numericRow->hasOffsetValueType($offsetType)->yes() - ? $numericRow->getOffsetValueType($offsetType) - : new ConstantBooleanType(false); - } - } - } else { - return null; - } - - if ($columnTypes === []) { - return null; - } - - // The ...|false on DB connection failure - $columnTypes[] = new ConstantBooleanType(false); - return TypeCombinator::union(...$columnTypes); + if ($functionName === 'mysqli_fetch_column') { + // arg[1] is int $column = 0 + $offsetType = $this->getArgOrDefault($functionReflection->getVariants(), $functionCall, $scope, 1); + return $this->fetchColumn($offsetType, $numericRowType); } return null; } - private function getArgOrDefault(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope, int $i): Type + /** + * @param list $variants + */ + private function getArgOrDefault(array $variants, CallLike $call, Scope $scope, int $i): Type { - $args = $funcCall->getArgs(); + $args = $call->getArgs(); if (\count($args) > $i) { return $scope->getType($args[$i]->value); } $variant = ParametersAcceptorSelector::selectFromArgs( $scope, $args, - $functionReflection->getVariants() + $variants ); $default = $variant->getParameters()[$i]->getDefaultValue(); if ($default === null) { @@ -233,55 +278,99 @@ private function getObjectTypeFromName(Scope $scope, Type $name): ?Type public function isMethodSupported(MethodReflection $methodReflection): bool { - return \in_array(strtolower($methodReflection->getName()), ['fetch_assoc', 'fetch_all'], true); + return \in_array( + strtolower($methodReflection->getName()), + ['fetch_all', 'fetch_assoc', 'fetch_column', 'fetch_object', 'fetch_row'], + true + ); } public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type { $resultType = $scope->getType($methodCall->var); + $args = $methodCall->getArgs(); $methodName = strtolower($methodReflection->getName()); - if ('fetch_assoc' === $methodName) { - if (count($methodCall->getArgs()) === 0) { - $rowType = $this->getRowType($resultType); - if ($rowType !== null) { - return TypeCombinator::union( - $rowType, - new NullType(), - new ConstantBooleanType(false) - ); + + switch ($methodName) { + case 'fetch_all': + if (\count($args) > 1) { + return null; } - } + break; + case 'fetch_assoc': + case 'fetch_row': + if (\count($args) !== 0) { + return null; + } + break; + case 'fetch_object': // $class = 'stdClass', $constructor = [] + if (\count($args) > 2) { + return null; + } + break; + case 'fetch_column': // $column = 0; added in PHP 8.1 + $phpVersion = $scope->getPhpVersion()->getType(); + if (\count($args) > 1 || + IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($phpVersion)->no()) { + return null; + } + break; + } + $rowType = $this->getRowType($resultType); + if ($rowType === null) { return null; } - $modes = [self::MYSQLI_NUM]; - if (count($methodCall->getArgs()) > 0) { - $modes = $this->getModes($scope->getType($methodCall->getArgs()[0]->value)); + // The return type is sealed array w. SQL fields as elements|null|false + if ($methodName === 'fetch_assoc') { + return TypeCombinator::union( + $rowType, + new NullType(), + new ConstantBooleanType(false) + ); + } + + // The return type is stdClass w. SQL fields as properties|null|false + if ($methodName === 'fetch_object') { + // arg[0] is string $class = "stdClass" + $classNameType = $this->getArgOrDefault($methodReflection->getVariants(), $methodCall, $scope, 0); + return $this->fetchObject($scope, $classNameType, $rowType); + } + + if ($methodName === 'fetch_all') { + $modesType = $this->getArgOrDefault($methodReflection->getVariants(), $methodCall, $scope, 0); + $modes = $this->getModes($modesType); if ($modes === null) { return null; } + + return $this->fetchAll($modes, $rowType); } - $rowType = $this->getRowType($resultType); - if ($rowType === null) { + $numericRowType = $this->getRowTypeForMode($rowType, self::MYSQLI_NUM); + if ($numericRowType === null) { return null; } - $rows = []; - foreach ($modes as $mode) { - $modeRowType = $this->getRowTypeForMode($rowType, $mode); - if ($modeRowType === null) { - return null; - } + // The return type is enumerated array of SQL fields|null|false + if ($methodName === 'fetch_row') { + return TypeCombinator::union( + $numericRowType, + new NullType(), + new ConstantBooleanType(false) + ); + } - $rows[] = $modeRowType; + // The return type is a single SQL field|false + if ($methodName === 'fetch_column') { + $columnTypes = []; + // arg[0] is int $column = 0 + $offsetType = $this->getArgOrDefault($methodReflection->getVariants(), $methodCall, $scope, 0); + return $this->fetchColumn($offsetType, $numericRowType); } - return TypeCombinator::intersect( - new ArrayType(new IntegerType(), TypeCombinator::union(...$rows)), - new AccessoryArrayListType() - ); + return null; } private function getRowType(Type $resultType): ?Type diff --git a/tests/default/data/mysqli-fetch-column.php b/tests/default/data/mysqli-fetch-column.php index d02c2d35..711df504 100644 --- a/tests/default/data/mysqli-fetch-column.php +++ b/tests/default/data/mysqli-fetch-column.php @@ -7,6 +7,40 @@ class Foo { + public function fetchColumn(mysqli $mysqli) + { + $result = mysqli_query($mysqli, 'SELECT c_varchar255, c_smallint, c_float FROM typemix'); + + // default column + $col0 = $result->fetch_column(); + assertType('string|false', $col0); + + // explicit column + $col1 = $result->fetch_column(1); + assertType('int<-32768, 32767>|false', $col1); + + // out-of-range column + $col999 = $result->fetch_column(999); + assertType('false', $col999); + + // multiple possible columns + $col0_or_1 = $result->fetch_column(rand() ? 0 : 1); + assertType('int<-32768, 32767>|string|false', $col0_or_1); + + // range of possible columns + for($i=0; $i<3; $i++) { + $col0_to_2 = mysqli_fetch_column($result, $i); + assertType('float|int<-32768, 32767>|string|false', $col0_to_2); + } + + // Function added in PHP 8.1 + if (PHP_VERSION_ID < 80100) { + $unknown = $result->fetch_column(); + // TODO PHPStan shouldn't recognize this method at all <8.1. Fix stubs? + assertType('float|int|string|false|null', $unknown); + } + } + public function proceduralFetchColumn(mysqli $mysqli) { $result = mysqli_query($mysqli, 'SELECT c_varchar255, c_smallint, c_float FROM typemix'); diff --git a/tests/default/data/mysqli.php b/tests/default/data/mysqli.php index e3cef85c..b0d8c44d 100644 --- a/tests/default/data/mysqli.php +++ b/tests/default/data/mysqli.php @@ -116,6 +116,31 @@ public function fetchAllUnionMode(mysqli $mysqli) assertType('list}>', $result->fetch_all(MYSQLI_ASSOC)); } + public function fetchObject(mysqli $mysqli) + { + $result = mysqli_query($mysqli, 'SELECT email, adaid FROM ada'); + $row = $result->fetch_object(); + assertType('(object{email: string, adaid: int<-32768, 32767>}&stdClass)|false|null', $row); + + $row = $result->fetch_object('AdaClass1'); + assertType('AdaClass1|false|null', $row); + + $row = $result->fetch_object(rand() ? 'AdaClass1' : 'AdaClass2'); + assertType('AdaClass1|AdaClass2|false|null', $row); + + for($i = 1; $i < 3; $i++) { + $row = $result->fetch_object('AdaClass' . $i); + assertType('AdaClass1|AdaClass2|false|null', $row); + } + } + + public function fetchRow(mysqli $mysqli) + { + $result = mysqli_query($mysqli, 'SELECT email, adaid FROM ada'); + $row = $result->fetch_row(); + assertType('array{string, int<-32768, 32767>}|false|null', $row); + } + public function proceduralFetchAssoc(mysqli $mysqli) { $result = mysqli_query($mysqli, 'SELECT email, adaid FROM ada'); @@ -123,6 +148,45 @@ public function proceduralFetchAssoc(mysqli $mysqli) assertType('array{email: string, adaid: int<-32768, 32767>}|false|null', $row); } + public function proceduralFetchAllColumns(mysqli $mysqli) + { + $result = $mysqli->query('SELECT email, adaid FROM ada'); + assertType('list}>', mysqli_fetch_all($result)); + assertType('list}>', mysqli_fetch_all($result, MYSQLI_ASSOC)); + assertType('list}>', mysqli_fetch_all($result, MYSQLI_NUM)); + assertType('list, 1: int<-32768, 32767>}>', mysqli_fetch_all($result, MYSQLI_BOTH)); + assertType('list}>', mysqli_fetch_all($result, 1)); + assertType('list}>', mysqli_fetch_all($result, 2)); + assertType('list, 1: int<-32768, 32767>}>', mysqli_fetch_all($result, 3)); + assertType('array{email: string, adaid: int<-32768, 32767>}|false|null', mysqli_fetch_assoc($result)); + foreach ($result as $row) { + assertType('array{email: string, adaid: int<-32768, 32767>}', $row); + } + } + + public function proceduralFetchAllUnionResult(mysqli $mysqli) + { + $queries = ['SELECT adaid FROM ada', 'SELECT email FROM ada']; + + foreach ($queries as $query) { + $result = $mysqli->query($query); + assertType('list}|array{string}>', mysqli_fetch_all($result)); + assertType('list}|array{email: string}>', mysqli_fetch_all($result, MYSQLI_ASSOC)); + assertType('list}|array{string}>', mysqli_fetch_all($result,MYSQLI_NUM)); + assertType('list, 0: int<-32768, 32767>}|array{email: string, 0: string}>', mysqli_fetch_all($result, MYSQLI_BOTH)); + assertType('array{adaid: int<-32768, 32767>}|array{email: string}|false|null', mysqli_fetch_assoc($result)); + } + } + + public function proceduralFetchAllUnionMode(mysqli $mysqli) + { + $result = $mysqli->query('SELECT adaid FROM ada'); + $mode = rand(0, 1) ? MYSQLI_ASSOC : MYSQLI_NUM; + assertType('list}|array{int<-32768, 32767>}>', mysqli_fetch_all($result, $mode)); + assertType('list, 0: int<-32768, 32767>}>', mysqli_fetch_all($result, MYSQLI_ASSOC | MYSQLI_NUM)); + assertType('list}>', mysqli_fetch_all($result, MYSQLI_ASSOC)); + } + public function proceduralFetchObject(mysqli $mysqli) { $result = mysqli_query($mysqli, 'SELECT email, adaid FROM ada'); From 1c11d5cb35427ca5260af07780c0f83b8f798935 Mon Sep 17 00:00:00 2001 From: Brian Foley Date: Tue, 10 Nov 2026 10:34:55 +0000 Subject: [PATCH 3/6] Add named argument test for fetch_column method & procedure --- tests/default/data/mysqli-fetch-column.php | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/default/data/mysqli-fetch-column.php b/tests/default/data/mysqli-fetch-column.php index 711df504..7f99e5e8 100644 --- a/tests/default/data/mysqli-fetch-column.php +++ b/tests/default/data/mysqli-fetch-column.php @@ -39,6 +39,18 @@ public function fetchColumn(mysqli $mysqli) // TODO PHPStan shouldn't recognize this method at all <8.1. Fix stubs? assertType('float|int|string|false|null', $unknown); } + + // explict column (named arg) + $col1 = $result->fetch_column(column: 1); + assertType('int<-32768, 32767>|false', $col1); + + // out-of-range column (named arg) + $col999 = $result->fetch_column(column: 999); + assertType('false', $col999); + + // multiple possible columns (named arg) + $col0_or_1 = $result->fetch_column(column: rand() ? 0 : 1); + assertType('int<-32768, 32767>|string|false', $col0_or_1); } public function proceduralFetchColumn(mysqli $mysqli) @@ -73,5 +85,17 @@ public function proceduralFetchColumn(mysqli $mysqli) // TODO PHPStan shouldn't recognize this function at all <8.1. Fix stubs? assertType('float|int|string|false|null', $unknown); } + + // explicit column (named arg) + $col1 = mysqli_fetch_column($result, column: 1); + assertType('int<-32768, 32767>|false', $col1); + + // out-of-range column (named arg) + $col999 = mysqli_fetch_column($result, column: 999); + assertType('false', $col999); + + // multiple possible columns (named arg) + $col0_or_1 = mysqli_fetch_column($result, column: rand() ? 0 : 1); + assertType('int<-32768, 32767>|string|false', $col0_or_1); } } From 18e39d14821cd09d364fb532fccea7d0efe4041d Mon Sep 17 00:00:00 2001 From: Brian Foley Date: Tue, 10 Nov 2026 10:54:26 +0000 Subject: [PATCH 4/6] Add data file for named arg test only, and add a lint >= 8.0 directive --- tests/default/DbaInferenceTest.php | 1 + tests/default/data/mysqli-fetch-column.php | 24 ------- tests/default/data/mysqli-named-args.php | 82 ++++++++++++++++++++++ 3 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 tests/default/data/mysqli-named-args.php diff --git a/tests/default/DbaInferenceTest.php b/tests/default/DbaInferenceTest.php index eeb0f437..ab58ac50 100644 --- a/tests/default/DbaInferenceTest.php +++ b/tests/default/DbaInferenceTest.php @@ -60,6 +60,7 @@ public function dataFileAsserts(): iterable // PHP >= 8.1.0 if (function_exists('mysqli_fetch_column')) { yield from $this->gatherAssertTypes(__DIR__ . '/data/mysqli-fetch-column.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/mysqli-named-args.php'); } } diff --git a/tests/default/data/mysqli-fetch-column.php b/tests/default/data/mysqli-fetch-column.php index 7f99e5e8..711df504 100644 --- a/tests/default/data/mysqli-fetch-column.php +++ b/tests/default/data/mysqli-fetch-column.php @@ -39,18 +39,6 @@ public function fetchColumn(mysqli $mysqli) // TODO PHPStan shouldn't recognize this method at all <8.1. Fix stubs? assertType('float|int|string|false|null', $unknown); } - - // explict column (named arg) - $col1 = $result->fetch_column(column: 1); - assertType('int<-32768, 32767>|false', $col1); - - // out-of-range column (named arg) - $col999 = $result->fetch_column(column: 999); - assertType('false', $col999); - - // multiple possible columns (named arg) - $col0_or_1 = $result->fetch_column(column: rand() ? 0 : 1); - assertType('int<-32768, 32767>|string|false', $col0_or_1); } public function proceduralFetchColumn(mysqli $mysqli) @@ -85,17 +73,5 @@ public function proceduralFetchColumn(mysqli $mysqli) // TODO PHPStan shouldn't recognize this function at all <8.1. Fix stubs? assertType('float|int|string|false|null', $unknown); } - - // explicit column (named arg) - $col1 = mysqli_fetch_column($result, column: 1); - assertType('int<-32768, 32767>|false', $col1); - - // out-of-range column (named arg) - $col999 = mysqli_fetch_column($result, column: 999); - assertType('false', $col999); - - // multiple possible columns (named arg) - $col0_or_1 = mysqli_fetch_column($result, column: rand() ? 0 : 1); - assertType('int<-32768, 32767>|string|false', $col0_or_1); } } diff --git a/tests/default/data/mysqli-named-args.php b/tests/default/data/mysqli-named-args.php new file mode 100644 index 00000000..42a75d86 --- /dev/null +++ b/tests/default/data/mysqli-named-args.php @@ -0,0 +1,82 @@ += 8.0 + +namespace MysqliFetchColumnNamedArgTest; + +use mysqli; +use function PHPStan\Testing\assertType; + +class AdaClass1 { +} + +class AdaClass2 { +} + +class Foo +{ + public function fetchColumnNamedArg(mysqli $mysqli) + { + $result = mysqli_query($mysqli, 'SELECT c_varchar255, c_smallint, c_float FROM typemix'); + + // explict column (named arg) + $col1 = $result->fetch_column(column: 1); + assertType('int<-32768, 32767>|false', $col1); + + // out-of-range column (named arg) + $col999 = $result->fetch_column(column: 999); + assertType('false', $col999); + + // multiple possible columns (named arg) + $col0_or_1 = $result->fetch_column(column: rand() ? 0 : 1); + assertType('int<-32768, 32767>|string|false', $col0_or_1); + } + + public function proceduralFetchColumnNamedArg(mysqli $mysqli) + { + $result = mysqli_query($mysqli, 'SELECT c_varchar255, c_smallint, c_float FROM typemix'); + + // explicit column (named arg) + $col1 = mysqli_fetch_column($result, column: 1); + assertType('int<-32768, 32767>|false', $col1); + + // out-of-range column (named arg) + $col999 = mysqli_fetch_column($result, column: 999); + assertType('false', $col999); + + // multiple possible columns (named arg) + $col0_or_1 = mysqli_fetch_column($result, column: rand() ? 0 : 1); + assertType('int<-32768, 32767>|string|false', $col0_or_1); + } + + public function mysqliResultfetchNamedArg(mysqli $mysqli) + { + $result = mysqli_query($mysqli, 'SELECT adaid FROM ada'); + assertType('list}>', $result->fetch_all(mode: MYSQLI_ASSOC)); + assertType('list}>', $result->fetch_all(mode: MYSQLI_NUM)); + } + + public function fetchObjectNamedArg(mysqli $mysqli) + { + $result = mysqli_query($mysqli, 'SELECT email, adaid FROM ada'); + + $row = $result->fetch_object(class: 'AdaClass1'); + assertType('AdaClass1|false|null', $row); + $row = $result->fetch_object(class: rand() ? 'AdaClass1' : 'AdaClass2'); + assertType('AdaClass1|AdaClass2|false|null', $row); + } + + public function proceduralFetchAllColumnsNamedArg(mysqli $mysqli) + { + $result = $mysqli->query('SELECT email, adaid FROM ada'); + assertType('list}>', mysqli_fetch_all($result, mode: MYSQLI_ASSOC)); + assertType('list}>', mysqli_fetch_all($result, mode: MYSQLI_NUM)); + } + + public function proceduralFetchObjectNamedArg(mysqli $mysqli) + { + $result = mysqli_query($mysqli, 'SELECT email, adaid FROM ada'); + + $row = mysqli_fetch_object($result, class: rand() ? 'AdaClass1' : 'AdaClass2'); + assertType('AdaClass1|AdaClass2|false|null', $row); + + } +} From 3efef1c56f72d1b52349a60b0c6b5646432d79fd Mon Sep 17 00:00:00 2001 From: Brian Foley Date: Thu, 12 Nov 2026 17:10:39 +0000 Subject: [PATCH 5/6] Woops. Should use $result->fetch_column in OO tests --- tests/default/data/mysqli-fetch-column.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/default/data/mysqli-fetch-column.php b/tests/default/data/mysqli-fetch-column.php index 711df504..bfdf30aa 100644 --- a/tests/default/data/mysqli-fetch-column.php +++ b/tests/default/data/mysqli-fetch-column.php @@ -29,7 +29,7 @@ public function fetchColumn(mysqli $mysqli) // range of possible columns for($i=0; $i<3; $i++) { - $col0_to_2 = mysqli_fetch_column($result, $i); + $col0_to_2 = $result->fetch_column($i); assertType('float|int<-32768, 32767>|string|false', $col0_to_2); } From ca27a692a7f610b3621d2fae2e6d59031f813294 Mon Sep 17 00:00:00 2001 From: Brian Foley Date: Fri, 13 Nov 2026 12:19:57 +0000 Subject: [PATCH 6/6] Reduce getVariants repetition --- .../MysqliResultDynamicReturnTypeExtension.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Extensions/MysqliResultDynamicReturnTypeExtension.php b/src/Extensions/MysqliResultDynamicReturnTypeExtension.php index 76a069c8..789520d5 100644 --- a/src/Extensions/MysqliResultDynamicReturnTypeExtension.php +++ b/src/Extensions/MysqliResultDynamicReturnTypeExtension.php @@ -158,6 +158,7 @@ public function fetchColumn(Type $offsetType, Type $numericRowType): ?Type public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type { $args = $functionCall->getArgs(); + $variants = $functionReflection->getVariants(); $functionName = strtolower($functionReflection->getName()); // mysqli_fetch_ functions all have at least 1 arg: the mysqli_result @@ -200,12 +201,12 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, // The return type is stdClass w. SQL fields as properties|null|false if ($functionName === 'mysqli_fetch_object') { // arg[1] is string $class = "stdClass" - $classNameType = $this->getArgOrDefault($functionReflection->getVariants(), $functionCall, $scope, 1); + $classNameType = $this->getArgOrDefault($variants, $functionCall, $scope, 1); return $this->fetchObject($scope, $classNameType, $rowType); } if ($functionName === 'mysqli_fetch_all') { - $modesType = $this->getArgOrDefault($functionReflection->getVariants(), $functionCall, $scope, 1); + $modesType = $this->getArgOrDefault($variants, $functionCall, $scope, 1); $modes = $this->getModes($modesType); if ($modes === null) { return null; @@ -231,7 +232,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection, // The return type is a single SQL field|false if ($functionName === 'mysqli_fetch_column') { // arg[1] is int $column = 0 - $offsetType = $this->getArgOrDefault($functionReflection->getVariants(), $functionCall, $scope, 1); + $offsetType = $this->getArgOrDefault($variants, $functionCall, $scope, 1); return $this->fetchColumn($offsetType, $numericRowType); } @@ -289,6 +290,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method { $resultType = $scope->getType($methodCall->var); $args = $methodCall->getArgs(); + $variants = $methodReflection->getVariants(); $methodName = strtolower($methodReflection->getName()); switch ($methodName) { @@ -334,12 +336,12 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method // The return type is stdClass w. SQL fields as properties|null|false if ($methodName === 'fetch_object') { // arg[0] is string $class = "stdClass" - $classNameType = $this->getArgOrDefault($methodReflection->getVariants(), $methodCall, $scope, 0); + $classNameType = $this->getArgOrDefault($variants, $methodCall, $scope, 0); return $this->fetchObject($scope, $classNameType, $rowType); } if ($methodName === 'fetch_all') { - $modesType = $this->getArgOrDefault($methodReflection->getVariants(), $methodCall, $scope, 0); + $modesType = $this->getArgOrDefault($variants, $methodCall, $scope, 0); $modes = $this->getModes($modesType); if ($modes === null) { return null; @@ -366,7 +368,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method if ($methodName === 'fetch_column') { $columnTypes = []; // arg[0] is int $column = 0 - $offsetType = $this->getArgOrDefault($methodReflection->getVariants(), $methodCall, $scope, 0); + $offsetType = $this->getArgOrDefault($variants, $methodCall, $scope, 0); return $this->fetchColumn($offsetType, $numericRowType); }