-
Notifications
You must be signed in to change notification settings - Fork 574
Return list<mixed> from PDOStatement::fetchAll()
#5643
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
Merged
VincentLanglet
merged 5 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-e4psdj6
May 15, 2026
+147
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6500e71
Introduce PdoStatementFetchAllReturnTypeExtension
VincentLanglet e56988d
Fix
VincentLanglet baf366c
Fix
VincentLanglet c86f9e6
Add test for list|false return type on PHP < 8.0
phpstan-bot cc14171
Update tests/PHPStan/Analyser/nsrt/bug-11889-php7.php
VincentLanglet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Php; | ||
|
|
||
| use PDO; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Reflection\ParametersAcceptorSelector; | ||
| use PHPStan\Type\Accessory\AccessoryArrayListType; | ||
| use PHPStan\Type\ArrayType; | ||
| use PHPStan\Type\Constant\ConstantBooleanType; | ||
| use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
| use PHPStan\Type\IntegerType; | ||
| use PHPStan\Type\MixedType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use PHPStan\Type\TypeUtils; | ||
| use function count; | ||
|
|
||
| #[AutowiredService] | ||
| final class PdoStatementFetchAllReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
| { | ||
|
|
||
| public function getClass(): string | ||
| { | ||
| return 'PDOStatement'; | ||
| } | ||
|
|
||
| public function isMethodSupported(MethodReflection $methodReflection): bool | ||
| { | ||
| return $methodReflection->getName() === 'fetchAll'; | ||
| } | ||
|
|
||
| public function getTypeFromMethodCall( | ||
| MethodReflection $methodReflection, | ||
| MethodCall $methodCall, | ||
| Scope $scope, | ||
| ): ?Type | ||
| { | ||
| $args = $methodCall->getArgs(); | ||
| if (count($args) < 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $modeType = $scope->getType($args[0]->value); | ||
| $constantIntegers = TypeUtils::getConstantIntegers($modeType); | ||
|
|
||
| if (count($constantIntegers) === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| foreach ($constantIntegers as $constantInteger) { | ||
| $mode = $constantInteger->getValue(); | ||
| if ( | ||
| ($mode & 0xFFFF) === PDO::FETCH_KEY_PAIR | ||
| || ($mode & PDO::FETCH_GROUP) !== 0 | ||
| || ($mode & PDO::FETCH_UNIQUE) !== 0 | ||
| ) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| $variant = ParametersAcceptorSelector::selectFromArgs($scope, $args, $methodReflection->getVariants()); | ||
| $returnType = $variant->getReturnType(); | ||
|
|
||
| $listType = TypeCombinator::intersect( | ||
| new ArrayType(new IntegerType(), new MixedType()), | ||
| new AccessoryArrayListType(), | ||
| ); | ||
|
|
||
| if (!$returnType->isFalse()->no()) { | ||
|
Check warning on line 73 in src/Type/Php/PdoStatementFetchAllReturnTypeExtension.php
|
||
| return TypeCombinator::union($listType, new ConstantBooleanType(false)); | ||
| } | ||
|
|
||
| return $listType; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php // lint < 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug11889Php7; | ||
|
|
||
| use PDO; | ||
| use PDOStatement; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function test(PDOStatement $stmt): void | ||
| { | ||
| assertType('list|false', $stmt->fetchAll(PDO::FETCH_ASSOC)); | ||
| assertType('list|false', $stmt->fetchAll(PDO::FETCH_COLUMN)); | ||
|
|
||
| // Non-list modes | ||
| assertType('array|false', $stmt->fetchAll(PDO::FETCH_KEY_PAIR)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug11889; | ||
|
|
||
| use PDO; | ||
| use PDOStatement; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function test(PDOStatement $stmt): void | ||
| { | ||
| // No mode argument - unknown default, stays as array | ||
| assertType('array', $stmt->fetchAll()); | ||
|
|
||
| // Single-argument modes that return lists | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_ASSOC)); | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_NUM)); | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_BOTH)); | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_OBJ)); | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_COLUMN)); | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_CLASS)); | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_NAMED)); | ||
|
|
||
| // Modes that return non-list arrays | ||
| assertType('array', $stmt->fetchAll(PDO::FETCH_KEY_PAIR)); | ||
| assertType('array', $stmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC)); | ||
| assertType('array', $stmt->fetchAll(PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC)); | ||
|
|
||
| // Multi-argument overload variants always return lists | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_COLUMN, 0)); | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_CLASS, \stdClass::class)); | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_CLASS, \stdClass::class, [])); | ||
| assertType('list', $stmt->fetchAll(PDO::FETCH_FUNC, function () { | ||
| return 'test'; | ||
| })); | ||
| } | ||
|
|
||
| /** | ||
| * @return list<string> | ||
| */ | ||
| function get_cv_files(): array | ||
| { | ||
| $pdo = new PDO(""); | ||
| $stmt = $pdo->prepare('SELECT `file` FROM `commonvoice`'); | ||
| $stmt->execute(); | ||
|
|
||
| return $stmt->fetchAll(PDO::FETCH_COLUMN); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.