Passed
Push — master ( 4da460...9bdebc )
by Michal
04:05
created

GroupKeyword::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * `GROUP BY` keyword parser.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Components;
8
9
use PhpMyAdmin\SqlParser\Component;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\Token;
12
use PhpMyAdmin\SqlParser\TokensList;
13
14
/**
15
 * `GROUP BY` keyword parser.
16
 *
17
 * @category   Keywords
18
 *
19
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
20
 */
21
class GroupKeyword extends Component
22
{
23
    /**
24
     * The expression that is used for grouping.
25
     *
26
     * @var Expression
27
     */
28
    public $expr;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param Expression $expr the expression that we are sorting by
0 ignored issues
show
Documentation introduced by
Should the type for parameter $expr not be Expression|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
34
     */
35 4
    public function __construct($expr = null)
36
    {
37 4
        $this->expr = $expr;
38 4
    }
39
40
    /**
41
     * @param Parser     $parser  the parser that serves as context
42
     * @param TokensList $list    the list of tokens that are being parsed
43
     * @param array      $options parameters for parsing
44
     *
45
     * @return GroupKeyword[]
46
     */
47 3 View Code Duplication
    public static function parse(Parser $parser, TokensList $list, array $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
48
    {
49 3
        $ret = array();
50
51 3
        $expr = new self();
52
53
        /**
54
         * The state of the parser.
55
         *
56
         * Below are the states of the parser.
57
         *
58
         *      0 --------------------[ expression ]-------------------> 1
59
         *
60
         *      1 ------------------------[ , ]------------------------> 0
61
         *      1 -------------------[ ASC / DESC ]--------------------> 1
62
         *
63
         * @var int
64
         */
65 3
        $state = 0;
66
67 3
        for (; $list->idx < $list->count; ++$list->idx) {
68
            /**
69
             * Token parsed at this moment.
70
             *
71
             * @var Token
72
             */
73 3
            $token = $list->tokens[$list->idx];
74
75
            // End of statement.
76 3
            if ($token->type === Token::TYPE_DELIMITER) {
77 2
                break;
78
            }
79
80
            // Skipping whitespaces and comments.
81 3
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
82 3
                continue;
83
            }
84
85 3
            if ($state === 0) {
86 3
                $expr->expr = Expression::parse($parser, $list);
87 3
                $state = 1;
88 1
            } elseif ($state === 1) {
89 1
                if (($token->type === Token::TYPE_KEYWORD)
90 1
                    && (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
91
                ) {
92
                    $expr->type = $token->keyword;
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
93 1
                } elseif (($token->type === Token::TYPE_OPERATOR)
94 1
                    && ($token->value === ',')
95
                ) {
96
                    if (!empty($expr->expr)) {
97
                        $ret[] = $expr;
98
                    }
99
                    $expr = new self();
100
                    $state = 0;
101
                } else {
102 1
                    break;
103
                }
104
            }
105
        }
106
107
        // Last iteration was not processed.
108 3
        if (!empty($expr->expr)) {
109 3
            $ret[] = $expr;
110
        }
111
112 3
        --$list->idx;
113
114 3
        return $ret;
115
    }
116
117
    /**
118
     * @param GroupKeyword|GroupKeyword[] $component the component to be built
119
     * @param array                       $options   parameters for building
120
     *
121
     * @return string
122
     */
123 3
    public static function build($component, array $options = array())
124
    {
125 3
        if (is_array($component)) {
126 3
            return implode(', ', $component);
127
        }
128
129 3
        return trim($component->expr);
130
131
    }
132
}
133