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

src/Bind.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use ReflectionClass;
0 ignored issues
show
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;
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;
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