-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathPdoStatementExecuteTypeSpecifyingExtension.php
More file actions
75 lines (58 loc) · 2.52 KB
/
PdoStatementExecuteTypeSpecifyingExtension.php
File metadata and controls
75 lines (58 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace staabm\PHPStanDba\Extensions;
use PDOStatement;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\Type;
use staabm\PHPStanDba\PdoReflection\PdoStatementReflection;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
final class PdoStatementExecuteTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{
private TypeSpecifier $typeSpecifier;
public function getClass(): string
{
return PDOStatement::class;
}
public function isMethodSupported(MethodReflection $methodReflection, MethodCall $node, TypeSpecifierContext $context): bool
{
return 'execute' === strtolower($methodReflection->getName());
}
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}
public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
// keep original param name because named-parameters
$methodCall = $node;
$inferedType = $this->inferStatementType($methodReflection, $methodCall, $scope);
if (null !== $inferedType) {
return $this->typeSpecifier->create($methodCall->var, $inferedType, TypeSpecifierContext::createTruthy(), true);
}
return new SpecifiedTypes();
}
private function inferStatementType(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
$args = $methodCall->getArgs();
if (0 === \count($args)) {
return null;
}
$stmtReflection = new PdoStatementReflection();
$queryExpr = $stmtReflection->findPrepareQueryStringExpression($methodCall);
if (null === $queryExpr) {
return null;
}
$parameterTypes = $scope->getType($args[0]->value);
$queryReflection = new QueryReflection();
$queryStrings = $queryReflection->resolvePreparedQueryStrings($queryExpr, $parameterTypes, null, $scope);
$reflectionFetchType = QueryReflection::getRuntimeConfiguration()->getDefaultFetchMode();
return $stmtReflection->createGenericStatement($queryStrings, $reflectionFetchType);
}
}