Code Duplication    Length = 69-69 lines in 2 locations

src/Components/GroupKeyword.php 1 location

@@ 47-115 (lines=69) @@
44
     *
45
     * @return GroupKeyword[]
46
     */
47
    public static function parse(Parser $parser, TokensList $list, array $options = array())
48
    {
49
        $ret = array();
50
51
        $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
        $state = 0;
66
67
        for (; $list->idx < $list->count; ++$list->idx) {
68
            /**
69
             * Token parsed at this moment.
70
             *
71
             * @var Token
72
             */
73
            $token = $list->tokens[$list->idx];
74
75
            // End of statement.
76
            if ($token->type === Token::TYPE_DELIMITER) {
77
                break;
78
            }
79
80
            // Skipping whitespaces and comments.
81
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
82
                continue;
83
            }
84
85
            if ($state === 0) {
86
                $expr->expr = Expression::parse($parser, $list);
87
                $state = 1;
88
            } elseif ($state === 1) {
89
                if (($token->type === Token::TYPE_KEYWORD)
90
                    && (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
91
                ) {
92
                    $expr->type = $token->keyword;
93
                } elseif (($token->type === Token::TYPE_OPERATOR)
94
                    && ($token->value === ',')
95
                ) {
96
                    if (!empty($expr->expr)) {
97
                        $ret[] = $expr;
98
                    }
99
                    $expr = new self();
100
                    $state = 0;
101
                } else {
102
                    break;
103
                }
104
            }
105
        }
106
107
        // Last iteration was not processed.
108
        if (!empty($expr->expr)) {
109
            $ret[] = $expr;
110
        }
111
112
        --$list->idx;
113
114
        return $ret;
115
    }
116
117
    /**
118
     * @param GroupKeyword|GroupKeyword[] $component the component to be built

src/Components/OrderKeyword.php 1 location

@@ 56-124 (lines=69) @@
53
     *
54
     * @return OrderKeyword[]
55
     */
56
    public static function parse(Parser $parser, TokensList $list, array $options = array())
57
    {
58
        $ret = array();
59
60
        $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
        $state = 0;
75
76
        for (; $list->idx < $list->count; ++$list->idx) {
77
            /**
78
             * Token parsed at this moment.
79
             *
80
             * @var Token
81
             */
82
            $token = $list->tokens[$list->idx];
83
84
            // End of statement.
85
            if ($token->type === Token::TYPE_DELIMITER) {
86
                break;
87
            }
88
89
            // Skipping whitespaces and comments.
90
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
91
                continue;
92
            }
93
94
            if ($state === 0) {
95
                $expr->expr = Expression::parse($parser, $list);
96
                $state = 1;
97
            } elseif ($state === 1) {
98
                if (($token->type === Token::TYPE_KEYWORD)
99
                    && (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
100
                ) {
101
                    $expr->type = $token->keyword;
102
                } elseif (($token->type === Token::TYPE_OPERATOR)
103
                    && ($token->value === ',')
104
                ) {
105
                    if (!empty($expr->expr)) {
106
                        $ret[] = $expr;
107
                    }
108
                    $expr = new self();
109
                    $state = 0;
110
                } else {
111
                    break;
112
                }
113
            }
114
        }
115
116
        // Last iteration was not processed.
117
        if (!empty($expr->expr)) {
118
            $ret[] = $expr;
119
        }
120
121
        --$list->idx;
122
123
        return $ret;
124
    }
125
126
    /**
127
     * @param OrderKeyword|OrderKeyword[] $component the component to be built