| Conditions | 4 |
| Paths | 8 |
| Total Lines | 261 |
| Code Lines | 189 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 204 | private static function initSchema(Connection $connection): void |
||
| 205 | { |
||
| 206 | $fromSchema = $connection->getSchemaManager()->createSchema(); |
||
| 207 | $toSchema = clone $fromSchema; |
||
| 208 | |||
| 209 | $db = new FluidSchema($toSchema); |
||
| 210 | |||
| 211 | $db->table('country') |
||
| 212 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 213 | ->column('label')->string(255); |
||
| 214 | |||
| 215 | $db->table('person') |
||
| 216 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 217 | ->column('name')->string(255); |
||
| 218 | |||
| 219 | if ($connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
| 220 | $toSchema->getTable('person') |
||
| 221 | ->addColumn( |
||
| 222 | 'created_at', |
||
| 223 | 'datetime', |
||
| 224 | ['columnDefinition' => 'TIMESTAMP(0) DEFAULT SYSDATE NOT NULL'] |
||
| 225 | ); |
||
| 226 | } else { |
||
| 227 | $toSchema->getTable('person') |
||
| 228 | ->addColumn( |
||
| 229 | 'created_at', |
||
| 230 | 'datetime', |
||
| 231 | ['columnDefinition' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'] |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | |||
| 235 | $db->table('person') |
||
| 236 | ->column('modified_at')->datetime()->null() |
||
| 237 | ->column('order')->integer()->null(); |
||
| 238 | |||
| 239 | |||
| 240 | $db->table('contact') |
||
| 241 | ->extends('person') |
||
| 242 | ->column('email')->string(255) |
||
| 243 | ->column('manager_id')->references('contact')->null(); |
||
| 244 | |||
| 245 | $db->table('users') |
||
| 246 | ->extends('contact') |
||
| 247 | ->column('login')->string(255) |
||
| 248 | ->column('password')->string(255)->null() |
||
| 249 | ->column('status')->string(10)->null()->default(null) |
||
| 250 | ->column('country_id')->references('country'); |
||
| 251 | |||
| 252 | $db->table('rights') |
||
| 253 | ->column('label')->string(255)->primaryKey()->comment('Non autoincrementable primary key'); |
||
| 254 | |||
| 255 | $db->table('roles') |
||
| 256 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 257 | ->column('name')->string(255) |
||
| 258 | ->column('created_at')->date()->null() |
||
| 259 | ->column('status')->boolean()->null()->default(1); |
||
| 260 | |||
| 261 | $db->table('roles_rights') |
||
| 262 | ->column('role_id')->references('roles') |
||
| 263 | ->column('right_label')->references('rights')->then() |
||
| 264 | ->primaryKey(['role_id', 'right_label']); |
||
| 265 | |||
| 266 | $db->table('users_roles') |
||
| 267 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 268 | ->column('user_id')->references('users') |
||
| 269 | ->column('role_id')->references('roles'); |
||
| 270 | |||
| 271 | $db->table('all_nullable') |
||
| 272 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 273 | ->column('label')->string(255)->null() |
||
| 274 | ->column('country_id')->references('country')->null(); |
||
| 275 | |||
| 276 | $db->table('animal') |
||
| 277 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 278 | ->column('name')->string(45)->index() |
||
| 279 | ->column('order')->integer()->null(); |
||
| 280 | |||
| 281 | $db->table('dog') |
||
| 282 | ->extends('animal') |
||
| 283 | ->column('race')->string(45)->null(); |
||
| 284 | |||
| 285 | $db->table('cat') |
||
| 286 | ->extends('animal') |
||
| 287 | ->column('cuteness_level')->integer()->null(); |
||
| 288 | |||
| 289 | $db->table('boats') |
||
| 290 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 291 | ->column('name')->string(255) |
||
| 292 | ->column('anchorage_country')->references('country')->notNull(); |
||
| 293 | |||
| 294 | $db->table('sailed_countries') |
||
| 295 | ->column('boat_id')->references('boats') |
||
| 296 | ->column('country_id')->references('country') |
||
| 297 | ->then()->primaryKey(['boat_id', 'country_id']); |
||
| 298 | |||
| 299 | $db->table('category') |
||
| 300 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
| 301 | ->column('label')->string(255) |
||
| 302 | ->column('parent_id')->references('category')->null(); |
||
| 303 | |||
| 304 | $db->table('article') |
||
| 305 | ->column('id')->string(36)->primaryKey()->comment('@UUID') |
||
| 306 | ->column('content')->string(255); |
||
| 307 | |||
| 308 | $db->table('article2') |
||
| 309 | ->column('id')->string(36)->primaryKey()->comment('@UUID v4') |
||
| 310 | ->column('content')->string(255); |
||
| 311 | |||
| 312 | $toSchema->getTable('users') |
||
| 313 | ->addUniqueIndex(['login'], 'users_login_idx') |
||
| 314 | ->addIndex(['status', 'country_id'], 'users_status_country_idx'); |
||
| 315 | |||
| 316 | // We create the same index twice |
||
| 317 | // except for Oracle that won't let us create twice the same index. |
||
| 318 | if (!$connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
| 319 | $toSchema->getTable('users') |
||
| 320 | ->addUniqueIndex(['login'], 'users_login_idx_2'); |
||
| 321 | } |
||
| 322 | |||
| 323 | |||
| 324 | $sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform()); |
||
| 325 | |||
| 326 | foreach ($sqlStmts as $sqlStmt) { |
||
| 327 | $connection->exec($sqlStmt); |
||
| 328 | } |
||
| 329 | |||
| 330 | $connection->insert('country', [ |
||
| 331 | 'label' => 'France', |
||
| 332 | ]); |
||
| 333 | $connection->insert('country', [ |
||
| 334 | 'label' => 'UK', |
||
| 335 | ]); |
||
| 336 | $connection->insert('country', [ |
||
| 337 | 'label' => 'Jamaica', |
||
| 338 | ]); |
||
| 339 | |||
| 340 | $connection->insert('person', [ |
||
| 341 | 'name' => 'John Smith', |
||
| 342 | 'created_at' => '2015-10-24 11:57:13', |
||
| 343 | ]); |
||
| 344 | $connection->insert('person', [ |
||
| 345 | 'name' => 'Jean Dupont', |
||
| 346 | 'created_at' => '2015-10-24 11:57:13', |
||
| 347 | ]); |
||
| 348 | $connection->insert('person', [ |
||
| 349 | 'name' => 'Robert Marley', |
||
| 350 | 'created_at' => '2015-10-24 11:57:13', |
||
| 351 | ]); |
||
| 352 | $connection->insert('person', [ |
||
| 353 | 'name' => 'Bill Shakespeare', |
||
| 354 | 'created_at' => '2015-10-24 11:57:13', |
||
| 355 | ]); |
||
| 356 | |||
| 357 | $connection->insert('contact', [ |
||
| 358 | 'id' => 1, |
||
| 359 | 'email' => '[email protected]', |
||
| 360 | 'manager_id' => null, |
||
| 361 | ]); |
||
| 362 | $connection->insert('contact', [ |
||
| 363 | 'id' => 2, |
||
| 364 | 'email' => '[email protected]', |
||
| 365 | 'manager_id' => null, |
||
| 366 | ]); |
||
| 367 | $connection->insert('contact', [ |
||
| 368 | 'id' => 3, |
||
| 369 | 'email' => '[email protected]', |
||
| 370 | 'manager_id' => null, |
||
| 371 | ]); |
||
| 372 | $connection->insert('contact', [ |
||
| 373 | 'id' => 4, |
||
| 374 | 'email' => '[email protected]', |
||
| 375 | 'manager_id' => 1, |
||
| 376 | ]); |
||
| 377 | |||
| 378 | $connection->insert('rights', [ |
||
| 379 | 'label' => 'CAN_SING', |
||
| 380 | ]); |
||
| 381 | $connection->insert('rights', [ |
||
| 382 | 'label' => 'CAN_WRITE', |
||
| 383 | ]); |
||
| 384 | |||
| 385 | $connection->insert('roles', [ |
||
| 386 | 'name' => 'Admins', |
||
| 387 | 'created_at' => '2015-10-24' |
||
| 388 | ]); |
||
| 389 | $connection->insert('roles', [ |
||
| 390 | 'name' => 'Writers', |
||
| 391 | 'created_at' => '2015-10-24' |
||
| 392 | ]); |
||
| 393 | $connection->insert('roles', [ |
||
| 394 | 'name' => 'Singers', |
||
| 395 | 'created_at' => '2015-10-24' |
||
| 396 | ]); |
||
| 397 | |||
| 398 | $connection->insert('roles_rights', [ |
||
| 399 | 'role_id' => 1, |
||
| 400 | 'right_label' => 'CAN_SING' |
||
| 401 | ]); |
||
| 402 | $connection->insert('roles_rights', [ |
||
| 403 | 'role_id' => 3, |
||
| 404 | 'right_label' => 'CAN_SING' |
||
| 405 | ]); |
||
| 406 | $connection->insert('roles_rights', [ |
||
| 407 | 'role_id' => 1, |
||
| 408 | 'right_label' => 'CAN_WRITE' |
||
| 409 | ]); |
||
| 410 | $connection->insert('roles_rights', [ |
||
| 411 | 'role_id' => 2, |
||
| 412 | 'right_label' => 'CAN_WRITE' |
||
| 413 | ]); |
||
| 414 | |||
| 415 | $connection->insert('users', [ |
||
| 416 | 'id' => 1, |
||
| 417 | 'login' => 'john.smith', |
||
| 418 | 'password' => null, |
||
| 419 | 'status' => 'on', |
||
| 420 | 'country_id' => 2 |
||
| 421 | ]); |
||
| 422 | $connection->insert('users', [ |
||
| 423 | 'id' => 2, |
||
| 424 | 'login' => 'jean.dupont', |
||
| 425 | 'password' => null, |
||
| 426 | 'status' => 'on', |
||
| 427 | 'country_id' => 1 |
||
| 428 | ]); |
||
| 429 | $connection->insert('users', [ |
||
| 430 | 'id' => 3, |
||
| 431 | 'login' => 'robert.marley', |
||
| 432 | 'password' => null, |
||
| 433 | 'status' => 'off', |
||
| 434 | 'country_id' => 3 |
||
| 435 | ]); |
||
| 436 | $connection->insert('users', [ |
||
| 437 | 'id' => 4, |
||
| 438 | 'login' => 'bill.shakespeare', |
||
| 439 | 'password' => null, |
||
| 440 | 'status' => 'off', |
||
| 441 | 'country_id' => 2 |
||
| 442 | ]); |
||
| 443 | |||
| 444 | $connection->insert('users_roles', [ |
||
| 445 | 'user_id' => 1, |
||
| 446 | 'role_id' => 1, |
||
| 447 | ]); |
||
| 448 | $connection->insert('users_roles', [ |
||
| 449 | 'user_id' => 2, |
||
| 450 | 'role_id' => 1, |
||
| 451 | ]); |
||
| 452 | $connection->insert('users_roles', [ |
||
| 453 | 'user_id' => 3, |
||
| 454 | 'role_id' => 3, |
||
| 455 | ]); |
||
| 456 | $connection->insert('users_roles', [ |
||
| 457 | 'user_id' => 4, |
||
| 458 | 'role_id' => 2, |
||
| 459 | ]); |
||
| 460 | $connection->insert('users_roles', [ |
||
| 461 | 'user_id' => 3, |
||
| 462 | 'role_id' => 2, |
||
| 463 | ]); |
||
| 464 | } |
||
| 465 | } |
||
| 466 |