| Conditions | 23 |
| Paths | 38 |
| Total Lines | 112 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 141 | private function sqlColumns(): string |
||
| 142 | { |
||
| 143 | $result = array(); |
||
| 144 | foreach ($this->columns as $col){ |
||
| 145 | |||
| 146 | // Parse arguments. First item is NAME and second is TYPE |
||
| 147 | $sqlName = $this->escape($col[0]); |
||
| 148 | $sqlType = $col[1]; //TODO check type |
||
| 149 | |||
| 150 | // following arguments |
||
| 151 | $args = array_slice($col, 2); |
||
| 152 | $currentIndex = 0; |
||
| 153 | $defaultValueIndex = -1; |
||
| 154 | |||
| 155 | $sqlConstraintUnique = ''; // UNIQUE ?, not by default |
||
| 156 | $sqlConstraintNullable = 'NULL'; // allow null value by default |
||
| 157 | $isPk = false; // PRIMARY KEY? |
||
| 158 | $sqlDefault = ''; // DEFAULT VALUE? |
||
| 159 | |||
| 160 | foreach ($args as $arg){ |
||
| 161 | |||
| 162 | // last index was DEFAULT, so the current argument |
||
| 163 | // is the value for default contsaint |
||
| 164 | if ($currentIndex === $defaultValueIndex){ |
||
| 165 | |||
| 166 | // string |
||
| 167 | if (is_string($arg)){ |
||
| 168 | |||
| 169 | // escape everything except constants |
||
| 170 | if (in_array(strtoupper($arg), $this->supportedDefaults)){ |
||
| 171 | $sqlDefault = 'DEFAULT ' . $arg; |
||
| 172 | } else { |
||
| 173 | $sqlDefault = 'DEFAULT ' . $this->driver->escapeValue($arg); |
||
| 174 | } |
||
| 175 | |||
| 176 | // int/float are not escaped |
||
| 177 | } elseif (is_int($arg) || is_float($arg)){ |
||
| 178 | $sqlDefault = 'DEFAULT ' . $arg; |
||
| 179 | |||
| 180 | // bool Type |
||
| 181 | } elseif (is_bool($arg)){ |
||
| 182 | $sqlDefault = 'DEFAULT ' . ($arg ? 'TRUE' : 'FALSE'); |
||
| 183 | } |
||
| 184 | |||
| 185 | |||
| 186 | } else { |
||
| 187 | switch (strtoupper($arg)){ |
||
| 188 | |||
| 189 | // NULL /NOT NULL |
||
| 190 | case 'NULL': |
||
| 191 | $sqlConstraintNullable = 'NULL'; |
||
| 192 | break; |
||
| 193 | |||
| 194 | |||
| 195 | case 'NOT NULL': |
||
| 196 | $sqlConstraintNullable = 'NOT NULL'; |
||
| 197 | break; |
||
| 198 | |||
| 199 | // UNIQUE |
||
| 200 | case 'UNIQUE': |
||
| 201 | $sqlConstraintUnique = 'UNIQUE'; |
||
| 202 | break; |
||
| 203 | |||
| 204 | // AUTO INCREMENT |
||
| 205 | case 'AUTO INCREMENT': |
||
| 206 | case 'AUTO_INCREMENT': |
||
| 207 | case 'AI': |
||
| 208 | $sqlType = $this->driver->sqlColumnAutoIncrement($sqlType); |
||
| 209 | break; |
||
| 210 | |||
| 211 | // PK |
||
| 212 | case 'PRIMARY KEY': |
||
| 213 | case 'PRIMARY_KEY': |
||
| 214 | case 'PK': |
||
| 215 | $isPk = true; |
||
| 216 | break; |
||
| 217 | |||
| 218 | // DEFAULT |
||
| 219 | case 'DEFAULT': |
||
| 220 | // define next index as the DefaultValue index |
||
| 221 | $defaultValueIndex = $currentIndex +1; |
||
| 222 | break; |
||
| 223 | |||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | // update current index |
||
| 228 | $currentIndex ++; |
||
| 229 | } |
||
| 230 | |||
| 231 | // set optional params |
||
| 232 | // PK ?, UNIQUE ?, NULL? (PK cannot be null), DEFAULT? |
||
| 233 | // AI is handle with sqltype |
||
| 234 | $result[] = trim(implode(' ', [$sqlName, |
||
| 235 | $sqlType, |
||
| 236 | $isPk ? 'NOT NULL' : $sqlConstraintNullable, |
||
| 237 | $isPk ? 'PRIMARY KEY' : '', |
||
| 238 | $sqlConstraintUnique, |
||
| 239 | $sqlDefault])); |
||
| 240 | } |
||
| 241 | |||
| 242 | // FK CONSTRANT |
||
| 243 | foreach ($this->foreignKeys as $foreignKey){ |
||
| 244 | $result[] = trim(sprintf('CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)', |
||
| 245 | $foreignKey['name'], |
||
| 246 | $this->driver->escapeIdentifier($foreignKey['src_column']), |
||
| 247 | $this->driver->escapeIdentifier($foreignKey['ref_table']), |
||
| 248 | $this->driver->escapeIdentifier($foreignKey['ref_column']) |
||
| 249 | )); |
||
| 250 | } |
||
| 251 | |||
| 252 | return implode(', ', $result); |
||
| 253 | } |
||
| 275 | } |