Failed Conditions
Push — type ( 257e93...5997a7 )
by Michael
02:15
created

NativeTypeParser::parsePropertyType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Annotations\TypeParser;
6
7
use Doctrine\Annotations\Type\Type;
8
use Hoa\Compiler\Llk\Llk;
9
use Hoa\Compiler\Llk\Parser;
10
use Hoa\Compiler\Llk\TreeNode;
11
use Hoa\File\Read;
12
use function preg_match;
13
14
/**
15
 * @internal
16
 */
17
final class NativeTypeParser implements TypeParser
18
{
19
    private const ROOT_RULE = 'type';
20
21
    /** @var Parser */
22
    private $compiler;
23
24
    /** @var TypeVisitor */
25
    private $visitor;
26
27
    public function __construct()
28
    {
29
        $this->compiler = Llk::load(new Read(__DIR__ . '/type.pp'));
30
        $this->visitor  = new TypeVisitor();
31
    }
32
33
    /**
34
     * @param array<string, string> $imports
35
     */
36
    public function parsePropertyType(string $docBlock, array $imports) : Type
37
    {
38
        preg_match('~@var\s+(.+)$~', $docBlock, $match);
39
40
        /** @var TreeNode $trace */
41
        $tree = $this->compiler->parse($match[1], self::ROOT_RULE);
42
43
        return $this->visitor->visit($tree);
44
    }
45
}
46