Passed
Pull Request — master (#315)
by Théo
02:54
created

DocblockParser::parse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Annotation;
16
17
use Hoa\Compiler\Exception\UnrecognizedToken;
18
use Hoa\Compiler\Llk\Llk;
19
use Hoa\Compiler\Llk\TreeNode;
20
use Hoa\File\Read;
21
use function strpos;
22
use function substr;
23
use function trim;
24
25
/**
26
 * @private
27
 */
28
final class DocblockParser
29
{
30
    /**
31
     * Parses the docblock and returns its AST.
32
     */
33
    public function parse(string $docblock): TreeNode
34
    {
35
        $docblock = trim($docblock);
36
37
        if (0 !== strpos($docblock, '/**') || '*/' !== substr($docblock, -2)) {
38
            return new TreeNode('#null');
39
        }
40
41
        $compiler = Llk::load(new Read(__DIR__.'/../../res/annotation-grammar.pp'));
42
43
        try {
44
            return $compiler->parse($docblock);
45
        } catch (UnrecognizedToken $exception) {
46
            throw InvalidDocblock::createFromHoaUnrecognizedToken($docblock, $exception);
47
        }
48
    }
49
}
50