Completed
Pull Request — master (#34)
by
unknown
03:03
created

AlterStatement   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 93.62%

Importance

Changes 11
Bugs 6 Features 3
Metric Value
wmc 14
c 11
b 6
f 3
lcom 1
cbo 6
dl 0
loc 130
ccs 44
cts 47
cp 0.9362
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 0 71 12
A build() 0 11 2
1
<?php
2
3
/**
4
 * `ALTER` statement.
5
 *
6
 * @package    SqlParser
7
 * @subpackage Statements
8
 */
9
namespace SqlParser\Statements;
10
11
use SqlParser\Parser;
12
use SqlParser\Statement;
13
use SqlParser\Token;
14
use SqlParser\TokensList;
15
use SqlParser\Components\AlterOperation;
16
use SqlParser\Components\Expression;
17
use SqlParser\Components\OptionsArray;
18
19
/**
20
 * `ALTER` statement.
21
 *
22
 * @category   Statements
23
 * @package    SqlParser
24
 * @subpackage Statements
25
 * @author     Dan Ungureanu <[email protected]>
26
 * @license    http://opensource.org/licenses/GPL-2.0 GNU Public License
27
 */
28
class AlterStatement extends Statement
29
{
30
31
    /**
32
     * Table affected.
33
     *
34
     * @var Expression
35
     */
36
    public $table;
37
38
    /**
39
     * Column affected by this statement.
40
     *
41
     * @var AlterOperation[]
42
     */
43
    public $altered = array();
44
45
    /**
46
     * Options of this statement.
47
     *
48
     * @var array
49
     */
50
    public static $OPTIONS = array(
51
        'ONLINE'                        => 1,
52
        'OFFLINE'                       => 1,
53
        'IGNORE'                        => 2,
54
55
        'DATABASE'                      => 3,
56
        'EVENT'                         => 3,
57
        'FUNCTION'                      => 3,
58
        'PROCEDURE'                     => 3,
59
        'SERVER'                        => 3,
60
        'TABLE'                         => 3,
61
        'TABLESPACE'                    => 3,
62
        'VIEW'                          => 3,
63
    );
64
65
    /**
66
     * @param Parser     $parser The instance that requests parsing.
67
     * @param TokensList $list   The list of tokens to be parsed.
68
     *
69
     * @return void
70
     */
71 5
    public function parse(Parser $parser, TokensList $list)
72
    {
73 5
        ++$list->idx; // Skipping `ALTER`.
74 5
        $this->options = OptionsArray::parse(
75 5
            $parser,
76 5
            $list,
77
            static::$OPTIONS
78 5
        );
79 5
        ++$list->idx;
80
81
        // Parsing affected table.
82 5
        $this->table = Expression::parse(
83 5
            $parser,
84 5
            $list,
85
            array(
86 5
                'parseField' => 'table',
87 5
                'breakOnAlias' => true,
88
            )
89 5
        );
90 5
        ++$list->idx; // Skipping field.
91
92
        /**
93
         * The state of the parser.
94
         *
95
         * Below are the states of the parser.
96
         *
97
         *      0 -----------------[ alter operation ]-----------------> 1
98
         *
99
         *      1 -------------------------[ , ]-----------------------> 0
100
         *
101
         * @var int $state
102
         */
103 5
        $state = 0;
104
105 5
        for (; $list->idx < $list->count; ++$list->idx) {
106
            /**
107
             * Token parsed at this moment.
108
             *
109
             * @var Token $token
110
             */
111 5
            $token = $list->tokens[$list->idx];
112
113
            // End of statement.
114 5
            if ($token->type === Token::TYPE_DELIMITER) {
115 5
                break;
116
            }
117
118
            // Skipping whitespaces and comments.
119 5
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
120 5
                continue;
121
            }
122
123 5
            if ($state === 0) {
124 5
                $options = array();
125 5
                if ($this->options->has('DATABASE')) {
126
                    $options = AlterOperation::$DB_OPTIONS;
127 5
                } elseif ($this->options->has('TABLE')) {
128 5
                    $options = AlterOperation::$TABLE_OPTIONS;
129 5
                } elseif ($this->options->has('VIEW')) {
130
                    $options = AlterOperation::$VIEW_OPTIONS;
131
                }
132
133 5
                $this->altered[] = AlterOperation::parse($parser, $list, $options);
134 5
                $state = 1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
135 5
            } elseif ($state === 1) {
136 3
                if (($token->type === Token::TYPE_OPERATOR) && ($token->value === ',')) {
137 3
                    $state = 0;
138 3
                }
139 3
            }
140 5
        }
141 5
    }
142
143
    /**
144
     * @return string
145
     */
146 1
    public function build()
147
    {
148 1
        $tmp = array();
149 1
        foreach ($this->altered as $altered) {
150 1
            $tmp[] = $altered::build($altered);
151 1
        }
152
153 1
        return 'ALTER ' . OptionsArray::build($this->options)
154 1
               . ' '    . Expression::build($this->table)
155 1
               . ' '    . implode(', ', $tmp);
156
    }
157
}
158