Passed
Push — master ( c776c7...570285 )
by Kirill
04:05
created

DeclareGrammar::parse()   C

Complexity

Conditions 15
Paths 40

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 36
c 1
b 0
f 0
nc 40
nop 1
dl 0
loc 55
rs 5.9166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Stempler\Lexer\Grammar\Dynamic;
13
14
use Spiral\Stempler\Lexer\Buffer;
15
use Spiral\Stempler\Lexer\Byte;
16
use Spiral\Stempler\Lexer\Grammar\Traits\TokenTrait;
17
use Spiral\Stempler\Lexer\GrammarInterface;
18
use Spiral\Stempler\Lexer\Token;
19
20
/**
21
 * Provides ability to parse complex declare options.
22
 */
23
final class DeclareGrammar implements GrammarInterface
24
{
25
    use TokenTrait;
0 ignored issues
show
introduced by
The trait Spiral\Stempler\Lexer\Grammar\Traits\TokenTrait requires some properties which are not provided by Spiral\Stempler\Lexer\Gr...\Dynamic\DeclareGrammar: $char, $content, $type
Loading history...
26
27
    public const TYPE_KEYWORD = 0;
28
    public const TYPE_EQUAL   = 1;
29
    public const TYPE_COMMA   = 2;
30
    public const TYPE_QUOTED  = 3;
31
32
    // whitespace
33
    private const REGEXP_WHITESPACE = '/\s/';
34
35
    /** @var array */
36
    private $keyword = [];
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function parse(Buffer $src): \Generator
42
    {
43
        while ($n = $src->next()) {
44
            switch ($n->char) {
0 ignored issues
show
Bug introduced by
The property char does not seem to exist on Spiral\Stempler\Lexer\Token.
Loading history...
45
                case '"':
46
                case "'":
47
                    if ($this->keyword !== []) {
48
                        yield $this->packToken($this->keyword, self::TYPE_KEYWORD);
49
                        $this->keyword = [];
50
                    }
51
52
                    $quoted[] = $n;
53
                    while ($nn = $src->next()) {
54
                        $quoted[] = $nn;
55
                        if ($nn instanceof Byte && $nn->char === $n->char) {
56
                            break;
57
                        }
58
                    }
59
60
                    yield $this->packToken($quoted, self::TYPE_QUOTED);
61
                    $quoted = [];
62
63
                    break;
64
                case '=':
65
                    if ($this->keyword !== []) {
66
                        yield $this->packToken($this->keyword, self::TYPE_KEYWORD);
67
                        $this->keyword = [];
68
                    }
69
70
                    yield new Token(self::TYPE_EQUAL, $n->offset, '=');
71
                    break;
72
                case ',':
73
                    if ($this->keyword !== []) {
74
                        yield $this->packToken($this->keyword, self::TYPE_KEYWORD);
75
                        $this->keyword = [];
76
                    }
77
78
                    yield new Token(self::TYPE_COMMA, $n->offset, ',');
79
                    break;
80
                default:
81
                    if (preg_match(self::REGEXP_WHITESPACE, $n->char)) {
82
                        if ($this->keyword !== []) {
83
                            yield $this->packToken($this->keyword, self::TYPE_KEYWORD);
84
                            $this->keyword = [];
85
                        }
86
87
                        break;
88
                    }
89
90
                    $this->keyword[] = $n;
91
            }
92
        }
93
94
        if ($this->keyword !== []) {
95
            yield $this->packToken($this->keyword, self::TYPE_KEYWORD);
96
        }
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public static function tokenName(int $token): string
103
    {
104
        switch ($token) {
105
            case self::TYPE_KEYWORD:
106
                return 'DECLARE:KEYWORD';
107
            case self::TYPE_EQUAL:
108
                return 'DECLARE:EQUAL';
109
            case self::TYPE_COMMA:
110
                return 'DECLARE:COMMA';
111
            case self::TYPE_QUOTED:
112
                return 'DECLARE:QUOTED';
113
            default:
114
                return 'DECLARE:UNDEFINED';
115
        }
116
    }
117
}
118