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

StaticClassMapSourceLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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