Issues (101)

src/di/SpyCompiler.php (4 issues)

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
     * NOTE: psalm-suppress is acceptable here for dummy/logging infrastructure
37
     *
38
     * @psalm-suppress MoreSpecificReturnType
39
     * @psalm-suppress LessSpecificReturnStatement
40
     */
41
    public function compile(string $class, BindInterface $bind): string
42
    {
43
        if ($this->hasNoBinding($class, $bind)) {
44
            return $class;
45
        }
46
47
        return $class . $this->getInterceptors($bind); // @phpstan-ignore-line
48
    }
49
50
    /**
51
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
52
     */
53
    private function hasNoBinding(string $class, BindInterface $bind): bool
54
    {
55
        $hasMethod = $this->hasBoundMethod($class, $bind);
56
57
        return ! $bind->getBindings() && ! $hasMethod;
58
    }
59
60
    /**
61
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
62
     */
63
    private function hasBoundMethod(string $class, BindInterface $bind): bool
64
    {
65
        $bindingMethods = array_keys($bind->getBindings());
0 ignored issues
show
$bind->getBindings() of type Ray\Aop\MethodBindings is incompatible with the type array expected by parameter $array of array_keys(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        $bindingMethods = array_keys(/** @scrutinizer ignore-type */ $bind->getBindings());
Loading history...
66
        $hasMethod = false;
67
        foreach ($bindingMethods as $bindingMethod) {
68
            if (method_exists($class, $bindingMethod)) {
69
                $hasMethod = true;
70
            }
71
        }
72
73
        return $hasMethod;
74
    }
75
76
    private function getInterceptors(BindInterface $bind): string
77
    {
78
        $bindings = $bind->getBindings();
79
        if (! $bindings) {
0 ignored issues
show
$bindings is of type Ray\Aop\MethodBindings, thus it always evaluated to true.
Loading history...
80
            return ''; // @codeCoverageIgnore
81
        }
82
83
        $log = ' (aop)';
84
        foreach ($bindings as $method => $interceptors) {
85
            /**
86
             * @phpstan-var array<string> $interceptors
87
             * @psalm-ignore-var
88
             */
89
            $log .= sprintf(
90
                ' +%s(%s)',
91
                $method,
92
                implode(', ', $interceptors)
93
            );
94
        }
95
96
        return $log;
97
    }
98
}
99