Passed
Pull Request — master (#315)
by Théo
06:13 queued 01:44
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
     * @throws InvalidDocblock
34
     */
35
    public function parse(string $docblock): TreeNode
36
    {
37
        $docblock = trim($docblock);
38
39
        if (0 !== strpos($docblock, '/**') || '*/' !== substr($docblock, -2)) {
40
            return new TreeNode('#null');
41
        }
42
43
        $compiler = Llk::load(new Read(__DIR__.'/../../res/annotation-grammar.pp'));
44
45
        try {
46
            return $compiler->parse($docblock);
47
        } catch (UnrecognizedToken $exception) {
48
            throw InvalidDocblock::createFromHoaUnrecognizedToken($docblock, $exception);
49
        }
50
    }
51
}
52