RawIdentifierContext   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
eloc 12
c 1
b 0
f 1
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFqnFromContext() 0 12 4
1
<?php
2
3
/**
4
 * This file is part of the sj-i/phpdoc-type-reader package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpDocTypeReader\Context;
15
16
final class RawIdentifierContext implements IdentifierContext
17
{
18
    private string $namespace;
19
    /** @var array<string, string> */
20
    private array $aliases;
21
22
    /**
23
     * IdentifierContext constructor.
24
     * @param string $namespace
25
     * @param array<string, string> $aliases
26
     */
27
    public function __construct(string $namespace, array $aliases)
28
    {
29
        $this->namespace = $namespace;
30
        $this->aliases = $aliases;
31
    }
32
33
    public function getFqnFromContext(string $identifier): string
34
    {
35
        if (($identifier[0] ?? '') === '\\') {
36
            if ($identifier === '\\') {
37
                throw new \LogicException('invalid identifier');
38
            }
39
            return substr($identifier, 1);
40
        }
41
        if (isset($this->aliases[$identifier])) {
42
            return $this->aliases[$identifier];
43
        }
44
        return "{$this->namespace}\\{$identifier}";
45
    }
46
}
47