QuerySyntax::getDefaultSyntaxGroup()   F
last analyzed

Complexity

Conditions 18
Paths 288

Size

Total Lines 47
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
eloc 21
c 1
b 0
f 0
nc 288
nop 1
dl 0
loc 47
rs 2.9333

How to fix   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
namespace Migratio\GrammarStructure\Mysql;
4
5
class QuerySyntax extends QuerySyntaxHelper
6
{
7
    /**
8
     * @var array $data
9
     */
10
    protected $data = array();
11
    
12
    /**
13
     * @var array $syntax
14
     */
15
    protected $syntax = array();
16
17
    /**
18
     * @var null|string
19
     */
20
    protected $group;
21
22
    /**
23
     * @var array 
24
     */
25
    protected $alterExtras = array();
26
27
    /**
28
     * add column
29
     * 
30
     * @param $alterType
31
     * @return array
32
     */
33
    private function addColumn($alterType)
34
    {
35
        if(isset($alterType['place'])){
36
37
38
            foreach ($alterType['place'] as $placeKey=>$placeValue){
39
                $placeList=$placeKey .' '.$placeValue.'';
40
            }
41
42
            $syntax = implode("",$this->syntax);
43
44
            $alterSytanx = 'ALTER TABLE '.$this->table.' ADD COLUMN '.$syntax.' '.$placeList;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $placeList seems to be defined by a foreach iteration on line 38. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
45
46
            $query=$this->schema->getConnection()->setQueryBasic($alterSytanx);
47
48
            if(count($this->alterExtras)){
49
                foreach($this->alterExtras as $extra){
50
51
                    $extraSyntax = 'ALTER TABLE '.$this->table.' '.$extra.'';
52
53
                    $query=$this->schema->getConnection()->setQueryBasic($extraSyntax);
54
                }
55
            }
56
57
            $this->alterExtras = [];
58
59
            return [
60
                'syntax'=>$syntax,
61
                'type'=>'addColumn',
62
                'result'=>$query['result'],
63
                'message'=>$query['message'],
64
            ];
65
        }
66
67
    }
68
69
    /**
70
     * change column
71
     * 
72
     * @param $alterType
73
     * @return array
74
     */
75
    private function change($alterType)
76
    {
77
        if(isset($alterType['place'])){
78
79
            foreach ($alterType['place'] as $placeKey=>$placeValue){
80
                $placeList=$placeKey .' '.$placeValue.'';
81
            }
82
83
            $syntax = implode("",$this->syntax);
84
85
            $columns = $this->schema->getConnection()->showColumnsFrom($this->table);
86
87
            foreach ($columns as $columnKey=>$columnData){
88
                if($columnData['Field']==$placeValue){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $placeValue seems to be defined by a foreach iteration on line 79. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
89
                    $changeAbleField = $columns[$columnKey+1]['Field'];
90
                }
91
            }
92
93
            $syntaxList = explode(' ',$syntax);
94
95
            if(current($syntaxList)!==$changeAbleField){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $changeAbleField does not seem to be defined for all execution paths leading up to this point.
Loading history...
96
                $alterSytanx = 'ALTER TABLE '.$this->table.' change '.$changeAbleField.' '.current($syntaxList).'  '.implode(' ',array_splice($syntaxList,1)).' '.$placeList;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $placeList seems to be defined by a foreach iteration on line 79. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
97
            }
98
            else{
99
                $alterSytanx = 'ALTER TABLE '.$this->table.' modify '.$syntax.' '.$placeList;
100
            }
101
102
103
            $query=$this->schema->getConnection()->setQueryBasic($alterSytanx);
104
105
            if(count($this->alterExtras)){
106
                foreach($this->alterExtras as $extra){
107
108
                    $extraSyntax = 'ALTER TABLE '.$this->table.' '.$extra.'';
109
110
                    $query=$this->schema->getConnection()->setQueryBasic($extraSyntax);
111
                }
112
            }
113
114
            $this->alterExtras = [];
115
116
            return [
117
                'syntax'=>$syntax,
118
                'type'=>'create',
119
                'result'=>$query['result'],
120
                'message'=>$query['message'],
121
            ];
122
        }
123
    }
124
125
    /**
126
     * add Indexes
127
     *
128
     * @param $alterType
129
     */
130
    private function addIndex($alterType)
131
    {
132
        if(isset($this->syntax[0])){
133
134
            $index = $this->syntax[0];
135
136
            foreach($index as $name=>$item){
137
                $index_name = $name;
138
                $indexes = implode(',',$item);
139
            }
140
            $alterSytanx = 'create index '.$index_name.' on '.$this->table.' ('.$indexes.')';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $index_name seems to be defined by a foreach iteration on line 136. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
Comprehensibility Best Practice introduced by
The variable $indexes seems to be defined by a foreach iteration on line 136. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
141
142
            $query=$this->schema->getConnection()->setQueryBasic($alterSytanx);
143
144
            return [
145
                'syntax'=>$this->syntax,
146
                'type'=>'alter',
147
                'result'=>$query['result'],
148
                'message'=>$query['message'],
149
            ];
150
151
152
        }
153
154
    }
155
156
    /**
157
     * add Indexes
158
     *
159
     * @param $alterType
160
     */
161
    private function addUnique($alterType)
162
    {
163
        if(isset($this->syntax[0])){
164
165
            $unique = $this->syntax[0];
166
167
            foreach($unique as $name=>$item){
168
                $unique_name = $name;
169
                $uniques = implode(',',$item);
170
            }
171
            $alterSytanx = 'ALTER TABLE '.$this->table.' ADD CONSTRAINT '.$unique_name.' UNIQUE ('.$uniques.')';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $unique_name seems to be defined by a foreach iteration on line 167. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
Comprehensibility Best Practice introduced by
The variable $uniques seems to be defined by a foreach iteration on line 167. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
172
173
            $query=$this->schema->getConnection()->setQueryBasic($alterSytanx);
174
175
            return [
176
                'syntax'=>$this->syntax,
177
                'type'=>'alter',
178
                'result'=>$query['result'],
179
                'message'=>$query['message'],
180
            ];
181
182
183
        }
184
185
    }
186
187
    /**
188
     * drop column
189
     *
190
     * @param $alterType
191
     */
192
    private function dropColumn($alterType)
193
    {
194
        if(isset($this->syntax[0])){
195
196
            $column = rtrim($this->syntax[0]);
197
198
            $alterSytanx = 'alter table '.$this->table.' drop column '.$column;
199
200
            $query=$this->schema->getConnection()->setQueryBasic($alterSytanx);
201
202
            return [
203
                'syntax'=>$this->syntax,
204
                'type'=>'alter',
205
                'result'=>$query['result'],
206
                'message'=>$query['message'],
207
            ];
208
209
210
        }
211
212
    }
213
214
    /**
215
     * drop column
216
     *
217
     * @param $alterType
218
     */
219
    private function dropKey($alterType)
220
    {
221
        if(isset($this->syntax[0])){
222
223
            $dropKey = $this->syntax[0];
224
225
            foreach ($dropKey as $dropType=>$dropName){
226
227
                $alterSytanx = 'alter table '.$this->table.' drop '.$dropType.' '.$dropName;
228
229
                $query=$this->schema->getConnection()->setQueryBasic($alterSytanx);
230
231
                return [
232
                    'syntax'=>$this->syntax,
233
                    'type'=>'alter',
234
                    'result'=>$query['result'],
235
                    'message'=>$query['message'],
236
                ];
237
            }
238
239
            
240
241
242
        }
243
244
    }
245
246
    /**
247
     * @return array
248
     */
249
    public function syntaxCreate()
250
    {
251
        $this->getWizardObjects($this->object);
252
        
253
        $existTables = $this->schema->getConnection()->showTables();
254
255
        $this->getCreateTableSyntax();
256
257
        $this->getDefaultSyntaxGroup();
258
259
        $this->syntax[]=')';
260
261
        //get table collation
262
        if(isset($this->data['tableCollation']['table'])){
263
            $this->syntax[]=' DEFAULT CHARACTER SET '.$this->data['tableCollation']['table'];
264
        }
265
        else{
266
            $this->syntax[]=' DEFAULT CHARACTER SET utf8';
267
        }
268
269
        //get engine
270
        if($this->data['engine']!==null)
271
        {
272
            $this->syntax[]=' ENGINE='.$this->data['engine'].' ';
273
        }
274
        else{
275
            $this->syntax[]=' ENGINE=InnoDB ';
276
        }
277
278
        $syntax = implode("",$this->syntax);
279
280
        if(in_array($this->table,$existTables)){
281
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
282
        }
283
        else{
284
            $query=$this->schema->getConnection()->setQueryBasic($syntax);
285
286
            return [
287
                'syntax'=>$syntax,
288
                'type'=>'create',
289
                'result'=>$query['result'],
290
                'message'=>$query['message'],
291
            ];
292
        }
293
        
294
    }
295
296
    /**
297
     * @param null $group
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $group is correct as it would always require null to be passed?
Loading history...
298
     */
299
    private function getDefaultSyntaxGroup($group=null)
300
    {
301
        $this->syntax[] = implode(",",$this->getCreateDefaultList());
302
        
303
        //get unique values
304
        if(isset($this->data['uniqueValueList']) && count($this->data['uniqueValueList'])){
305
            
306
            if($group=='create'){
307
                $this->syntax[]=','.implode(',',$this->data['uniqueValueList']);
308
            }
309
            else{
310
                $this->alterExtras[]='ADD '.implode(',',$this->data['uniqueValueList']);
311
            }
312
            
313
        }
314
315
        //get index values
316
        if(isset($this->data['indexValueList']) && count($this->data['indexValueList'])){
317
318
            if($group=='create'){
319
                $this->syntax[]=','.implode(',',$this->data['indexValueList']);
320
            }
321
            else{
322
                $this->alterExtras[]='ADD '.implode(',',$this->data['indexValueList']); 
323
            }
324
            
325
        }
326
327
        //get index values for key
328
        if(count($this->getKeyList())){
329
            $this->syntax[]=','.implode(',',$this->getKeyList());
330
        }
331
332
        if(count($this->data['references'])){
333
            $this->syntax[]=$this->getReferenceSyntax($this->data['references']);
334
        }
335
336
        if(isset($this->syntax[0]) && $this->syntax[0]=='' && $group=='addIndex'){
337
            $this->syntax[0] = $this->data['index'];
338
        }
339
340
        if(isset($this->syntax[0]) && $this->syntax[0]=='' && $group=='addUnique'){
341
            $this->syntax[0] = $this->data['unique'];
342
        }
343
344
        if(isset($this->syntax[0]) && $this->syntax[0]=='' && $group=='dropKey'){
345
            $this->syntax[0] = $this->data['key'];
346
        }
347
    }
348
349
350
    /**
351
     * @return mixed|void
352
     */
353
    public function syntaxAlter()
354
    {
355
        $this->getWizardObjects($this->object);
356
357
        $alterType = $this->object->getAlterType();
358
359
        $group = $alterType['group'];
360
361
        $this->getDefaultSyntaxGroup($group);
362
        
363
        return $this->{$group}($alterType);
364
365
    }
366
    
367
}
368
369