Completed
Push — 2.x ( 4c102d...e762e8 )
by Akihito
02:20
created

Bind::onionOrderMatch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 6
cts 6
cp 1
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 4
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
final class Bind implements BindInterface
8
{
9
    /**
10
     * @var array<string, array<MethodInterceptor>>
11
     */
12
    private $bindings = [];
13
14
    /**
15
     * @var MethodMatch
16
     */
17
    private $methodMatch;
18
19
    /**
20
     * @throws \Doctrine\Common\Annotations\AnnotationException
21
     */
22
    public function __construct()
23
    {
24
        $this->methodMatch = new MethodMatch($this);
25 40
    }
26
27 40
    public function __sleep()
28 40
    {
29
        return ['bindings'];
30
    }
31
32
    /**
33 31
     * {@inheritdoc}
34
     */
35 31
    public function bind(string $class, array $pointcuts) : BindInterface
36 31
    {
37
        $pointcuts = $this->getAnnotationPointcuts($pointcuts);
38 31
        $class = new \ReflectionClass($class);
39
        $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
40
        foreach ($methods as $method) {
41
            ($this->methodMatch)($class, $method, $pointcuts);
42
        }
43
44 37
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Ray\Aop\Bind) is incompatible with the return type declared by the interface Ray\Aop\BindInterface::bind of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
45
    }
46 37
47 3
    /**
48 3
     * {@inheritdoc}
49
     */
50
    public function bindInterceptors(string $method, array $interceptors) : BindInterface
51 37
    {
52
        $this->bindings[$method] = ! array_key_exists($method, $this->bindings) ? $interceptors : array_merge(
53
            $this->bindings[$method],
54
            $interceptors
55
        );
56
57 34
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Ray\Aop\Bind) is incompatible with the return type declared by the interface Ray\Aop\BindInterface::bindInterceptors of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
58
    }
59 34
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getBindings() : array
64
    {
65 18
        return $this->bindings;
66
    }
67 18
68
    /**
69 18
     * {@inheritdoc}
70
     */
71
    public function toString($salt) : string
72
    {
73
        unset($salt);
74
75
        return strtr(rtrim(base64_encode(pack('H*', sprintf('%u', crc32(serialize($this->bindings))))), '='), '+/', '-_');
76
    }
77 31
78
    /**
79 31
     * @param Pointcut[] $pointcuts
80 31
     *
81 31
     * @return Pointcut[]
82 2
     */
83
    public function getAnnotationPointcuts(array &$pointcuts) : array
84 31
    {
85
        $keyPointcuts = [];
86
        foreach ($pointcuts as $key => $pointcut) {
87 31
            if ($pointcut->methodMatcher instanceof AnnotatedMatcher) {
88
                $key = $pointcut->methodMatcher->annotation;
89
            }
90 31
            $keyPointcuts[$key] = $pointcut;
91
        }
92 31
93 31
        return $keyPointcuts;
94 31
    }
95
}
96