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

BuiltinMatcher::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Aop;
6
7
use Ray\Aop\Exception\InvalidMatcherException;
8
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...
9
use ReflectionMethod;
0 ignored issues
show
Bug introduced by
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
class BuiltinMatcher extends AbstractMatcher
12
{
13
    /**
14
     * @var string
15
     */
16
    private $matcherName;
17
18
    /**
19
     * @var AbstractMatcher
20
     */
21
    private $matcher;
22
23
    /**
24
     * @param mixed[] $arguments
25
     */
26
    public function __construct(string $matcherName, array $arguments)
27
    {
28 36
        parent::__construct();
29
        $this->matcherName = $matcherName;
30 36
        $this->arguments = $arguments;
31 36
        $matcherClass = 'Ray\Aop\Matcher\\' . ucwords($this->matcherName) . 'Matcher';
32 36
        assert(class_exists($matcherClass));
33 36
        $matcher = (new ReflectionClass($matcherClass))->newInstance();
34 36
        if (! $matcher instanceof AbstractMatcher) {
35 36
            throw new InvalidMatcherException($matcherClass);
36 1
        }
37
        $this->matcher = $matcher;
38 36
    }
39 36
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function matchesClass(ReflectionClass $class, array $arguments) : bool
44 30
    {
45
        return $this->matcher->matchesClass($class, $arguments);
46 30
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function matchesMethod(ReflectionMethod $method, array $arguments) : bool
52 32
    {
53
        return $this->matcher->matchesMethod($method, $arguments);
54 32
    }
55
}
56