UnaryOperator::accept()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 1
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 3
rs 10
1
<?php declare(strict_types = 1);
2
3
namespace Apicart\FQL\Generator\Native;
4
5
use Apicart\FQL\Generator\Common\AbstractVisitor;
6
use Apicart\FQL\Token\Node\LogicalNot;
7
use Apicart\FQL\Token\Node\Mandatory;
8
use Apicart\FQL\Token\Node\Prohibited;
9
use Apicart\FQL\Tokenizer\Tokenizer;
10
use Apicart\FQL\Value\AbstractNode;
11
use LogicException;
12
13
final class UnaryOperator extends AbstractVisitor
14
{
15
16 133
    public function accept(AbstractNode $node): bool
17
    {
18 133
        return $node instanceof Mandatory || $node instanceof Prohibited || $node instanceof LogicalNot;
19
    }
20
21
22 81
    public function visit(AbstractNode $node, ?AbstractVisitor $subVisitor = null, ?array $options = null): string
23
    {
24 81
        if (! $node instanceof Mandatory && ! $node instanceof Prohibited && ! $node instanceof LogicalNot) {
25
            throw new LogicException('Implementation accepts instance of Mandatory, Prohibited or LogicalNot Node');
26
        }
27 81
        if ($subVisitor === null) {
28
            throw new LogicException('Implementation requires sub-visitor');
29
        }
30 81
        $clause = $subVisitor->visit($node->getOperand(), $subVisitor, $options);
31 81
        $padding = '';
32 81
        if ($node->getToken()->getType() === Tokenizer::TOKEN_LOGICAL_NOT) {
33 26
            $padding = ' ';
34
        }
35 81
        return "{$node->getToken()->getLexeme()}{$padding}{$clause}";
36
    }
37
38
}
39