Completed
Push — master ( e5de66...386907 )
by Nico
02:02
created

Compiler::operatorRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @license     http://opensource.org/licenses/mit-license.php MIT
7
 * @link        https://github.com/nicoSWD
8
 * @author      Nicolas Oelgart <[email protected]>
9
 */
10
namespace nicoSWD\Rules;
11
12
use nicoSWD\Rules\Tokens\BaseToken;
13
14
class Compiler
15
{
16
    private $output = '';
17
    private $openParenthesis = 0;
18
    private $closedParenthesis = 0;
19
    private $incompleteCondition = false;
20
    private $operatorRequired = false;
21
22 220
    public function clear()
23
    {
24 220
        $this->operatorRequired = false;
25 220
        $this->incompleteCondition = false;
26 220
        $this->output = '';
27 220
    }
28
29 160
    public function getCompiledRule()
30
    {
31 160
        if ($this->isIncompleteCondition()) {
32 4
            throw new Exceptions\ParserException(
33 4
                'Incomplete and/or condition'
34
            );
35 156
        } elseif (!$this->numParenthesesMatch()) {
36 2
            throw new Exceptions\ParserException(
37 2
                'Missing closing parenthesis'
38
            );
39
        }
40
41 154
        return $this->output;
42
    }
43
44 20
    public function openParentheses(BaseToken $token)
0 ignored issues
show
Unused Code introduced by
The parameter $token is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46 20
        $this->openParenthesis++;
47 20
        $this->output .= '(';
48 20
    }
49
50 20
    public function closeParentheses(BaseToken $token)
51
    {
52 20
        if ($this->openParenthesis < 1) {
53 2
            throw new Exceptions\ParserException(sprintf(
54 2
                'Missing opening parenthesis at position %d on line %d',
55 2
                $token->getPosition(),
56 2
                $token->getLine()
57
            ));
58
        }
59
60 18
        $this->closedParenthesis++;
61 18
        $this->output .= ')';
62 18
    }
63
64 22
    public function addParentheses(BaseToken $token)
65
    {
66 22
        if ($token instanceof Tokens\TokenOpeningParenthesis) {
67 20
            $lastChar = substr($this->output, -1);
68
69 20
            if ($lastChar !== '' && $lastChar !== '&' && $lastChar !== '|' && $lastChar !== '(') {
70 4
                throw Exceptions\ParserException::unexpectedToken($token);
71
            }
72 20
            $this->openParentheses($token);
73
        } else {
74 20
            $this->closeParentheses($token);
75
        }
76 20
    }
77
78 36
    public function addLogical(BaseToken $token)
79
    {
80 36
        if (!$this->operatorRequired) {
81 2
            throw Exceptions\ParserException::unexpectedToken($token);
82
        }
83
84 36
        if ($token instanceof Tokens\TokenAnd) {
85 34
            $this->output .= '&';
86
        } else {
87 14
            $this->output .= '|';
88
        }
89
90 36
        $this->operatorRequired = false;
91 36
        $this->incompleteCondition = true;
92 36
    }
93
94 172
    public function addBoolean(bool $bool)
95
    {
96 172
        $lastChar = substr($this->output, -1);
97
98 172
        if ($lastChar === '1' || $lastChar === '0') {
99
            throw new \Exception('Missing operator');
100
        }
101
102 172
        $this->operatorRequired = true;
103 172
        $this->incompleteCondition = false;
104 172
        $this->output .= $bool ? '1' : '0';
105 172
    }
106
107 156
    private function numParenthesesMatch(): bool
108
    {
109 156
        return $this->openParenthesis === $this->closedParenthesis;
110
    }
111
112 160
    private function isIncompleteCondition(): bool
113
    {
114 160
        return $this->incompleteCondition;
115
    }
116
117 176
    public function operatorRequired(bool $bool)
118
    {
119 176
        $this->operatorRequired = $bool;
120 176
    }
121
122 178
    public function flipOperatorRequired(BaseToken $token)
123
    {
124 178
        if ($this->operatorRequired) {
125 2
            throw new Exceptions\ParserException(sprintf(
126 2
                'Missing operator at position %d on line %d',
127 2
                $token->getPosition(),
128 2
                $token->getLine()
129
            ));
130
        }
131
132 178
        $this->operatorRequired = !$this->operatorRequired;
133
    }
134
}