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

src/MethodMatch.php (5 issues)

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 Doctrine\Common\Annotations\AnnotationReader;
8
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...
9
use ReflectionMethod;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Ray\Aop\ReflectionMethod.

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...
10
11
final class MethodMatch
12
{
13
    /**
14
     * @var AnnotationReader
15
     */
16
    private $reader;
17
18
    /**
19
     * @var BindInterface
20
     */
21
    private $bind;
22
23
    public function __construct(BindInterface $bind)
24
    {
25
        $this->bind = $bind;
26
        $this->reader = new AnnotationReader;
27
    }
28
29
    /**
30
     * @param ReflectionClass<object> $class
0 ignored issues
show
The doc-type ReflectionClass<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
31
     * @param Pointcut[]              $pointcuts
32
     */
33
    public function __invoke(ReflectionClass $class, ReflectionMethod $method, array $pointcuts) : void
34
    {
35
        /** @var array<int, object> $annotations */
36
        $annotations = $this->reader->getMethodAnnotations($method);
37
        // priority bind
38
        foreach ($pointcuts as $key => $pointcut) {
39
            if ($pointcut instanceof PriorityPointcut) {
40
                $this->annotatedMethodMatchBind($class, $method, $pointcut);
41
                unset($pointcuts[$key]);
42
            }
43
        }
44
        $onion = $this->onionOrderMatch($class, $method, $pointcuts, $annotations);
45
46
        // default binding
47
        foreach ($onion as $pointcut) {
48
            $this->annotatedMethodMatchBind($class, $method, $pointcut);
49
        }
50
    }
51
52
    /**
53
     * @param ReflectionClass<object> $class
0 ignored issues
show
The doc-type ReflectionClass<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
54
     */
55
    private function annotatedMethodMatchBind(ReflectionClass $class, ReflectionMethod $method, Pointcut $pointCut) : void
56
    {
57
        $isMethodMatch = $pointCut->methodMatcher->matchesMethod($method, $pointCut->methodMatcher->getArguments());
58
        if (! $isMethodMatch) {
59
            return;
60
        }
61
        $isClassMatch = $pointCut->classMatcher->matchesClass($class, $pointCut->classMatcher->getArguments());
62
        if (! $isClassMatch) {
63
            return;
64
        }
65
        /** @var MethodInterceptor[] $interceptors */
66
        $interceptors = $pointCut->interceptors;
67
        $this->bind->bindInterceptors($method->name, $interceptors);
68
    }
69
70
    /**
71
     * @param ReflectionClass<object> $class
0 ignored issues
show
The doc-type ReflectionClass<object> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
72
     * @param Pointcut[]              $pointcuts
73
     * @param array<int, object>      $annotations
74
     *
75
     * @return Pointcut[]
76
     */
77
    private function onionOrderMatch(
78
        ReflectionClass $class,
79
        ReflectionMethod $method,
80
        array $pointcuts,
81
        array $annotations
82
    ) : array {
83
        // method bind in annotation order
84
        foreach ($annotations as $annotation) {
85
            $annotationIndex = get_class($annotation);
86
            if (array_key_exists($annotationIndex, $pointcuts)) {
87
                $this->annotatedMethodMatchBind($class, $method, $pointcuts[$annotationIndex]);
88
                unset($pointcuts[$annotationIndex]);
89
            }
90
        }
91
92
        return $pointcuts;
93
    }
94
}
95