Test Failed
Push — master ( a0eed4...bb00ac )
by Kirill
149:40
created

DocumentBuilder::append()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 21
rs 9.584
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\Frontend\Builder;
11
12
use Railt\Io\Readable;
13
use Railt\Parser\Ast\RuleInterface;
14
use Railt\SDL\Exception\InternalException;
15
use Railt\SDL\IR\Definition;
16
use Railt\SDL\IR\Type;
17
18
/**
19
 * Class DocumentBuilder
20
 */
21
class DocumentBuilder extends DefinitionBuilder
22
{
23
    /**
24
     * @param Readable $file
25
     * @param RuleInterface $ast
26
     * @return \Generator|mixed|Definition|\Railt\SDL\IR\TypeDefinition|\Railt\SDL\IR\TypeInvocation
27
     */
28
    public function build(Readable $file, RuleInterface $ast)
29
    {
30
        $document = new Definition();
31
        $document->in($file);
32
33
        $document->type = Type::DOCUMENT;
34
35
        $document->definitions =
36
        $document->extensions  =
37
        $document->directives  = [];
38
39
        foreach ($ast->getChildren() as $child) {
40
            if ($result = yield $child) {
41
                $this->append($document, $result);
42
            }
43
        }
44
45
        return $document;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $document; (Railt\SDL\IR\Definition) is incompatible with the return type documented by Railt\SDL\Frontend\Builder\DocumentBuilder::build of type Generator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
46
    }
47
48
    /**
49
     * @param Definition $document
50
     * @param Definition $value
51
     * @return void
52
     */
53
    private function append(Definition $document, Definition $value): void
54
    {
55
        switch (true) {
56
            case $this->isTypeDefinition($value):
57
                $document->definitions[] = $value;
58
                break;
59
60
            case $this->isTypeExtension($value):
61
                $document->extensions[] = $value;
62
                break;
63
64
            case $this->isDirective($value):
65
                $document->directives[] = $value;
66
                break;
67
68
            default:
69
                throw new InternalException(
70
                    'Unrecognized struct ' . $value->toJson(\JSON_PRETTY_PRINT)
71
                );
72
        }
73
    }
74
75
    /**
76
     * @param Definition $definition
77
     * @return bool
78
     */
79
    private function isTypeDefinition(Definition $definition): bool
80
    {
81
        return $definition->type && $definition->type->isIndependent();
82
    }
83
84
    /**
85
     * @param Definition $definition
86
     * @return bool
87
     */
88
    private function isTypeExtension(Definition $definition): bool
89
    {
90
        return $definition->type && $definition->type->isExtension();
91
    }
92
93
    /**
94
     * @param Definition $definition
95
     * @return bool
96
     */
97
    private function isDirective(Definition $definition): bool
98
    {
99
        return $definition->type === null;
100
    }
101
}
102