1 | <?php |
||
14 | trait AsyncTable |
||
15 | { |
||
16 | /** |
||
17 | * @var Pool |
||
18 | */ |
||
19 | protected $pool; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $tableName; |
||
25 | |||
26 | /** |
||
27 | * @var AnnotationReader |
||
28 | */ |
||
29 | protected $annotationReader; |
||
30 | |||
31 | /** |
||
32 | * @var \ReflectionClass |
||
33 | */ |
||
34 | protected $reflectionClass; |
||
35 | |||
36 | /** |
||
37 | * @param Pool $pool |
||
38 | * @param string $tableName |
||
39 | */ |
||
40 | public function setUpAsyncTable(Pool $pool, $tableName, $tableClass) |
||
41 | { |
||
42 | $this->pool = $pool; |
||
43 | $this->tableName = $tableName; |
||
44 | $this->annotationReader = new AnnotationReader(); |
||
45 | $this->reflectionClass = new \ReflectionClass($tableClass); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param $function |
||
50 | * @param array $arguments |
||
51 | * @return PromiseInterface |
||
52 | */ |
||
53 | public function __call($function, array $arguments = []) |
||
54 | { |
||
55 | if ( |
||
56 | $this->returnsQuery($function) || |
||
57 | $this->hasMethodAnnotation($function, Async::class) || |
||
58 | ( |
||
59 | $this->hasClassAnnotation(Async::class) && |
||
60 | $this->hasNoMethodAnnotation($function) |
||
61 | ) || |
||
62 | strpos(strtolower($function), 'save') === 0 || |
||
63 | strpos(strtolower($function), 'find') === 0 || |
||
64 | strpos(strtolower($function), 'fetch') === 0 || |
||
65 | strpos(strtolower($function), 'retrieve') === 0 |
||
66 | ) { |
||
67 | return $this->callAsync($function, $arguments); |
||
68 | } |
||
69 | |||
70 | return $this->callSync($function, $arguments); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param $function |
||
75 | * @param array $arguments |
||
76 | * @return PromiseInterface |
||
77 | */ |
||
78 | protected function callSync($function, array $arguments = []) |
||
94 | |||
95 | /** |
||
96 | * @param $function |
||
97 | * @param array $arguments |
||
98 | * @return PromiseInterface |
||
99 | */ |
||
100 | protected function callAsync($function, array $arguments = []) |
||
114 | |||
115 | /** |
||
116 | * @param $class |
||
117 | * @return bool |
||
118 | */ |
||
119 | protected function hasClassAnnotation($class) |
||
123 | |||
124 | /** |
||
125 | * @param $method |
||
126 | * @param $class |
||
127 | * @return bool |
||
128 | */ |
||
129 | protected function hasMethodAnnotation($method, $class) |
||
134 | |||
135 | /** |
||
136 | * @param $method |
||
137 | * @return bool |
||
138 | */ |
||
139 | protected function hasNoMethodAnnotation($method) |
||
147 | |||
148 | /** |
||
149 | * @param $function |
||
150 | * @return bool |
||
151 | */ |
||
152 | protected function returnsQuery($function) |
||
168 | |||
169 | /** |
||
170 | * @param $docBlockContents |
||
171 | * @return DocBlock |
||
172 | */ |
||
173 | protected function getDocBlock($docBlockContents) |
||
181 | } |
||
182 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: