-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAssociationTableMixinClassReflectionExtension.php
More file actions
123 lines (107 loc) · 4.08 KB
/
AssociationTableMixinClassReflectionExtension.php
File metadata and controls
123 lines (107 loc) · 4.08 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
<?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
{
// Handle Table classes
if ($classReflection->is(Table::class)) {
if ($classReflection->hasNativeMethod($methodName)) {
return false; // Let the native method be used
}
// magic findBy* and findAllBy* methods - available on ALL table classes
if (preg_match('/^find(All)?By/', $methodName) === 1) {
return true;
}
}
if (!$classReflection->is(Association::class)) {
return false;
}
// For associations, provide magic find(All)?By methods
if (preg_match('/^find(All)?By/', $methodName) === 1) {
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
{
// Handle Table classes
if ($classReflection->is(Table::class)) {
if ($classReflection->hasNativeMethod($methodName)) {
return $classReflection->getNativeMethod($methodName);
}
// magic findBy* and findAllBy* methods
if (preg_match('/^find(All)?By/', $methodName) === 1) {
return new TableFindByPropertyMethodReflection($methodName, $classReflection);
}
}
// For associations, handle magic find(All)?By methods
if (preg_match('/^find(All)?By/', $methodName) === 1) {
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);
}
}