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
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ parameters:
count: 2
path: src/QueryReflection/QuerySimulation.php

-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
identifier: phpstanApi.instanceofType
count: 1
path: src/Extensions/PdoStatementExecuteTypeSpecifyingExtension.php

-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
identifier: phpstanApi.instanceofType
Expand Down
29 changes: 25 additions & 4 deletions src/Extensions/PdoStatementExecuteTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\Type;
use staabm\PHPStanDba\PdoReflection\PdoStatementReflection;
Expand Down Expand Up @@ -62,9 +65,7 @@ private function inferStatementType(MethodReflection $methodReflection, MethodCa
{
$args = $methodCall->getArgs();

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


$stmtReflection = new PdoStatementReflection();
$queryExpr = $stmtReflection->findPrepareQueryStringExpression($methodCall);
Expand All @@ -73,7 +74,27 @@ private function inferStatementType(MethodReflection $methodReflection, MethodCa
}

$queryReflection = new QueryReflection();
$parameterTypes = $queryReflection->resolveParameterTypes($args[0]->value, $scope);

if (0 === \count($args)) {
$parameterKeys = [];
$parameterValues = [];

foreach ($stmtReflection->findPrepareBindCalls($methodCall) as $bindCall) {
$bindArgs = $bindCall->getArgs();
if (\count($bindArgs) >= 2) {
$keyType = $scope->getType($bindArgs[0]->value);
if ($keyType instanceof ConstantIntegerType || $keyType instanceof ConstantStringType) {
$parameterKeys[] = $keyType;
$parameterValues[] = $scope->getType($bindArgs[1]->value);
}
}
Comment thread
vaielab marked this conversation as resolved.
}

$parameterTypes = new ConstantArrayType($parameterKeys, $parameterValues);
} else {
$parameterTypes = $queryReflection->resolveParameterTypes($args[0]->value, $scope);
}

$queryStrings = $queryReflection->resolvePreparedQueryStrings($queryExpr, $parameterTypes, $scope);

$reflectionFetchType = QueryReflection::getRuntimeConfiguration()->getDefaultFetchMode();
Expand Down
28 changes: 20 additions & 8 deletions tests/default/config/.phpunit-phpstan-dba-mysqli.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions tests/default/config/.phpunit-phpstan-dba-pdo-mysql.cache

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions tests/default/data/pdo-stmt-execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Foo
{
public function execute(PDO $pdo)
{

$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
$stmt->execute([':adaid' => 1]);
foreach ($stmt as $row) {
Expand Down Expand Up @@ -38,6 +39,7 @@ public function execute(PDO $pdo)
foreach ($stmt as $row) {
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
}

}

public function executeWithBindCalls(PDO $pdo)
Expand All @@ -48,6 +50,43 @@ public function executeWithBindCalls(PDO $pdo)
$stmt->bindParam(':test1', $test);
$stmt->bindValue(':test2', 1001);
$stmt->execute();

$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
$stmt->bindValue(':adaid', 1);
$stmt->execute();
foreach ($stmt as $row) {
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
}

$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
$stmt->bindValue('adaid', 1);
$stmt->execute(); // prefixed ":" is optional
foreach ($stmt as $row) {
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
}

$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE email = :email');
$stmt->bindValue(':email', 'email@example.org');
$stmt->execute();
foreach ($stmt as $row) {
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
}

$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = ?');
$stmt->bindValue(1, 1);
$stmt->execute();
foreach ($stmt as $row) {
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
}

$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = ? and email = ? ');
$stmt->bindValue(1, 1);
$stmt->bindValue(2, 'email@example.org');
$stmt->execute();
foreach ($stmt as $row) {
assertType('array{email: string, 0: string, adaid: int<-32768, 32767>, 1: int<-32768, 32767>}', $row);
}

}

public function errors(PDO $pdo)
Expand Down
Loading