Bind::bind()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 15
ccs 6
cts 6
cp 1
crap 3
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use Doctrine\Common\Annotations\AnnotationException;
8
9
use function array_key_exists;
10
use function array_merge;
11
use function serialize;
12
13
final class Bind implements BindInterface
14
{
15
    /** @var array<string, array<MethodInterceptor>> */
16
    private $bindings = [];
17
18
    /** @var MethodMatch */
19
    private $methodMatch;
20
21
    /** @throws AnnotationException */
22
    public function __construct()
23
    {
24
        $this->methodMatch = new MethodMatch($this);
25 40
    }
26
27 40
    /** @return list<string> */
0 ignored issues
show
Bug introduced by
The type Ray\Aop\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28 40
    public function __sleep(): array
29
    {
30
        return ['bindings'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('bindings') returns the type array<integer,string> which is incompatible with the documented return type Ray\Aop\list.
Loading history...
31
    }
32
33 31
    /**
34
     * {@inheritDoc}
35 31
     */
36 31
    public function bind(string $class, array $pointcuts): BindInterface
37
    {
38 31
        $pointcuts = $this->getAnnotationPointcuts($pointcuts);
39
        $reflectionClass = new ReflectionClass($class);
40
        $methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
41
        foreach ($methods as $method) {
42
            if ($method->getName() === '__construct') {
43
                continue;
44 37
            }
45
46 37
            $rayMethod = new ReflectionMethod($reflectionClass->getName(), $method->getName());
47 3
            ($this->methodMatch)($reflectionClass, $rayMethod, $pointcuts);
48 3
        }
49
50
        return $this;
51 37
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function bindInterceptors(string $method, array $interceptors): BindInterface
57 34
    {
58
        $this->bindings[$method] = ! array_key_exists($method, $this->bindings) ? $interceptors : array_merge(
59 34
            $this->bindings[$method],
60
            $interceptors
61
        );
62
63
        return $this;
64
    }
65 18
66
    /**
67 18
     * {@inheritDoc}
68
     */
69 18
    public function getBindings(): array
70
    {
71
        return $this->bindings;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 31
    public function __toString(): string
78
    {
79 31
        return serialize($this->bindings);
80 31
    }
81 31
82 2
    /**
83
     * @param Pointcut[] $pointcuts
84 31
     *
85
     * @return Pointcut[]
86
     */
87 31
    public function getAnnotationPointcuts(array &$pointcuts): array
88
    {
89
        $keyPointcuts = [];
90 31
        foreach ($pointcuts as $key => $pointcut) {
91
            if ($pointcut->methodMatcher instanceof AnnotatedMatcher) {
92 31
                $key = $pointcut->methodMatcher->annotation;
93 31
            }
94 31
95
            $keyPointcuts[$key] = $pointcut;
96 31
        }
97
98 31
        return $keyPointcuts;
99
    }
100
}
101