Completed
Push — php7.3 ( 50c0fb )
by Akihito
03:19
created

SpyCompiler::newInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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
final class SpyCompiler implements CompilerInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function newInstance($class, array $args, BindInterface $bind)
16
    {
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 1
    public function compile($class, BindInterface $bind) : string
23
    {
24 1
        if ($this->hasNoBinding($class, $bind)) {
25
            return $class;
26
        }
27
28 1
        return $class . $this->getInterceptors($bind);
29
    }
30
31 1
    private function hasNoBinding($class, BindInterface $bind) : bool
32
    {
33 1
        $hasMethod = $this->hasBoundMethod($class, $bind);
34
35 1
        return ! $bind->getBindings() && ! $hasMethod;
36
    }
37
38 1
    private function hasBoundMethod(string $class, BindInterface $bind) : bool
39
    {
40 1
        $bindingMethods = array_keys($bind->getBindings());
41 1
        $hasMethod = false;
42 1
        foreach ($bindingMethods as $bindingMethod) {
43 1
            if (method_exists($class, $bindingMethod)) {
44 1
                $hasMethod = true;
45
            }
46
        }
47
48 1
        return $hasMethod;
49
    }
50
51 1
    private function getInterceptors(BindInterface $bind) : string
52
    {
53 1
        $bindings = $bind->getBindings();
54 1
        if (! $bindings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $bindings of type array 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 empty(..) or ! empty(...) instead.

Loading history...
55
            return '';
56
        }
57 1
        $log = ' (aop)';
58 1
        foreach ($bindings as $mehtod => $intepceptors) {
59 1
            $log .= sprintf(
60 1
                ' +%s(%s)',
61 1
                $mehtod,
62 1
                implode(', ', $intepceptors)
63
            );
64
        }
65
66 1
        return $log;
67
    }
68
}
69