Completed
Push — 2.x ( 711074...57aa77 )
by Akihito
17s queued 12s
created

Bind::bind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use ReflectionClass;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ray\Aop\ReflectionClass.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
final class Bind implements BindInterface
10
{
11
    /**
12
     * @var array<string, array<MethodInterceptor>>
13
     */
14
    private $bindings = [];
15
16
    /**
17
     * @var MethodMatch
18
     */
19
    private $methodMatch;
20
21
    /**
22
     * @throws \Doctrine\Common\Annotations\AnnotationException
23
     */
24
    public function __construct()
25 40
    {
26
        $this->methodMatch = new MethodMatch($this);
27 40
    }
28 40
29
    public function __sleep()
30
    {
31
        return ['bindings'];
32
    }
33 31
34
    /**
35 31
     * {@inheritdoc}
36 31
     */
37
    public function bind(string $class, array $pointcuts) : BindInterface
38 31
    {
39
        $pointcuts = $this->getAnnotationPointcuts($pointcuts);
40
        $class = new ReflectionClass($class);
41
        $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
42
        foreach ($methods as $method) {
43
            ($this->methodMatch)($class, $method, $pointcuts);
44 37
        }
45
46 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...
47 3
    }
48 3
49
    /**
50
     * {@inheritdoc}
51 37
     */
52
    public function bindInterceptors(string $method, array $interceptors) : BindInterface
53
    {
54
        $this->bindings[$method] = ! array_key_exists($method, $this->bindings) ? $interceptors : array_merge(
55
            $this->bindings[$method],
56
            $interceptors
57 34
        );
58
59 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...
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 18
    public function getBindings() : array
66
    {
67 18
        return $this->bindings;
68
    }
69 18
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function toString($salt) : string
74
    {
75
        unset($salt);
76
77 31
        return serialize($this->bindings);
78
    }
79 31
80 31
    /**
81 31
     * @param Pointcut[] $pointcuts
82 2
     *
83
     * @return Pointcut[]
84 31
     */
85
    public function getAnnotationPointcuts(array &$pointcuts) : array
86
    {
87 31
        $keyPointcuts = [];
88
        foreach ($pointcuts as $key => $pointcut) {
89
            if ($pointcut->methodMatcher instanceof AnnotatedMatcher) {
90 31
                $key = $pointcut->methodMatcher->annotation;
91
            }
92 31
            $keyPointcuts[$key] = $pointcut;
93 31
        }
94 31
95
        return $keyPointcuts;
96 31
    }
97
}
98