Completed
Push — master ( 88ff28...2dc58f )
by Kirill
06:03
created

Grammar   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 57
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A process() 0 4 1
A getState() 0 12 4
A getResult() 0 4 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\Compiler\Reader;
11
12
use Railt\Compiler\Reader\Resolver\PragmaResolver;
13
use Railt\Compiler\Reader\Resolver\ResolverInterface;
14
use Railt\Compiler\Reader\Resolver\RuleResolver;
15
use Railt\Compiler\Reader\Resolver\TokenResolver;
16
use Railt\Io\Readable;
17
use Railt\Lexer\TokenInterface;
18
19
/**
20
 * Class Grammar
21
 */
22
class Grammar
23
{
24
    private const STATE_CONFIGURE   = 0x00;
25
    private const STATE_TOKEN       = 0x01;
26
    private const STATE_PRODUCTIONS = 0x02;
27
28
    /**
29
     * @var array|ResolverInterface[]
30
     */
31
    private $resolvers;
32
33
    /**
34
     * Parser constructor.
35
     */
36
    public function __construct()
37
    {
38
        $this->resolvers = [
39
            self::STATE_CONFIGURE   => new PragmaResolver(),
40
            self::STATE_TOKEN       => new TokenResolver(),
41
            self::STATE_PRODUCTIONS => new RuleResolver(),
42
        ];
43
    }
44
45
    /**
46
     * @param Readable $file
47
     * @param TokenInterface $token
48
     */
49
    public function process(Readable $file, TokenInterface $token): void
50
    {
51
        $this->resolvers[$this->getState($token)]->resolve($file, $token);
52
    }
53
54
    /**
55
     * @param TokenInterface $token
56
     * @return int
57
     */
58
    private function getState(TokenInterface $token): int
59
    {
60
        switch ($token->name()) {
61
            case 'T_PRAGMA':
62
                return self::STATE_CONFIGURE;
63
            case 'T_TOKEN':
64
            case 'T_SKIP':
65
                return self::STATE_TOKEN;
66
            default:
67
                return self::STATE_PRODUCTIONS;
68
        }
69
    }
70
71
    /**
72
     * @return Result
73
     */
74
    public function getResult(): Result
75
    {
76
        return new Result(...$this->resolvers);
0 ignored issues
show
Bug introduced by
The call to Result::__construct() misses some required arguments starting with $tokens.
Loading history...
Documentation introduced by
$this->resolvers is of type array, but the function expects a object<Railt\Compiler\Re...esolver\PragmaResolver>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
    }
78
}
79