Conditions | 4 |
Paths | 8 |
Total Lines | 293 |
Code Lines | 214 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
227 | private static function initSchema(Connection $connection): void |
||
228 | { |
||
229 | $fromSchema = $connection->getSchemaManager()->createSchema(); |
||
230 | $toSchema = clone $fromSchema; |
||
231 | |||
232 | $db = new FluidSchema($toSchema, new \TheCodingMachine\FluidSchema\DefaultNamingStrategy($connection->getDatabasePlatform())); |
||
233 | |||
234 | $db->table('country') |
||
235 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
236 | ->column('label')->string(255)->unique(); |
||
237 | |||
238 | $db->table('person') |
||
239 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
240 | ->column('name')->string(255); |
||
241 | |||
242 | if ($connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
243 | $toSchema->getTable($connection->quoteIdentifier('person')) |
||
244 | ->addColumn( |
||
245 | $connection->quoteIdentifier('created_at'), |
||
246 | 'datetime', |
||
247 | ['columnDefinition' => 'TIMESTAMP(0) DEFAULT SYSDATE NOT NULL'] |
||
248 | ); |
||
249 | } else { |
||
250 | $toSchema->getTable('person') |
||
251 | ->addColumn( |
||
252 | $connection->quoteIdentifier('created_at'), |
||
253 | 'datetime', |
||
254 | ['columnDefinition' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'] |
||
255 | ); |
||
256 | } |
||
257 | |||
258 | $db->table('person') |
||
259 | ->column('modified_at')->datetime()->null() |
||
260 | ->column('order')->integer()->null(); |
||
261 | |||
262 | |||
263 | $db->table('contact') |
||
264 | ->extends('person') |
||
265 | ->column('email')->string(255) |
||
266 | ->column('manager_id')->references('contact')->null(); |
||
267 | |||
268 | $db->table('users') |
||
269 | ->extends('contact') |
||
270 | ->column('login')->string(255) |
||
271 | ->column('password')->string(255)->null() |
||
272 | ->column('status')->string(10)->null()->default(null) |
||
273 | ->column('country_id')->references('country'); |
||
274 | |||
275 | $db->table('rights') |
||
276 | ->column('label')->string(255)->primaryKey()->comment('Non autoincrementable primary key'); |
||
277 | |||
278 | $db->table('roles') |
||
279 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
280 | ->column('name')->string(255) |
||
281 | ->column('created_at')->date()->null() |
||
282 | ->column('status')->boolean()->null()->default(1); |
||
283 | |||
284 | $db->table('roles_rights') |
||
285 | ->column('role_id')->references('roles') |
||
286 | ->column('right_label')->references('rights')->then() |
||
287 | ->primaryKey(['role_id', 'right_label']); |
||
288 | |||
289 | $db->table('users_roles') |
||
290 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
291 | ->column('user_id')->references('users') |
||
292 | ->column('role_id')->references('roles'); |
||
293 | |||
294 | $db->table('all_nullable') |
||
295 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
296 | ->column('label')->string(255)->null() |
||
297 | ->column('country_id')->references('country')->null(); |
||
298 | |||
299 | $db->table('animal') |
||
300 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
301 | ->column('name')->string(45)->index() |
||
302 | ->column('UPPERCASE_COLUMN')->string(45)->null() |
||
303 | ->column('order')->integer()->null(); |
||
304 | |||
305 | $db->table('dog') |
||
306 | ->extends('animal') |
||
307 | ->column('race')->string(45)->null(); |
||
308 | |||
309 | $db->table('cat') |
||
310 | ->extends('animal') |
||
311 | ->column('cuteness_level')->integer()->null(); |
||
312 | |||
313 | $db->table('panda') |
||
314 | ->extends('animal') |
||
315 | ->column('weight')->float()->null(); |
||
316 | |||
317 | $db->table('boats') |
||
318 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
319 | ->column('name')->string(255) |
||
320 | ->column('anchorage_country')->references('country')->notNull()->then() |
||
321 | ->column('current_country')->references('country')->null()->then() |
||
322 | ->column('length')->decimal(10, 2)->null()->then() |
||
323 | ->unique(['anchorage_country', 'name']); |
||
324 | |||
325 | $db->table('sailed_countries') |
||
326 | ->column('boat_id')->references('boats') |
||
327 | ->column('country_id')->references('country') |
||
328 | ->then()->primaryKey(['boat_id', 'country_id']); |
||
329 | |||
330 | $db->table('category') |
||
331 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
332 | ->column('label')->string(255) |
||
333 | ->column('parent_id')->references('category')->null(); |
||
334 | |||
335 | $db->table('article') |
||
336 | ->column('id')->string(36)->primaryKey()->comment('@UUID') |
||
337 | ->column('content')->string(255) |
||
338 | ->column('author_id')->references('users')->null(); |
||
339 | |||
340 | $db->table('article2') |
||
341 | ->column('id')->string(36)->primaryKey()->comment('@UUID v4') |
||
342 | ->column('content')->string(255); |
||
343 | |||
344 | $db->table('files') |
||
345 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
346 | ->column('file')->blob() |
||
347 | ->column('md5')->string()->null()->comment("@ProtectedGetter\n@ProtectedSetter") |
||
348 | ->column('article_id')->references('article')->null()->comment("@ProtectedGetter\n@ProtectedSetter\n@ProtectedOneToMany"); |
||
349 | |||
350 | $toSchema->getTable('users') |
||
351 | ->addUniqueIndex([$connection->quoteIdentifier('login')], 'users_login_idx') |
||
352 | ->addIndex([$connection->quoteIdentifier('status'), $connection->quoteIdentifier('country_id')], 'users_status_country_idx'); |
||
353 | |||
354 | // We create the same index twice |
||
355 | // except for Oracle that won't let us create twice the same index. |
||
356 | if (!$connection->getDatabasePlatform() instanceof OraclePlatform) { |
||
357 | $toSchema->getTable('users') |
||
358 | ->addUniqueIndex([$connection->quoteIdentifier('login')], 'users_login_idx_2'); |
||
359 | } |
||
360 | |||
361 | // A table with a foreign key that references a non primary key. |
||
362 | $db->table('ref_no_prim_key') |
||
363 | ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') |
||
364 | ->column('from')->string(50) |
||
365 | ->column('to')->string(50)->unique(); |
||
366 | |||
367 | $toSchema->getTable($connection->quoteIdentifier('ref_no_prim_key'))->addForeignKeyConstraint($connection->quoteIdentifier('ref_no_prim_key'), [$connection->quoteIdentifier('from')], [$connection->quoteIdentifier('to')]); |
||
368 | |||
369 | // A table with multiple primary keys. |
||
370 | $db->table('states') |
||
371 | ->column('country_id')->references('country') |
||
372 | ->column('code')->string(3) |
||
373 | ->column('name')->string(50)->then() |
||
374 | ->primaryKey(['country_id', 'code']); |
||
375 | |||
376 | $sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform()); |
||
377 | |||
378 | foreach ($sqlStmts as $sqlStmt) { |
||
379 | $connection->exec($sqlStmt); |
||
380 | } |
||
381 | |||
382 | self::insert($connection, 'country', [ |
||
383 | 'label' => 'France', |
||
384 | ]); |
||
385 | self::insert($connection, 'country', [ |
||
386 | 'label' => 'UK', |
||
387 | ]); |
||
388 | self::insert($connection, 'country', [ |
||
389 | 'label' => 'Jamaica', |
||
390 | ]); |
||
391 | |||
392 | self::insert($connection, 'person', [ |
||
393 | 'name' => 'John Smith', |
||
394 | 'created_at' => '2015-10-24 11:57:13', |
||
395 | ]); |
||
396 | self::insert($connection, 'person', [ |
||
397 | 'name' => 'Jean Dupont', |
||
398 | 'created_at' => '2015-10-24 11:57:13', |
||
399 | ]); |
||
400 | self::insert($connection, 'person', [ |
||
401 | 'name' => 'Robert Marley', |
||
402 | 'created_at' => '2015-10-24 11:57:13', |
||
403 | ]); |
||
404 | self::insert($connection, 'person', [ |
||
405 | 'name' => 'Bill Shakespeare', |
||
406 | 'created_at' => '2015-10-24 11:57:13', |
||
407 | ]); |
||
408 | |||
409 | self::insert($connection, 'contact', [ |
||
410 | 'id' => 1, |
||
411 | 'email' => '[email protected]', |
||
412 | 'manager_id' => null, |
||
413 | ]); |
||
414 | self::insert($connection, 'contact', [ |
||
415 | 'id' => 2, |
||
416 | 'email' => '[email protected]', |
||
417 | 'manager_id' => null, |
||
418 | ]); |
||
419 | self::insert($connection, 'contact', [ |
||
420 | 'id' => 3, |
||
421 | 'email' => '[email protected]', |
||
422 | 'manager_id' => null, |
||
423 | ]); |
||
424 | self::insert($connection, 'contact', [ |
||
425 | 'id' => 4, |
||
426 | 'email' => '[email protected]', |
||
427 | 'manager_id' => 1, |
||
428 | ]); |
||
429 | |||
430 | self::insert($connection, 'rights', [ |
||
431 | 'label' => 'CAN_SING', |
||
432 | ]); |
||
433 | self::insert($connection, 'rights', [ |
||
434 | 'label' => 'CAN_WRITE', |
||
435 | ]); |
||
436 | |||
437 | self::insert($connection, 'roles', [ |
||
438 | 'name' => 'Admins', |
||
439 | 'created_at' => '2015-10-24' |
||
440 | ]); |
||
441 | self::insert($connection, 'roles', [ |
||
442 | 'name' => 'Writers', |
||
443 | 'created_at' => '2015-10-24' |
||
444 | ]); |
||
445 | self::insert($connection, 'roles', [ |
||
446 | 'name' => 'Singers', |
||
447 | 'created_at' => '2015-10-24' |
||
448 | ]); |
||
449 | |||
450 | self::insert($connection, 'roles_rights', [ |
||
451 | 'role_id' => 1, |
||
452 | 'right_label' => 'CAN_SING' |
||
453 | ]); |
||
454 | self::insert($connection, 'roles_rights', [ |
||
455 | 'role_id' => 3, |
||
456 | 'right_label' => 'CAN_SING' |
||
457 | ]); |
||
458 | self::insert($connection, 'roles_rights', [ |
||
459 | 'role_id' => 1, |
||
460 | 'right_label' => 'CAN_WRITE' |
||
461 | ]); |
||
462 | self::insert($connection, 'roles_rights', [ |
||
463 | 'role_id' => 2, |
||
464 | 'right_label' => 'CAN_WRITE' |
||
465 | ]); |
||
466 | |||
467 | self::insert($connection, 'users', [ |
||
468 | 'id' => 1, |
||
469 | 'login' => 'john.smith', |
||
470 | 'password' => null, |
||
471 | 'status' => 'on', |
||
472 | 'country_id' => 2 |
||
473 | ]); |
||
474 | self::insert($connection, 'users', [ |
||
475 | 'id' => 2, |
||
476 | 'login' => 'jean.dupont', |
||
477 | 'password' => null, |
||
478 | 'status' => 'on', |
||
479 | 'country_id' => 1 |
||
480 | ]); |
||
481 | self::insert($connection, 'users', [ |
||
482 | 'id' => 3, |
||
483 | 'login' => 'robert.marley', |
||
484 | 'password' => null, |
||
485 | 'status' => 'off', |
||
486 | 'country_id' => 3 |
||
487 | ]); |
||
488 | self::insert($connection, 'users', [ |
||
489 | 'id' => 4, |
||
490 | 'login' => 'bill.shakespeare', |
||
491 | 'password' => null, |
||
492 | 'status' => 'off', |
||
493 | 'country_id' => 2 |
||
494 | ]); |
||
495 | |||
496 | self::insert($connection, 'users_roles', [ |
||
497 | 'user_id' => 1, |
||
498 | 'role_id' => 1, |
||
499 | ]); |
||
500 | self::insert($connection, 'users_roles', [ |
||
501 | 'user_id' => 2, |
||
502 | 'role_id' => 1, |
||
503 | ]); |
||
504 | self::insert($connection, 'users_roles', [ |
||
505 | 'user_id' => 3, |
||
506 | 'role_id' => 3, |
||
507 | ]); |
||
508 | self::insert($connection, 'users_roles', [ |
||
509 | 'user_id' => 4, |
||
510 | 'role_id' => 2, |
||
511 | ]); |
||
512 | self::insert($connection, 'users_roles', [ |
||
513 | 'user_id' => 3, |
||
514 | 'role_id' => 2, |
||
515 | ]); |
||
516 | |||
517 | self::insert($connection, 'ref_no_prim_key', [ |
||
518 | 'from' => 'foo', |
||
519 | 'to' => 'foo', |
||
520 | ]); |
||
541 |