1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Ray\Di; |
||
6 | |||
7 | use Ray\Aop\BindInterface; |
||
8 | use Ray\Aop\CompilerInterface; |
||
9 | |||
10 | use function array_keys; |
||
11 | use function implode; |
||
12 | use function method_exists; |
||
13 | use function sprintf; |
||
14 | |||
15 | /** |
||
16 | * @codeCoverageIgnore |
||
17 | */ |
||
18 | final class SpyCompiler implements CompilerInterface |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritDoc} |
||
22 | * |
||
23 | * @psalm-suppress InvalidReturnType |
||
24 | * @template T of object |
||
25 | */ |
||
26 | public function newInstance(string $class, array $args, BindInterface $bind) |
||
27 | { |
||
28 | // never called // @phpstan-ignore-line |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Return "logging" class name |
||
33 | * |
||
34 | * Dummy classes are used for logging and don't really exist. |
||
35 | * So the code breaks the QA rules as shown below. |
||
36 | * |
||
37 | * @psalm-suppress MoreSpecificReturnType |
||
38 | * @psalm-suppress LessSpecificReturnStatement |
||
39 | */ |
||
40 | public function compile(string $class, BindInterface $bind): string |
||
41 | { |
||
42 | if ($this->hasNoBinding($class, $bind)) { |
||
43 | return $class; |
||
44 | } |
||
45 | |||
46 | return $class . $this->getInterceptors($bind); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param class-string $class |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
51 | */ |
||
52 | private function hasNoBinding(string $class, BindInterface $bind): bool |
||
53 | { |
||
54 | $hasMethod = $this->hasBoundMethod($class, $bind); |
||
55 | |||
56 | return ! $bind->getBindings() && ! $hasMethod; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param class-string $class |
||
0 ignored issues
–
show
|
|||
61 | */ |
||
62 | private function hasBoundMethod(string $class, BindInterface $bind): bool |
||
63 | { |
||
64 | $bindingMethods = array_keys($bind->getBindings()); |
||
65 | $hasMethod = false; |
||
66 | foreach ($bindingMethods as $bindingMethod) { |
||
67 | if (method_exists($class, $bindingMethod)) { |
||
68 | $hasMethod = true; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $hasMethod; |
||
73 | } |
||
74 | |||
75 | private function getInterceptors(BindInterface $bind): string |
||
76 | { |
||
77 | $bindings = $bind->getBindings(); |
||
78 | if (! $bindings) { |
||
0 ignored issues
–
show
The expression
$bindings of type array<string,array<mixed...hodInterceptor|string>> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
79 | return ''; // @codeCoverageIgnore |
||
80 | } |
||
81 | |||
82 | $log = ' (aop)'; |
||
83 | foreach ($bindings as $method => $interceptors) { |
||
84 | /** |
||
85 | * @phpstan-var array<string> $interceptors |
||
86 | * @psalm-ignore-var |
||
87 | */ |
||
88 | $log .= sprintf( |
||
89 | ' +%s(%s)', |
||
90 | $method, |
||
91 | implode(', ', $interceptors) |
||
92 | ); |
||
93 | } |
||
94 | |||
95 | return $log; |
||
96 | } |
||
97 | } |
||
98 |