| Total Complexity | 41 |
| Total Lines | 323 |
| 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) |
||
| 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 | * @return array |
||
| 216 | */ |
||
| 217 | public function syntaxCreate() |
||
| 218 | { |
||
| 219 | $this->getWizardObjects($this->object); |
||
| 220 | |||
| 221 | $existTables = $this->schema->getConnection()->showTables(); |
||
| 222 | |||
| 223 | $this->getCreateTableSyntax(); |
||
| 224 | |||
| 225 | $this->getDefaultSyntaxGroup(); |
||
| 226 | |||
| 227 | $this->syntax[]=')'; |
||
| 228 | |||
| 229 | //get table collation |
||
| 230 | if(isset($this->data['tableCollation']['table'])){ |
||
| 231 | $this->syntax[]=' DEFAULT CHARACTER SET '.$this->data['tableCollation']['table']; |
||
| 232 | } |
||
| 233 | else{ |
||
| 234 | $this->syntax[]=' DEFAULT CHARACTER SET utf8'; |
||
| 235 | } |
||
| 236 | |||
| 237 | //get engine |
||
| 238 | if($this->data['engine']!==null) |
||
| 239 | { |
||
| 240 | $this->syntax[]=' ENGINE='.$this->data['engine'].' '; |
||
| 241 | } |
||
| 242 | else{ |
||
| 243 | $this->syntax[]=' ENGINE=InnoDB '; |
||
| 244 | } |
||
| 245 | |||
| 246 | $syntax = implode("",$this->syntax); |
||
| 247 | |||
| 248 | if(in_array($this->table,$existTables)){ |
||
| 249 | return false; |
||
| 250 | } |
||
| 251 | else{ |
||
| 252 | $query=$this->schema->getConnection()->setQueryBasic($syntax); |
||
| 253 | |||
| 254 | return [ |
||
| 255 | 'syntax'=>$syntax, |
||
| 256 | 'type'=>'create', |
||
| 257 | 'result'=>$query['result'], |
||
| 258 | 'message'=>$query['message'], |
||
| 259 | ]; |
||
| 260 | } |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param null $group |
||
| 266 | */ |
||
| 267 | private function getDefaultSyntaxGroup($group=null) |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @return mixed|void |
||
| 316 | */ |
||
| 317 | public function syntaxAlter() |
||
| 328 | |||
| 329 | } |
||
| 330 | |||
| 333 |