forked from CakeDC/cakephp-phpstan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssociationTableMixinClassReflectionExtension.php
More file actions
115 lines (99 loc) · 3.74 KB
/
Copy pathAssociationTableMixinClassReflectionExtension.php
File metadata and controls
115 lines (99 loc) · 3.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
115
<?php
declare(strict_types=1);
/**
* @source https://github.com/cakephp/cakephp
*/
namespace CakeDC\PHPStan\Method;
use Cake\ORM\Association;
use Cake\ORM\Table;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\PropertiesClassReflectionExtension;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
class AssociationTableMixinClassReflectionExtension implements
PropertiesClassReflectionExtension,
MethodsClassReflectionExtension
{
/**
* @var \PHPStan\Reflection\ReflectionProvider
*/
protected ReflectionProvider $reflectionProvider;
/**
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
*/
public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}
/**
* @return \PHPStan\Reflection\ClassReflection
*/
protected function getTableReflection(): ClassReflection
{
return $this->reflectionProvider->getClass(Table::class);
}
/**
* @param \PHPStan\Reflection\ClassReflection $classReflection Class reflection
* @param string $methodName Method name
* @return bool
*/
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
// magic findBy* method
if ($classReflection->is(Table::class) && preg_match('/^find(?:\w+)?By/', $methodName) > 0) {
return true;
}
if (!$classReflection->is(Association::class)) {
return false;
}
// magic findBy* method on Association
if (preg_match('/^find(?:\w+)?By/', $methodName) > 0) {
return true;
}
return $this->getTableReflection()->hasMethod($methodName);
}
/**
* @param \PHPStan\Reflection\ClassReflection $classReflection Class reflection
* @param string $methodName Method name
* @return \PHPStan\Reflection\MethodReflection
*/
public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
// magic findBy* method
if ($classReflection->is(Table::class) && preg_match('/^find(?:\w+)?By/', $methodName) > 0) {
return new TableFindByPropertyMethodReflection($methodName, $classReflection);
}
// magic findBy* method on Association
$associationReflection = $this->reflectionProvider->getClass(Association::class);
if (
$classReflection->isSubclassOfClass($associationReflection)
&& preg_match('/^find(?:\w+)?By/', $methodName) > 0
) {
return new TableFindByPropertyMethodReflection($methodName, $this->getTableReflection());
}
return $this->getTableReflection()->getNativeMethod($methodName);
}
/**
* @param \PHPStan\Reflection\ClassReflection $classReflection Class reflection
* @param string $propertyName Method name
* @return bool
*/
public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
if (!$classReflection->is(Association::class)) {
return false;
}
return $this->getTableReflection()->hasInstanceProperty($propertyName);
}
/**
* @param \PHPStan\Reflection\ClassReflection $classReflection Class reflection
* @param string $propertyName Method name
* @return \PHPStan\Reflection\PropertyReflection
*/
public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection
{
return $this->getTableReflection()->getNativeProperty($propertyName);
}
}