-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathRepositoryMethodCallRule.php
More file actions
114 lines (97 loc) · 2.74 KB
/
RepositoryMethodCallRule.php
File metadata and controls
114 lines (97 loc) · 2.74 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
<?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\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use PHPStan\Type\GenericTypeVariableResolver;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\VerbosityLevel;
/**
* @implements Rule<Node\Expr\MethodCall>
*/
class RepositoryMethodCallRule implements Rule
{
/** @var ObjectMetadataResolver */
private $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->args[0])) {
return [];
}
$argType = $scope->getType($node->args[0]->value);
if (!$argType instanceof ConstantArrayType) {
return [];
}
if (count($argType->getKeyTypes()) === 0) {
return [];
}
$calledOnType = $scope->getType($node->var);
if (!$calledOnType instanceof TypeWithClassName) {
return [];
}
$entityClassType = GenericTypeVariableResolver::getType($calledOnType, ObjectRepository::class, 'TEntityClass');
if ($entityClassType === null) {
$entityClassType = GenericTypeVariableResolver::getType($calledOnType, \Doctrine\Persistence\ObjectRepository::class, 'TEntityClass');
if ($entityClassType === null) {
return [];
}
}
if (!$entityClassType instanceof TypeWithClassName) {
return [];
}
$entityClass = $entityClassType->getClassName();
if (interface_exists($entityClass)) {
return [];
}
$methodNameIdentifier = $node->name;
if (!$methodNameIdentifier instanceof Node\Identifier) {
return [];
}
$methodName = $methodNameIdentifier->toString();
if (!in_array($methodName, [
'findBy',
'findOneBy',
'count',
], true)) {
return [];
}
$objectManager = $this->objectMetadataResolver->getObjectManager();
if ($objectManager === null) {
return [];
}
$classMetadata = $objectManager->getClassMetadata($entityClass);
$messages = [];
foreach ($argType->getKeyTypes() as $keyType) {
if (!$keyType instanceof ConstantStringType) {
continue;
}
$fieldName = $keyType->getValue();
if (
$classMetadata->hasField($fieldName)
|| $classMetadata->hasAssociation($fieldName)
) {
continue;
}
$messages[] = sprintf(
'Call to method %s::%s() - entity %s does not have a field named $%s.',
$calledOnType->describe(VerbosityLevel::typeOnly()),
$methodName,
$entityClass,
$fieldName
);
}
return $messages;
}
}