1 | <?php |
||||||
2 | |||||||
3 | namespace Larapie\Actions\Concerns; |
||||||
4 | |||||||
5 | trait SuccessHook |
||||||
6 | { |
||||||
7 | /* Extensive resolving to ensure you always get the proper object as parameter */ |
||||||
8 | 41 | protected function successHook($result) |
|||||
9 | { |
||||||
10 | 41 | if (method_exists($this, 'onSuccess')) { |
|||||
11 | try { |
||||||
12 | 5 | $parameters = (new \ReflectionMethod($this, 'onSuccess'))->getParameters(); |
|||||
13 | 5 | $extraParameter = $this->resolveParameter($parameters, $result); |
|||||
14 | } catch (\ReflectionException $e) { |
||||||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
![]() |
|||||||
15 | } |
||||||
16 | 5 | $this->resolveAndCall($this, 'onSuccess', $extraParameter ?? []); |
|||||
0 ignored issues
–
show
It seems like
resolveAndCall() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
17 | } |
||||||
18 | 41 | } |
|||||
19 | |||||||
20 | 5 | private function resolveParameter($parameters, $result) |
|||||
21 | { |
||||||
22 | 5 | $extraParameter = null; |
|||||
23 | |||||||
24 | //BIND the value to the first parameter with the correct type |
||||||
25 | 5 | collect($parameters) |
|||||
26 | 5 | ->filter(function (\ReflectionParameter $parameter) { |
|||||
27 | 4 | return $parameter->hasType(); |
|||||
28 | 5 | }) |
|||||
29 | 5 | ->filter(function (\ReflectionParameter $parameter) use ($result) { |
|||||
30 | 2 | return (gettype($result) === 'object' ? get_class($result) : gettype($result)) === $parameter->getType()->getName(); |
|||||
31 | 5 | }) |
|||||
32 | 5 | ->each(function (\ReflectionParameter $parameter) use ($result, &$extraParameter) { |
|||||
33 | 2 | $extraParameter = [$parameter->getName() => $result]; |
|||||
34 | |||||||
35 | 2 | return false; |
|||||
36 | 5 | }); |
|||||
37 | |||||||
38 | 5 | if (isset($extraParameter)) { |
|||||
39 | 2 | return $extraParameter; |
|||||
40 | } |
||||||
41 | |||||||
42 | //BIND the value to the parameter with the same name |
||||||
43 | //IF there's not a parameter with the same name as the value type |
||||||
44 | //CHOOSE the first value that doesn't have a type |
||||||
45 | 3 | collect($parameters) |
|||||
46 | 3 | ->filter(function (\ReflectionParameter $parameter) { |
|||||
47 | 2 | return ! $parameter->hasType(); |
|||||
48 | 3 | }) |
|||||
49 | 3 | ->each(function (\ReflectionParameter $parameter) use ($result, &$extraParameter) { |
|||||
50 | 2 | if ($extraParameter === null) { |
|||||
51 | 2 | $extraParameter = [$parameter->getName() => $result]; |
|||||
52 | 1 | } elseif (strcasecmp(substr(strrchr(get_class($result), '\\'), 1), $parameter->getName()) == 0) { |
|||||
53 | 1 | $extraParameter = [$parameter->getName() => $result]; |
|||||
54 | |||||||
55 | 1 | return false; |
|||||
56 | } |
||||||
57 | 3 | }); |
|||||
58 | |||||||
59 | 3 | return $extraParameter; |
|||||
60 | } |
||||||
61 | } |
||||||
62 |