Skip to content
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

wip: prepared param type validation #65

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
93 changes: 93 additions & 0 deletions .phpstan-dba.cache
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,99 @@
)),
),
),
'SELECT email, adaid FROM ada WHERE adaid = \'hello world\'' =>
array (
'error' => NULL,
'result' =>
array (
3 =>
PHPStan\Type\Constant\ConstantArrayType::__set_state(array(
'keyType' =>
PHPStan\Type\UnionType::__set_state(array(
'types' =>
array (
0 =>
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
'value' => 0,
)),
1 =>
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
'value' => 1,
)),
2 =>
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
'value' => 'adaid',
'isClassString' => false,
)),
3 =>
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
'value' => 'email',
'isClassString' => false,
)),
),
)),
'itemType' =>
PHPStan\Type\UnionType::__set_state(array(
'types' =>
array (
0 =>
PHPStan\Type\IntegerRangeType::__set_state(array(
'min' => 0,
'max' => 4294967295,
)),
1 =>
PHPStan\Type\StringType::__set_state(array(
)),
),
)),
'allArrays' => NULL,
'keyTypes' =>
array (
0 =>
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
'value' => 'email',
'isClassString' => false,
)),
1 =>
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
'value' => 0,
)),
2 =>
PHPStan\Type\Constant\ConstantStringType::__set_state(array(
'value' => 'adaid',
'isClassString' => false,
)),
3 =>
PHPStan\Type\Constant\ConstantIntegerType::__set_state(array(
'value' => 1,
)),
),
'valueTypes' =>
array (
0 =>
PHPStan\Type\StringType::__set_state(array(
)),
1 =>
PHPStan\Type\StringType::__set_state(array(
)),
2 =>
PHPStan\Type\IntegerRangeType::__set_state(array(
'min' => 0,
'max' => 4294967295,
)),
3 =>
PHPStan\Type\IntegerRangeType::__set_state(array(
'min' => 0,
'max' => 4294967295,
)),
),
'nextAutoIndex' => 2,
'optionalKeys' =>
array (
),
)),
),
),
'SELECT email, adaid FROM ada WHERE adaid = 1' =>
array (
'error' => NULL,
Expand Down
80 changes: 76 additions & 4 deletions src/Rules/PdoStatementExecuteErrorMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@

use PDOStatement;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use staabm\PHPStanDba\PdoReflection\PdoStatementReflection;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use staabm\PHPStanDba\QueryReflection\QueryReflector;

/**
* @implements Rule<MethodCall>
Expand Down Expand Up @@ -81,13 +87,13 @@ private function checkErrors(MethodReflection $methodReflection, MethodCall $met
];
}

return $this->checkParameterValues($methodCall, $scope, $queryString, $placeholderCount);
return $this->checkParameterValues($methodCall, $scope, $queryExpr, $queryString, $placeholderCount);
}

/**
* @return RuleError[]
*/
private function checkParameterValues(MethodCall $methodCall, Scope $scope, string $queryString, int $placeholderCount): array
private function checkParameterValues(MethodCall $methodCall, Scope $scope, Expr $queryExpr, string $queryString, int $placeholderCount): array
{
$queryReflection = new QueryReflection();
$args = $methodCall->getArgs();
Expand All @@ -97,8 +103,11 @@ private function checkParameterValues(MethodCall $methodCall, Scope $scope, stri
if (null === $parameters) {
return [];
}
$parameterCount = \count($parameters);
if (!$parameterTypes instanceof ConstantArrayType) {
throw new ShouldNotHappenException();
}

$parameterCount = \count($parameters);
if ($parameterCount !== $placeholderCount) {
if (1 === $parameterCount) {
return [
Expand All @@ -125,9 +134,72 @@ private function checkParameterValues(MethodCall $methodCall, Scope $scope, stri
}
}

return $errors;
if ($errors != []) {
return $errors;
}

return $this->checkParameterTypes($methodCall, $queryExpr, $parameterTypes, $scope);
}

/**
* @return RuleError[]
*/
private function checkParameterTypes(MethodCall $methodCall, Expr $queryExpr, ConstantArrayType $parameterTypes, Scope $scope): array
{
$queryReflection = new QueryReflection();
$queryString = $queryReflection->resolvePreparedQueryString($queryExpr, $parameterTypes, $scope);
if (null === $queryString) {
return [];
}

$resultType = $queryReflection->getResultType($queryString, QueryReflector::FETCH_TYPE_ASSOC);
if (!$resultType instanceof ConstantArrayType) {
return [];
}

var_dump($queryString);
foreach ($resultType->getKeyTypes() as $keyType) {
var_dump($keyType->describe(VerbosityLevel::value()));
}

$errors = [];
$keyTypes = $parameterTypes->getKeyTypes();
$valueTypes = $parameterTypes->getValueTypes();

foreach ($keyTypes as $i => $keyType) {
if (!$keyType instanceof ConstantStringType) {
continue;
}

$columnName = $keyType->getValue();
ltrim($columnName, ':');

var_dump($keyType->describe(VerbosityLevel::precise()));
var_dump($resultType->hasOffsetValueType($keyType)->describe());
if (!$resultType->hasOffsetValueType($keyType)->yes()) {
// we only know types of columns which are selected.
continue;
}

var_dump($resultType->getOffsetValueType($keyType)->describe(VerbosityLevel::precise()));
var_dump($valueTypes[$i]->describe(VerbosityLevel::precise()));
if ($resultType->getOffsetValueType($keyType)->isSuperTypeOf($valueTypes[$i])->yes()) {
continue;
}

$errors[] = RuleErrorBuilder::message(
sprintf(
'Value %s with type %s is given to execute(), but the query expects %s.',
$placeholderName,
$resultType->getOffsetValueType($keyType)->describe(VerbosityLevel::precise()),
$valueTypes[$i]->describe(VerbosityLevel::precise())
)
)->line($methodCall->getLine())->build();
}

return $errors;
}

/**
* @return 0|positive-int
*/
Expand Down
2 changes: 0 additions & 2 deletions tests/PdoStatementExecuteErrorMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public function testSyntaxErrorInQueryRule(): void
'Value :wrongParamName is given to execute(), but the query does not containt this placeholder.',
15,
],
/*
[
'Query expects placeholder :adaid, but it is missing from values given to execute().',
18,
Expand All @@ -46,7 +45,6 @@ public function testSyntaxErrorInQueryRule(): void
'Value :wrongParamValue is given to execute(), but the query does not containt this placeholder.',
18,
],
*/
[
'Query expects 1 placeholders, but no values are given to execute().',
21,
Expand Down
4 changes: 2 additions & 2 deletions tests/data/pdo-stmt-execute-error.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function errors(PDO $pdo)
$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
$stmt->execute([':wrongParamName' => 1]);

// $stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
// $stmt->execute([':adaid' => 'hello world']); // wrong param type
$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
$stmt->execute([':adaid' => 'hello world']); // wrong param type

$stmt = $pdo->prepare('SELECT email, adaid FROM ada WHERE adaid = :adaid');
$stmt->execute(); // missing parameter
Expand Down