Completed
Push — master ( 82eb41...4f6b0a )
by Alexander
03:49
created

GroupNodeParser::parse()   D

Complexity

Conditions 10
Paths 17

Size

Total Lines 37
Code Lines 26

Duplication

Lines 10
Ratio 27.03 %

Code Coverage

Tests 24
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 37
ccs 24
cts 24
cp 1
rs 4.8196
cc 10
eloc 26
nc 17
nop 1
crap 10

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
namespace Xiag\Rql\Parser\NodeParser\Query;
3
4
use Xiag\Rql\Parser\Token;
5
use Xiag\Rql\Parser\TokenStream;
6
use Xiag\Rql\Parser\NodeParserInterface;
7
use Xiag\Rql\Parser\SubParserInterface;
8
use Xiag\Rql\Parser\Exception\SyntaxErrorException;
9
use Xiag\Rql\Parser\Node\AbstractQueryNode;
10
use Xiag\Rql\Parser\Node\Query\LogicalOperator\AndNode;
11
use Xiag\Rql\Parser\Node\Query\LogicalOperator\OrNode;
12
13
class GroupNodeParser implements NodeParserInterface
14
{
15
    /**
16
     * @var SubParserInterface
17
     */
18
    protected $conditionParser;
19
20
    /**
21
     * @param SubParserInterface $conditionParser
22
     */
23 64
    public function __construct(SubParserInterface $conditionParser)
24
    {
25 64
        $this->conditionParser = $conditionParser;
26 64
    }
27
28
    /**
29
     * @inheritdoc
30
     * @return AbstractQueryNode
31
     */
32 5
    public function parse(TokenStream $tokenStream)
33
    {
34 5
        $operator = null;
35 5
        $queries = [];
36
37 5
        $tokenStream->expect(Token::T_OPEN_PARENTHESIS);
38
39
        do {
40 5
            $queries[] = $this->conditionParser->parse($tokenStream);
41
42 5
            if ($tokenStream->nextIf(Token::T_AMPERSAND)) {
43 4 View Code Duplication
                if ($operator === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44 3
                    $operator = Token::T_AMPERSAND;
45 4
                } elseif ($operator !== Token::T_AMPERSAND) {
46 1
                    throw new SyntaxErrorException('Cannot mix "&" and "|" within a group');
47
                }
48 5
            } elseif ($tokenStream->nextIf(Token::T_VERTICAL_BAR)) {
49 4 View Code Duplication
                if ($operator === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 3
                    $operator = Token::T_VERTICAL_BAR;
51 4
                } elseif ($operator !== Token::T_VERTICAL_BAR) {
52 1
                    throw new SyntaxErrorException('Cannot mix "&" and "|" within a group');
53
                }
54 3
            } else {
55 3
                break;
56
            }
57 4
        } while (true);
58
59 3
        $tokenStream->expect(Token::T_CLOSE_PARENTHESIS);
60
61 3
        if ($operator === Token::T_VERTICAL_BAR) {
62 2
            return new OrNode($queries);
63 3
        } elseif ($operator === Token::T_AMPERSAND) {
64 2
            return new AndNode($queries);
65
        } else {
66 1
            return $queries[0];
67
        }
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 64
    public function supports(TokenStream $tokenStream)
74
    {
75 64
        return $tokenStream->test(Token::T_OPEN_PARENTHESIS);
76
    }
77
}
78