-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathRepositoryMethodCallRule.php
More file actions
124 lines (106 loc) · 3.27 KB
/
RepositoryMethodCallRule.php
File metadata and controls
124 lines (106 loc) · 3.27 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Doctrine\ORM;
use Doctrine\Persistence\ObjectRepository;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use PHPStan\Type\VerbosityLevel;
use function count;
use function in_array;
use function sprintf;
/**
* @implements Rule<Node\Expr\MethodCall>
*/
class RepositoryMethodCallRule implements Rule
{
private ObjectMetadataResolver $objectMetadataResolver;
public function __construct(ObjectMetadataResolver $objectMetadataResolver)
{
$this->objectMetadataResolver = $objectMetadataResolver;
}
public function getNodeType(): string
{
return Node\Expr\MethodCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!isset($node->getArgs()[0])) {
return [];
}
$argType = $scope->getType($node->getArgs()[0]->value);
$calledOnType = $scope->getType($node->var);
$entityClassType = $calledOnType->getTemplateType(ObjectRepository::class, 'TEntityClass');
/** @var list<class-string> $entityClassNames */
$entityClassNames = $entityClassType->getObjectClassNames();
if (count($entityClassNames) !== 1) {
return [];
}
$methodNameIdentifier = $node->name;
if (!$methodNameIdentifier instanceof Node\Identifier) {
return [];
}
$methodName = $methodNameIdentifier->toString();
if (!in_array($methodName, [
'findBy',
'findOneBy',
'count',
], true)) {
return [];
}
$classMetadata = $this->objectMetadataResolver->getClassMetadata($entityClassNames[0]);
if ($classMetadata === null) {
return [];
}
$messages = [];
foreach ($argType->getConstantArrays() as $constantArray) {
foreach ($constantArray->getKeyTypes() as $keyType) {
foreach ($keyType->getConstantStrings() as $fieldName) {
if (
$classMetadata->hasField($fieldName->getValue())
|| $classMetadata->hasAssociation($fieldName->getValue())
) {
continue;
}
$messages[] = RuleErrorBuilder::message(sprintf(
'Call to method %s::%s() - entity %s does not have a field named $%s.',
$calledOnType->describe(VerbosityLevel::typeOnly()),
$methodName,
$entityClassNames[0],
$fieldName->getValue(),
))->identifier(sprintf('doctrine.%sArgument', $methodName))->build();
}
}
}
$orderByType = count($node->getArgs()) > 1 ? $scope->getType($node->getArgs()[1]->value) : null;
if (
in_array($methodName, [
'findBy',
'findOneBy',
], true)
&& $orderByType !== null
) {
foreach ($orderByType->getConstantArrays() as $constantArray) {
foreach ($constantArray->getKeyTypes() as $keyType) {
foreach ($keyType->getConstantStrings() as $fieldName) {
if (
$classMetadata->hasField($fieldName->getValue())
|| $classMetadata->hasAssociation($fieldName->getValue())
) {
continue;
}
$messages[] = RuleErrorBuilder::message(sprintf(
'Call to method %s::%s() - entity %s does not have a field named $%s.',
$calledOnType->describe(VerbosityLevel::typeOnly()),
$methodName,
$entityClassNames[0],
$fieldName->getValue(),
))->identifier(sprintf('doctrine.%sArgument', $methodName))->build();
}
}
}
}
return $messages;
}
}