Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getClass(): string

public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool
{
return 'execute' === $methodReflection->getName();
return 'execute' === strtolower($methodReflection->getName());
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by improvement

}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
Expand Down
32 changes: 5 additions & 27 deletions src/Extensions/PdoStatementFetchDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use staabm\PHPStanDba\PdoReflection\PdoStatementReflection;
use staabm\PHPStanDba\QueryReflection\QueryReflection;

final class PdoStatementFetchDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
Expand Down Expand Up @@ -69,16 +70,6 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
$args = $methodCall->getArgs();
$statementType = $scope->getType($methodCall->var);

if (!$statementType instanceof GenericObjectType) {
return null;
}

$genericTypes = $statementType->getTypes();

if (1 !== \count($genericTypes)) {
return null;
}

$fetchType = PDO::FETCH_BOTH;
if (\count($args) > 0) {
$fetchModeType = $scope->getType($args[0]->value);
Expand All @@ -92,23 +83,10 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
}
}

$resultType = $genericTypes[0];

if ((PDO::FETCH_NUM === $fetchType || PDO::FETCH_ASSOC === $fetchType) && $resultType instanceof ConstantArrayType) {
$builder = ConstantArrayTypeBuilder::createEmpty();

$keyTypes = $resultType->getKeyTypes();
$valueTypes = $resultType->getValueTypes();

foreach ($keyTypes as $i => $keyType) {
if (PDO::FETCH_NUM === $fetchType && $keyType instanceof ConstantIntegerType) {
$builder->setOffsetValueType($keyType, $valueTypes[$i]);
} elseif (PDO::FETCH_ASSOC === $fetchType && $keyType instanceof ConstantStringType) {
$builder->setOffsetValueType($keyType, $valueTypes[$i]);
}
}

$resultType = $builder->getArray();
$pdoStatementReflection = new PdoStatementReflection();
$resultType = $pdoStatementReflection->getStatementResultType($statementType, $fetchType);
if ($resultType === null) {
return null;
}

if ('fetchAll' === $methodReflection->getName()) {
Expand Down
45 changes: 45 additions & 0 deletions src/PdoReflection/PdoStatementReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

namespace staabm\PHPStanDba\PdoReflection;

use PDO;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\Type;
use staabm\PHPStanDba\QueryReflection\ExpressionFinder;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use staabm\PHPStanDba\QueryReflection\QueryReflector;

final class PdoStatementReflection
{
Expand All @@ -25,4 +34,40 @@ public function findPrepareQueryStringExpression(MethodReflection $methodReflect

return null;
}

/**
* // the following param doesnt work, see phpstan bug https://github.com/phpstan/phpstan/issues/6577
* @xx-param PDO::FETCH* $fetchType
* @return Type|null
*/
public function getStatementResultType(Type $statementType, int $fetchType) {
if (!$statementType instanceof GenericObjectType) {
return null;
}

$genericTypes = $statementType->getTypes();
if (1 !== \count($genericTypes)) {
return null;
}

$resultType = $genericTypes[0];
if ((PDO::FETCH_NUM === $fetchType || PDO::FETCH_ASSOC === $fetchType) && $resultType instanceof ConstantArrayType) {
$builder = ConstantArrayTypeBuilder::createEmpty();

$keyTypes = $resultType->getKeyTypes();
$valueTypes = $resultType->getValueTypes();

foreach ($keyTypes as $i => $keyType) {
if (PDO::FETCH_NUM === $fetchType && $keyType instanceof ConstantIntegerType) {
$builder->setOffsetValueType($keyType, $valueTypes[$i]);
} elseif (PDO::FETCH_ASSOC === $fetchType && $keyType instanceof ConstantStringType) {
$builder->setOffsetValueType($keyType, $valueTypes[$i]);
}
}

return $builder->getArray();
}

return $resultType;
}
}