Completed
Push — master ( aee4a1...066343 )
by Nico
01:30
created

RuleGenerator::addBoolean()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
crap 4.0312
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 RuleGenerator
15
{
16
    private $output = '';
17
    private $openParenthesis = 0;
18
    private $closedParenthesis = 0;
19
    private $incompleteCondition = false;
20
    private $operatorRequired = false;
21
22 224
    public function clear()
23
    {
24 224
        $this->operatorRequired = false;
25 224
        $this->incompleteCondition = false;
26 224
        $this->output = '';
27 224
    }
28
29 164
    public function get()
30
    {
31 164
        if ($this->isIncompleteCondition()) {
32 4
            throw new Exceptions\ParserException(
33 4
                'Incomplete and/or condition'
34
            );
35 160
        } elseif (!$this->numParenthesesMatch()) {
36 2
            throw new Exceptions\ParserException(
37 2
                'Missing closing parenthesis'
38
            );
39
        }
40
41 158
        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\TokenOpeningParentheses) {
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 176
    public function addBoolean(bool $bool)
95
    {
96 176
        $substr = substr($this->output, -1);
97
98 176
        if ($substr === '1' || $substr === '0') {
99
            throw new \Exception('Missing operator');
100
        }
101
102 176
        $this->operatorRequired = true;
103 176
        $this->incompleteCondition = false;
104 176
        $this->output .=  $bool ? '1' : '0';
105 176
    }
106
107 160
    public function numParenthesesMatch(): bool
108
    {
109 160
        return $this->openParenthesis === $this->closedParenthesis;
110
    }
111
112 164
    public function isIncompleteCondition(): bool
113
    {
114 164
        return $this->incompleteCondition;
115
    }
116
117 180
    public function operatorRequired($false)
118
    {
119 180
        $this->operatorRequired = $false;
120 180
    }
121
122 182
    public function flipOperatorRequired(BaseToken $token)
123
    {
124 182
        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 182
        $this->operatorRequired = !$this->operatorRequired;
133
    }
134
}