Test Failed
Push — master ( 08b170...e6bc2a )
by Kirill
02:02
created

Utils::findName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Builder;
11
12
use Railt\Io\Readable;
13
use Railt\Parser\Ast\RuleInterface;
14
use Railt\Reflection\AbstractDefinition;
15
use Railt\Reflection\Contracts\Definition as DefinitionInterface;
16
use Railt\Reflection\Contracts\Document;
17
use Railt\SDL\Exception\SyntaxException;
18
19
/**
20
 * Class Utils
21
 */
22
class Utils
23
{
24
    /**
25
     * @param RuleInterface $rule
26
     * @return string|null
27
     */
28
    public static function findName(RuleInterface $rule): ?string
29
    {
30
        foreach ($rule->getChildren() as $child) {
31
            if ($child->getName() === 'Name') {
32
                return $child->getValue();
33
            }
34
        }
35
36
        return null;
37
    }
38
39
    /**
40
     * @param Readable $file
41
     * @param RuleInterface $rule
42
     * @return string
43
     * @throws SyntaxException
44
     */
45
    public static function getName(Readable $file, RuleInterface $rule): string
46
    {
47
        if (($name = static::findName($rule)) !== null) {
48
            return $name;
49
        }
50
51
        $exception = new SyntaxException('Unable to determine type name');
52
        $exception->throwsIn($file, $rule->getOffset());
53
54
        throw $exception;
55
    }
56
57
    /**
58
     * @param RuleInterface $rule
59
     * @return string|null
60
     */
61
    public static function findDescription(RuleInterface $rule): ?string
62
    {
63
        foreach ($rule->getChildren() as $child) {
64
            if ($child->getName() === 'Description') {
65
                return $child->getChild(0)->getValue(1);
66
            }
67
        }
68
69
        return null;
70
    }
71
}
72