Completed
Push — 2.x ( 144798...0daeb2 )
by Akihito
10s
created

SpyCompiler::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2.032
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 1
    public function compile($class, BindInterface $bind) : string
27
    {
28 1
        if ($this->hasNoBinding($class, $bind)) {
29
            return $class;
30
        }
31 1
        $newClass = $class . $this->getInterceptors($bind);
32
33 1
        return $newClass;
34
    }
35
36 1
    private function hasNoBinding($class, BindInterface $bind) : bool
37
    {
38 1
        $hasMethod = $this->hasBoundMethod($class, $bind);
39
40 1
        return ! $bind->getBindings() && ! $hasMethod;
41
    }
42
43 1
    private function hasBoundMethod(string $class, BindInterface $bind) : bool
44
    {
45 1
        $bindingMethods = array_keys($bind->getBindings());
46 1
        $hasMethod = false;
47 1
        foreach ($bindingMethods as $bindingMethod) {
48 1
            if (method_exists($class, $bindingMethod)) {
49 1
                $hasMethod = true;
50
            }
51
        }
52
53 1
        return $hasMethod;
54
    }
55
56 1
    private function getInterceptors(BindInterface $bind) : string
57
    {
58 1
        $bindings = $bind->getBindings();
59 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...
60
            return '';
61
        }
62 1
        $log = ' (aop)';
63 1
        foreach ($bindings as $mehtod => $intepceptors) {
64 1
            $log .= sprintf(
65 1
                ' +%s(%s)',
66 1
                $mehtod,
67 1
                implode(', ', $intepceptors)
68
            );
69
        }
70
71 1
        return $log;
72
    }
73
}
74