Repository navigation
Support mysqli_result->fetch_assoc() and mysqli_result->fetch_all()
#815
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67df2d0
Support mysqli_result->fetch_assoc
staabm 81e7b9f
MysqliResultDynamicReturnTypeExtension
staabm 42845f8
Merge branch 'main' into assoc
staabm 02f7558
add more expectations
staabm 2dce25f
Handle mysqli result row types
Copilot 4e98824
Implement mysqli fetch_all inference
Copilot 398a7c4
Fix mysqli row conversion PHPStan return type
Copilot 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
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
164 changes: 164 additions & 0 deletions
164
src/Extensions/MysqliResultDynamicReturnTypeExtension.php
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,164 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace staabm\PHPStanDba\Extensions; | ||
|
|
||
| use mysqli_result; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\MethodReflection; | ||
| use PHPStan\Type\Accessory\AccessoryArrayListType; | ||
| use PHPStan\Type\ArrayType; | ||
| use PHPStan\Type\Constant\ConstantArrayType; | ||
| use PHPStan\Type\Constant\ConstantArrayTypeBuilder; | ||
| use PHPStan\Type\Constant\ConstantIntegerType; | ||
| use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
| use PHPStan\Type\IntegerType; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use PHPStan\Type\UnionType; | ||
| use staabm\PHPStanDba\MysqliReflection\MysqliResultObjectType; | ||
|
|
||
| final class MysqliResultDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
| { | ||
| public function getClass(): string | ||
| { | ||
| return mysqli_result::class; | ||
| } | ||
|
|
||
| public function isMethodSupported(MethodReflection $methodReflection): bool | ||
| { | ||
| return \in_array(strtolower($methodReflection->getName()), ['fetch_assoc', 'fetch_all'], true); | ||
| } | ||
|
|
||
| public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type | ||
| { | ||
| $resultType = $scope->getType($methodCall->var); | ||
| $methodName = strtolower($methodReflection->getName()); | ||
| if ('fetch_assoc' === $methodName) { | ||
| if (count($methodCall->getArgs()) === 0) { | ||
| return $this->getRowType($resultType); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| $modes = [\MYSQLI_NUM]; | ||
| if (count($methodCall->getArgs()) > 0) { | ||
| $modes = $this->getModes($scope->getType($methodCall->getArgs()[0]->value)); | ||
| if ($modes === null) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| $rowType = $this->getRowType($resultType); | ||
| if ($rowType === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $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() | ||
| ); | ||
| } | ||
|
|
||
| private function getRowType(Type $resultType): ?Type | ||
| { | ||
| if ($resultType instanceof MysqliResultObjectType) { | ||
| return $resultType->getRowType(); | ||
| } | ||
|
|
||
| if (! $resultType instanceof UnionType) { | ||
| return null; | ||
| } | ||
|
|
||
| $rowTypes = []; | ||
| foreach ($resultType->getTypes() as $type) { | ||
| $rowType = $this->getRowType($type); | ||
| if ($rowType === null) { | ||
| return null; | ||
| } | ||
|
|
||
| $rowTypes[] = $rowType; | ||
| } | ||
|
|
||
| return TypeCombinator::union(...$rowTypes); | ||
| } | ||
|
|
||
| /** | ||
| * @return list<int>|null | ||
| */ | ||
| private function getModes(Type $modeType): ?array | ||
| { | ||
| if ($modeType instanceof ConstantIntegerType) { | ||
| return [$modeType->getValue()]; | ||
| } | ||
|
|
||
| if (! $modeType instanceof UnionType) { | ||
| return null; | ||
| } | ||
|
|
||
| $modes = []; | ||
| foreach ($modeType->getTypes() as $type) { | ||
| $typeModes = $this->getModes($type); | ||
| if ($typeModes === null) { | ||
| return null; | ||
| } | ||
|
|
||
| foreach ($typeModes as $mode) { | ||
| $modes[] = $mode; | ||
| } | ||
| } | ||
|
|
||
| return $modes; | ||
| } | ||
|
|
||
| private function getRowTypeForMode(Type $rowType, int $mode): ?Type | ||
| { | ||
| if (! \in_array($mode, [\MYSQLI_NUM, \MYSQLI_ASSOC, \MYSQLI_BOTH], true)) { | ||
| return null; | ||
| } | ||
|
|
||
| $rowTypes = []; | ||
| foreach ($rowType->getConstantArrays() as $arrayType) { | ||
| $rowTypes[] = $this->convertRowType($arrayType, $mode); | ||
| } | ||
|
|
||
| if (count($rowTypes) === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return TypeCombinator::union(...$rowTypes); | ||
| } | ||
|
|
||
| private function convertRowType(ConstantArrayType $rowType, int $mode): Type | ||
| { | ||
| $builder = ConstantArrayTypeBuilder::createEmpty(); | ||
| $numericIndex = 0; | ||
|
|
||
| foreach ($rowType->getKeyTypes() as $index => $keyType) { | ||
| $valueType = $rowType->getValueTypes()[$index]; | ||
| if ($mode === \MYSQLI_ASSOC || $mode === \MYSQLI_BOTH) { | ||
| $builder->setOffsetValueType($keyType, $valueType); | ||
| } | ||
|
|
||
| if ($mode === \MYSQLI_NUM || $mode === \MYSQLI_BOTH) { | ||
| $builder->setOffsetValueType(new ConstantIntegerType($numericIndex), $valueType); | ||
| $numericIndex++; | ||
| } | ||
| } | ||
|
|
||
| return $builder->getArray(); | ||
| } | ||
| } |
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
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
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.