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

OrderKeyword::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * `ORDER 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
 * `ORDER 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 OrderKeyword extends Component
22
{
23
    /**
24
     * The expression that is used for ordering.
25
     *
26
     * @var Expression
27
     */
28
    public $expr;
29
30
    /**
31
     * The order type.
32
     *
33
     * @var string
34
     */
35
    public $type;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @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...
41
     * @param string     $type the sorting type
42
     */
43 18
    public function __construct($expr = null, $type = 'ASC')
44
    {
45 18
        $this->expr = $expr;
46 18
        $this->type = $type;
47 18
    }
48
49
    /**
50
     * @param Parser     $parser  the parser that serves as context
51
     * @param TokensList $list    the list of tokens that are being parsed
52
     * @param array      $options parameters for parsing
53
     *
54
     * @return OrderKeyword[]
55
     */
56 17 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...
57
    {
58 17
        $ret = array();
59
60 17
        $expr = new self();
61
62
        /**
63
         * The state of the parser.
64
         *
65
         * Below are the states of the parser.
66
         *
67
         *      0 --------------------[ expression ]-------------------> 1
68
         *
69
         *      1 ------------------------[ , ]------------------------> 0
70
         *      1 -------------------[ ASC / DESC ]--------------------> 1
71
         *
72
         * @var int
73
         */
74 17
        $state = 0;
75
76 17
        for (; $list->idx < $list->count; ++$list->idx) {
77
            /**
78
             * Token parsed at this moment.
79
             *
80
             * @var Token
81
             */
82 17
            $token = $list->tokens[$list->idx];
83
84
            // End of statement.
85 17
            if ($token->type === Token::TYPE_DELIMITER) {
86 7
                break;
87
            }
88
89
            // Skipping whitespaces and comments.
90 17
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
91 17
                continue;
92
            }
93
94 17
            if ($state === 0) {
95 17
                $expr->expr = Expression::parse($parser, $list);
96 17
                $state = 1;
97 13
            } elseif ($state === 1) {
98 13
                if (($token->type === Token::TYPE_KEYWORD)
99 13
                    && (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
100
                ) {
101 10
                    $expr->type = $token->keyword;
102 12
                } elseif (($token->type === Token::TYPE_OPERATOR)
103 12
                    && ($token->value === ',')
104
                ) {
105 3
                    if (!empty($expr->expr)) {
106 3
                        $ret[] = $expr;
107
                    }
108 3
                    $expr = new self();
109 3
                    $state = 0;
110
                } else {
111 11
                    break;
112
                }
113
            }
114
        }
115
116
        // Last iteration was not processed.
117 17
        if (!empty($expr->expr)) {
118 17
            $ret[] = $expr;
119
        }
120
121 17
        --$list->idx;
122
123 17
        return $ret;
124
    }
125
126
    /**
127
     * @param OrderKeyword|OrderKeyword[] $component the component to be built
128
     * @param array                       $options   parameters for building
129
     *
130
     * @return string
131
     */
132 3
    public static function build($component, array $options = array())
133
    {
134 3
        if (is_array($component)) {
135 2
            return implode(', ', $component);
136
        }
137
138 3
        return $component->expr . ' ' . $component->type;
139
    }
140
}
141