| Total Complexity | 47 |
| Total Lines | 359 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like QuerySyntax often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QuerySyntax, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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) |
||
| 64 | ]; |
||
| 65 | } |
||
| 66 | |||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * change column |
||
| 71 | * |
||
| 72 | * @param $alterType |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | private function change($alterType) |
||
| 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.')'; |
||
| 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.')'; |
||
| 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) |
||
| 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; |
||
| 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 |
||
| 298 | */ |
||
| 299 | private function getDefaultSyntaxGroup($group=null) |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | |||
| 350 | /** |
||
| 351 | * @return mixed|void |
||
| 352 | */ |
||
| 353 | public function syntaxAlter() |
||
| 369 |