forked from staabm/phpstan-dba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntaxErrorInQueryFunctionRule.php
111 lines (93 loc) · 3.18 KB
/
SyntaxErrorInQueryFunctionRule.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace staabm\PHPStanDba\Rules;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use staabm\PHPStanDba\Tests\SyntaxErrorInQueryFunctionRuleTest;
use staabm\PHPStanDba\UnresolvableQueryException;
/**
* @implements Rule<FuncCall>
*
* @see SyntaxErrorInQueryFunctionRuleTest
*/
final class SyntaxErrorInQueryFunctionRule implements Rule
{
/**
* @var list<string>
*/
private $functionNames;
/**
* @var ReflectionProvider
*/
private $reflectionProvider;
/**
* @param list<string> $functionNames
*/
public function __construct(array $functionNames, ReflectionProvider $reflectionProvider)
{
$this->functionNames = $functionNames;
$this->reflectionProvider = $reflectionProvider;
}
public function getNodeType(): string
{
return FuncCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Name) {
return [];
}
$calledFunctionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
if (null === $calledFunctionName) {
return [];
}
$queryArgPosition = null;
$unsupportedFunction = true;
foreach ($this->functionNames as $functionName) {
sscanf($functionName, '%[^#]#%i', $functionName, $queryArgPosition);
if (!\is_string($functionName) || !\is_int($queryArgPosition)) {
throw new ShouldNotHappenException('Invalid functionName definition');
}
if (strtolower($functionName) === strtolower($calledFunctionName)) {
$unsupportedFunction = false;
break;
}
}
if (null === $queryArgPosition) {
throw new ShouldNotHappenException('Invalid classMethod definition');
}
if ($unsupportedFunction) {
return [];
}
$args = $node->getArgs();
if (!\array_key_exists($queryArgPosition, $args)) {
return [];
}
$queryExpr = $args[$queryArgPosition]->value;
$queryReflection = new QueryReflection();
if ($queryReflection->isResolvable($queryExpr, $scope)->no()) {
return [];
}
try {
foreach ($queryReflection->resolveQueryStrings($queryExpr, $scope) as $queryString) {
$queryError = $queryReflection->validateQueryString($queryString);
if (null !== $queryError) {
return [
RuleErrorBuilder::message($queryError->asRuleMessage())->line($node->getLine())->build(),
];
}
}
} catch (UnresolvableQueryException $exception) {
return [
RuleErrorBuilder::message($exception->asRuleMessage())->tip($exception::getTip())->line($node->getLine())->build(),
];
}
return [];
}
}