Passed
Push — master ( 3a32e2...77143f )
by Edward
04:21
created

TranslationScheme::applyProductionActions()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 24
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 40
ccs 24
cts 24
cp 1
crap 11
rs 7.3166

How to fix   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
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Pointer\Parser;
5
6
use Remorhaz\UniLex\Grammar\SDD\TranslationSchemeInterface;
7
use Remorhaz\UniLex\Lexer\Token;
8
use Remorhaz\UniLex\Parser\Production;
9
use Remorhaz\UniLex\Parser\Symbol;
10
11
final class TranslationScheme implements TranslationSchemeInterface
12
{
13
14
    private $locatorBuilder;
15
16 20
    public function __construct(LocatorBuilderInterface $locatorBuilder)
17
    {
18 20
        $this->locatorBuilder = $locatorBuilder;
19 20
    }
20
21 18
    public function applyTokenActions(Symbol $symbol, Token $token): void
22
    {
23 18
        $s = $symbol->getShortcut();
24 18
        $t = $token->getShortcut();
25 18
        switch ($symbol->getSymbolId()) {
26
            case SymbolType::T_UNESCAPED:
27
            case SymbolType::T_SLASH:
28
            case SymbolType::T_TILDE:
29
            case SymbolType::T_ZERO:
30
            case SymbolType::T_ONE:
31 18
                $s['s.text'] = $t['text'];
32 18
                break;
33
        }
34 18
    }
35
36 14
    public function applyProductionActions(Production $production): void
37
    {
38 14
        $header = $production->getHeaderShortcut();
39 14
        $symbols = $production->getSymbolListShortcut();
40 14
        $hash = "{$production->getHeader()->getSymbolId()}.{$production->getIndex()}";
41 14
        switch ($hash) {
42
            case SymbolType::NT_REFERENCE . '.0':
43
                // [ 0:NT_REFERENCE_PART, 1:NT_REFERENCE ]
44 14
            case SymbolType::NT_ESCAPED . '.0':
45
                // [ 0:T_TILDE, 1:NT_ESCAPED_SYMBOL ]
46 12
                $header['s.text'] = $symbols[1]['s.text'];
47 12
                break;
48
49 14
            case SymbolType::NT_REFERENCE . '.1':
50
                // [ ]
51 13
                $header['s.text'] = $header['i.text'];
52 13
                break;
53
54 14
            case SymbolType::NT_REFERENCE_PART . '.0':
55
                // [ 0:NT_UNESCAPED ]
56 14
            case SymbolType::NT_REFERENCE_PART . '.1':
57
                // [ 0:NT_ESCAPED ]
58 14
            case SymbolType::NT_UNESCAPED . '.0':
59
                // [ 0:T_UNESCAPED ]
60 14
            case SymbolType::NT_UNESCAPED . '.1':
61
                // [ 0:T_ZERO ]
62 14
            case SymbolType::NT_UNESCAPED . '.2':
63
                // [ 0:T_ONE ]
64 12
                $header['s.text'] = $symbols[0]['s.text'];
65 12
                break;
66
67 14
            case SymbolType::NT_ESCAPED_SYMBOL . '.0':
68
                // [ 0:T_ZERO ]
69 6
                $header['s.text'] = '~';
70 6
                break;
71
72 14
            case SymbolType::NT_ESCAPED_SYMBOL . '.1':
73
                // [ 0:T_ONE ]
74 5
                $header['s.text'] = '/';
75 5
                break;
76
        }
77 14
    }
78
79 20
    public function applySymbolActions(Production $production, int $symbolIndex): void
80
    {
81 20
        $header = $production->getHeaderShortcut();
82 20
        $symbols = $production->getSymbolListShortcut();
83 20
        $hash = "{$production->getHeader()->getSymbolId()}.{$production->getIndex()}.{$symbolIndex}";
84 20
        switch ($hash) {
85
            case SymbolType::NT_REFERENCE_LIST . '.0.1':
86
                // [ 0:T_SLASH, 1:NT_REFERENCE, 2:NT_REFERENCE_LIST ]
87 18
                $symbols[1]['i.text'] = '';
88 18
                break;
89
90
            case SymbolType::NT_REFERENCE_LIST . '.0.2':
91
                // [ 0:T_SLASH, 1:NT_REFERENCE, 2:NT_REFERENCE_LIST ]
92
                $this
93 13
                    ->locatorBuilder
94 13
                    ->addReference($symbols[1]['s.text']);
95 13
                break;
96
97
            case SymbolType::NT_REFERENCE . '.0.1':
98
                // [ 0:NT_REFERENCE_PART, 1:NT_REFERENCE ]
99 12
                $symbols[1]['i.text'] = $header['i.text'] . $symbols[0]['s.text'];
100 12
                break;
101
        }
102 20
    }
103
}
104