Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 6 | class CreateTestTables extends Migration |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * Run the migrations. |
||
| 10 | * |
||
| 11 | * @return void |
||
| 12 | */ |
||
| 13 | public function up() |
||
| 14 | { |
||
| 15 | Schema::create('test_images', function (Blueprint $table) { |
||
| 16 | $table->increments('id'); |
||
| 17 | $table->string('image1'); |
||
| 18 | $table->string('image2'); |
||
| 19 | $table->string('image3'); |
||
| 20 | $table->string('image4'); |
||
| 21 | $table->string('image5'); |
||
| 22 | $table->string('image6'); |
||
| 23 | $table->timestamps(); |
||
| 24 | }); |
||
| 25 | |||
| 26 | Schema::create('test_multiple_images', function (Blueprint $table) { |
||
| 27 | $table->increments('id'); |
||
| 28 | $table->text('pictures'); |
||
| 29 | $table->timestamps(); |
||
| 30 | }); |
||
| 31 | |||
| 32 | Schema::create('test_files', function (Blueprint $table) { |
||
| 33 | $table->increments('id'); |
||
| 34 | $table->string('file1'); |
||
| 35 | $table->string('file2'); |
||
| 36 | $table->string('file3'); |
||
| 37 | $table->string('file4'); |
||
| 38 | $table->string('file5'); |
||
| 39 | $table->string('file6'); |
||
| 40 | $table->timestamps(); |
||
| 41 | }); |
||
| 42 | |||
| 43 | Schema::create('test_users', function (Blueprint $table) { |
||
| 44 | $table->increments('id'); |
||
| 45 | $table->string('username'); |
||
| 46 | $table->string('email'); |
||
| 47 | $table->string('mobile')->nullable(); |
||
| 48 | $table->string('avatar')->nullable(); |
||
| 49 | $table->string('password'); |
||
| 50 | $table->timestamps(); |
||
| 51 | }); |
||
| 52 | |||
| 53 | Schema::create('test_user_profiles', function (Blueprint $table) { |
||
| 54 | $table->increments('id'); |
||
| 55 | $table->string('user_id'); |
||
| 56 | $table->string('first_name')->nullable(); |
||
| 57 | $table->string('last_name')->nullable(); |
||
| 58 | $table->string('postcode')->nullable(); |
||
| 59 | $table->string('address')->nullable(); |
||
| 60 | $table->string('latitude')->nullable(); |
||
| 61 | $table->string('longitude')->nullable(); |
||
| 62 | $table->string('color')->nullable(); |
||
| 63 | $table->timestamp('start_at')->nullable(); |
||
| 64 | $table->timestamp('end_at')->nullable(); |
||
| 65 | |||
| 66 | $table->timestamps(); |
||
| 67 | }); |
||
| 68 | |||
| 69 | Schema::create('test_tags', function (Blueprint $table) { |
||
| 70 | $table->increments('id'); |
||
| 71 | $table->string('name'); |
||
| 72 | $table->timestamps(); |
||
| 73 | }); |
||
| 74 | |||
| 75 | Schema::create('test_user_tags', function (Blueprint $table) { |
||
| 76 | $table->integer('user_id'); |
||
| 77 | $table->integer('tag_id'); |
||
| 78 | $table->index(['user_id', 'tag_id']); |
||
| 79 | $table->timestamps(); |
||
| 80 | }); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Reverse the migrations. |
||
| 85 | * |
||
| 86 | * @return void |
||
| 87 | */ |
||
| 88 | public function down() |
||
| 89 | { |
||
| 90 | Schema::dropIfExists('test_images'); |
||
| 91 | Schema::dropIfExists('test_multiple_images'); |
||
| 92 | Schema::dropIfExists('test_files'); |
||
| 93 | Schema::dropIfExists('test_users'); |
||
| 94 | Schema::dropIfExists('test_user_profiles'); |
||
| 95 | Schema::dropIfExists('test_tags'); |
||
| 96 | Schema::dropIfExists('test_user_tags'); |
||
| 97 | } |
||
| 98 | } |
||
| 99 |