Issues (4)

src/SpyModule.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\TestDouble;
6
7
use Ray\Di\AbstractModule;
8
use Ray\TestDouble\Exception\InvalidSpyTargetException;
9
10
use function assert;
11
use function class_exists;
12
use function interface_exists;
13
use function is_string;
14
15
class SpyModule extends AbstractModule
16
{
17
    /** @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...
18
    private $spyTargets;
19
20
    /** @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...
21
    public function __construct(array $spyTargets = [], AbstractModule|null $module = null)
22
    {
23
        $this->spyTargets = $spyTargets;
24
        parent::__construct($module);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function configure()
31
    {
32
        $this->install(new SpyBaseModule());
33
        foreach ($this->spyTargets as $class) {
34
            if (interface_exists($class)) {
35
                $this->bindInterceptor(
36
                    new IsInterfaceMatcher($class),
37
                    $this->matcher->any(),
38
                    [SpyInterceptor::class],
39
                );
40
                continue;
41
            }
42
43
            if (class_exists($class)) {
44
                $this->bindInterceptor(
45
                    $this->matcher->subclassesOf($class),
46
                    $this->matcher->any(),
47
                    [SpyInterceptor::class],
48
                );
49
                continue;
50
            }
51
52
            assert(is_string($class));
53
54
            throw new InvalidSpyTargetException($class);
55
        }
56
    }
57
}
58