Passed
Pull Request — 1.x (#2)
by Akihito
11:09 queued 09:15
created

SpyModule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 19
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 25 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\TestDouble;
6
7
use Ray\Di\AbstractModule;
8
use Ray\Di\Scope;
9
use Ray\TestDouble\Exception\InvalidSpyTargetException;
10
11
use function assert;
12
use function class_exists;
13
use function interface_exists;
14
use function is_string;
15
16
class SpyModule extends AbstractModule
17
{
18
    /** @var array<class-string> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string>.
Loading history...
19
    private $spyTargets;
20
21
    /** @param array<class-string|interface-string> $spyTargets */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string|interface-string> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string|interface-string>.
Loading history...
22
    public function __construct(array $spyTargets = [], AbstractModule|null $module = null)
23
    {
24
        $this->spyTargets = $spyTargets;
25
        parent::__construct($module);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function configure()
32
    {
33
        $this->bind(Logger::class)->in(Scope::SINGLETON);
34
        foreach ($this->spyTargets as $class) {
35
            if (interface_exists($class)) {
36
                $this->bindInterceptor(
37
                    new IsInterfaceMatcher($class),
38
                    $this->matcher->any(),
39
                    [SpyInterceptor::class],
40
                );
41
                continue;
42
            }
43
44
            if (class_exists($class)) {
45
                $this->bindInterceptor(
46
                    $this->matcher->subclassesOf($class),
47
                    $this->matcher->any(),
48
                    [SpyInterceptor::class],
49
                );
50
                continue;
51
            }
52
53
            assert(is_string($class));
54
55
            throw new InvalidSpyTargetException($class);
56
        }
57
    }
58
}
59