Completed
Pull Request — master (#48)
by Marco
02:32
created

StaticClassMapSourceLocator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A createLocatedSource() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\SourceLocator;
6
7
use Assert\Assert;
8
use Roave\BetterReflection\Identifier\Identifier;
9
use Roave\BetterReflection\SourceLocator\Ast\Locator;
10
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
11
use Roave\BetterReflection\SourceLocator\Type\AbstractSourceLocator;
12
13
final class StaticClassMapSourceLocator extends AbstractSourceLocator
14
{
15
    /** @var string[] */
16
    private $classMap;
17
18
    public function __construct(
19
        array $classMap,
20
        Locator $astLocator
21
    ) {
22
        parent::__construct($astLocator);
23
24
        $realPaths = array_map('realpath', $classMap);
25
26
        Assert::that($classMap)->all()->file();
27
        Assert::that(array_keys($classMap))->all()->string()->notEmpty();
28
29
        $this->classMap = $realPaths;
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    protected function createLocatedSource(Identifier $identifier) : ?LocatedSource
36
    {
37
        if (! $identifier->isClass()) {
38
            return null;
39
        }
40
41
        $classFile = $this->classMap[$identifier->getName()] ?? null;
42
43
        if (null === $classFile) {
44
            return null;
45
        }
46
47
        return new LocatedSource(file_get_contents($classFile), $classFile);
48
    }
49
}
50