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

SpyCompiler::newInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 3
crap 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