Completed
Push — master ( 957ceb...9ec3d9 )
by Kirill
02:06
created

PP2::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Compiler\Grammar;
11
12
use Railt\Compiler\Grammar\PP2\Lexer;
13
use Railt\Compiler\Grammar\PP2\Parser;
14
use Railt\Compiler\Reader\GrammarInterface;
15
use Railt\Compiler\Reader\Result;
16
use Railt\Io\Readable;
17
use Railt\Parser\Ast\LeafInterface;
18
use Railt\Parser\Ast\RuleInterface;
19
20
/**
21
 * Class Grammar
22
 */
23
class PP2 implements GrammarInterface
24
{
25
    /**
26
     * @var PP2
27
     */
28
    private $parser;
29
30
    /**
31
     * @var array
32
     */
33
    private $ast = [];
34
35
    /**
36
     * PP2 constructor.
37
     * @throws \InvalidArgumentException
38
     */
39
    public function __construct()
40
    {
41
        $this->parser = new Parser();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Railt\Compiler\Grammar\PP2\Parser() of type object<Railt\Compiler\Grammar\PP2\Parser> is incompatible with the declared type object<Railt\Compiler\Grammar\PP2> of property $parser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
    }
43
44
    /**
45
     * @param Readable $grammar
46
     * @return GrammarInterface
47
     * @throws \LogicException
48
     * @throws \Railt\Io\Exception\ExternalFileException
49
     * @throws \Railt\Parser\Exception\UnrecognizedRuleException
50
     */
51
    public function add(Readable $grammar): GrammarInterface
52
    {
53
        /** @var RuleInterface|LeafInterface $rule */
54
        foreach ($this->parse($grammar) as $rule) {
55
            switch ($rule->getName()) {
56
                case 'Pragma':
57
                    echo \get_class($rule) . "\n";
58
                    break;
59
                case 'Include':
60
                    echo \get_class($rule) . "\n";
61
                    break;
62
                case 'Rule':
63
                    echo \get_class($rule) . "\n";
64
                    break;
65
                case 'Token':
66
                    echo \get_class($rule) . "\n";
67
                    break;
68
            }
69
        }
70
71
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Railt\Compiler\Grammar\PP2) is incompatible with the return type declared by the interface Railt\Compiler\Reader\GrammarInterface::add of type self.

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...
72
    }
73
74
    /**
75
     * @param Readable $grammar
76
     * @return RuleInterface
77
     * @throws \LogicException
78
     * @throws \Railt\Io\Exception\ExternalFileException
79
     * @throws \Railt\Parser\Exception\UnrecognizedRuleException
80
     */
81
    private function parse(Readable $grammar): RuleInterface
82
    {
83
        return $this->parser->parse($grammar);
84
    }
85
86
    /**
87
     * @return Result
88
     */
89
    public function make(): Result
90
    {
91
        foreach ($this->ast as $rule) {
92
            echo $rule . "\n\n";
93
        }
94
        die;
95
    }
96
}
97