Completed
Push — log ( 845274...7a269a )
by Akihito
02:41
created

SpyCompiler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 56
ccs 0
cts 25
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A newInstance() 0 3 1
A compile() 0 9 2
A hasNoBinding() 0 6 2
A hasBoundMethod() 0 12 3
A getInterceptors() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the Ray.Di package.
6
 *
7
 * @license http://opensource.org/licenses/MIT MIT
8
 */
9
namespace Ray\Di;
10
11
use Ray\Aop\BindInterface;
12
use Ray\Aop\CompilerInterface;
13
14
final class SpyCompiler implements CompilerInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function newInstance($class, array $args, BindInterface $bind)
20
    {
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function compile($class, BindInterface $bind) : string
27
    {
28
        if ($this->hasNoBinding($class, $bind)) {
29
            return $class;
30
        }
31
        $newClass = $class . $this->getInterceptors($bind);
32
33
        return $newClass;
34
    }
35
36
    private function hasNoBinding($class, BindInterface $bind) : bool
37
    {
38
        $hasMethod = $this->hasBoundMethod($class, $bind);
39
40
        return ! $bind->getBindings() && ! $hasMethod;
41
    }
42
43
    private function hasBoundMethod(string $class, BindInterface $bind) : bool
44
    {
45
        $bindingMethods = array_keys($bind->getBindings());
46
        $hasMethod = false;
47
        foreach ($bindingMethods as $bindingMethod) {
48
            if (method_exists($class, $bindingMethod)) {
49
                $hasMethod = true;
50
            }
51
        }
52
53
        return $hasMethod;
54
    }
55
56
    private function getInterceptors(BindInterface $bind) : string
57
    {
58
        $log = '';
59
        foreach ($bind->getBindings() as $mehtod => $intepceptors) {
60
            $log .= sprintf(
61
                ' ::%s(%s)',
62
                $mehtod,
63
                implode(', ', $intepceptors)
64
            );
65
        }
66
67
        return $log;
68
    }
69
}
70