Completed
Push — master ( 65f66e...428edc )
by Michal
04:14
created

Limit::parse()   C

Complexity

Conditions 14
Paths 6

Size

Total Lines 61
Code Lines 31

Duplication

Lines 5
Ratio 8.2 %

Code Coverage

Tests 35
CRAP Score 14

Importance

Changes 0
Metric Value
cc 14
eloc 31
nc 6
nop 3
dl 5
loc 61
ccs 35
cts 35
cp 1
crap 14
rs 6.2465
c 0
b 0
f 0

How to fix   Long Method    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
3
/**
4
 * `LIMIT` 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
 * `LIMIT` keyword parser.
16
 *
17
 * @category   Keywords
18
 *
19
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
20
 */
21
class Limit extends Component
22
{
23
    /**
24
     * The number of rows skipped.
25
     *
26
     * @var int
27
     */
28
    public $offset;
29
30
    /**
31
     * The number of rows to be returned.
32
     *
33
     * @var int
34
     */
35
    public $rowCount;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param int $rowCount the row count
41
     * @param int $offset   the offset
42
     */
43 21
    public function __construct($rowCount = 0, $offset = 0)
44
    {
45 21
        $this->rowCount = $rowCount;
46 21
        $this->offset = $offset;
47 21
    }
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 Limit
55
     */
56 18
    public static function parse(Parser $parser, TokensList $list, array $options = array())
57
    {
58 18
        $ret = new self();
59
60 18
        $offset = false;
61
62 18
        for (; $list->idx < $list->count; ++$list->idx) {
63
            /**
64
             * Token parsed at this moment.
65
             *
66
             * @var Token
67
             */
68 18
            $token = $list->tokens[$list->idx];
69
70
            // End of statement.
71 18
            if ($token->type === Token::TYPE_DELIMITER) {
72 15
                break;
73
            }
74
75
            // Skipping whitespaces and comments.
76 18
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
77 18
                continue;
78
            }
79
80 18
            if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
81 4
                break;
82
            }
83
84 18
            if (($token->type === Token::TYPE_KEYWORD) && ($token->value === 'OFFSET')) {
85 4
                if ($offset) {
86 1
                    $parser->error(__('An offset was expected.'), $token);
87 1
                }
88 4
                $offset = true;
89 4
                continue;
90
            }
91
92 18 View Code Duplication
            if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) {
93 12
                $ret->offset = $ret->rowCount;
94 12
                $ret->rowCount = 0;
95 12
                continue;
96
            }
97
98 18
            if ($offset) {
99 3
                $ret->offset = $token->value;
100 3
                $offset = false;
101 3
            } else {
102 18
                $ret->rowCount = $token->value;
103
            }
104 18
        }
105
106 18
        if ($offset) {
107 1
            $parser->error(
108 1
                __('An offset was expected.'),
109 1
                $list->tokens[$list->idx - 1]
110 1
            );
111 1
        }
112
113 18
        --$list->idx;
114
115 18
        return $ret;
116
    }
117
118
    /**
119
     * @param Limit $component the component to be built
120
     * @param array $options   parameters for building
121
     *
122
     * @return string
123
     */
124 5
    public static function build($component, array $options = array())
125
    {
126 5
        return $component->offset . ', ' . $component->rowCount;
127
    }
128
}
129