Completed
Push — master ( ee9170...1dcd4f )
by Kirill
03:45
created

TypeDefinitionDelegate::parseString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Compiler\Definition;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\Parser\Environment;
14
use Railt\Reflection\AbstractTypeDefinition;
15
use Railt\Reflection\Contracts\Definition\TypeDefinition;
16
use Railt\Reflection\Contracts\Dictionary;
17
use Railt\Reflection\Document;
18
use Railt\SDL\Compiler\Pipeline;
19
use Railt\SDL\Compiler\Value\StringValue;
20
use Railt\SDL\Exception\TypeConflictException;
21
22
/**
23
 * Class TypeDefinitionDelegate
24
 */
25
abstract class TypeDefinitionDelegate extends DefinitionDelegate
26
{
27
    /**
28
     * @param Environment $env
29
     */
30
    public function boot(Environment $env): void
31
    {
32
        parent::boot($env);
33
34
        $this->transaction($this->definition, function () {
35
            $this->register();
36
        });
37
38
        $this->future(Pipeline::PRIORITY_DEFINITION, function () {
39
            $this->addDescription($this->definition, $this);
40
        });
41
    }
42
43
    /**
44
     * @throws \Railt\Io\Exception\ExternalFileException
45
     * @throws \Railt\Reflection\Exception\TypeNotFoundException
46
     */
47
    private function register(): void
48
    {
49
        /** @var Document $document */
50
        $document = $this->definition->getDocument();
51
52
        $this->verifyDuplication($this->definition, $document->getDictionary());
53
54
        $document->withDefinition($this->definition);
55
    }
56
57
58
    /**
59
     * @param TypeDefinition $type
60
     * @param Dictionary $dict
61
     * @throws \Railt\Io\Exception\ExternalFileException
62
     * @throws \Railt\Reflection\Exception\TypeNotFoundException
63
     */
64
    private function verifyDuplication(TypeDefinition $type, Dictionary $dict): void
65
    {
66
        if ($dict->has($type->getName())) {
67
            $prev  = $dict->get($type->getName(), $type);
68
            $error = 'Could not redeclare type %s by %s';
69
            $error = \sprintf($error, $prev, $type);
70
71
            throw $this->error(new TypeConflictException($error));
72
        }
73
    }
74
75
    /**
76
     * @param TypeDefinition|AbstractTypeDefinition $type
77
     * @param RuleInterface $rule
78
     */
79
    protected function addDescription(TypeDefinition $type, RuleInterface $rule): void
80
    {
81
        /** @var RuleInterface $description */
82
        $description = $rule->first('Description', 1);
83
84
        if ($description) {
85
            $type->withDescription($this->parseString($description));
86
        }
87
    }
88
89
    /**
90
     * @param RuleInterface $rule
91
     * @return string
92
     */
93
    private function parseString(RuleInterface $rule): string
94
    {
95
        $value = new StringValue($rule->getChild(0));
0 ignored issues
show
Bug introduced by
It seems like $rule->getChild(0) can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
96
97
        return $value->toScalar();
98
    }
99
}
100