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 |
||
| 7 | class CreateAdminTables extends Migration |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * {@inheritdoc} |
||
| 11 | */ |
||
| 12 | public function getConnection() |
||
| 13 | { |
||
| 14 | return config('admin.database.connection') ?: config('database.default'); |
||
| 15 | } |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Run the migrations. |
||
| 19 | * |
||
| 20 | * @return void |
||
| 21 | */ |
||
| 22 | public function up() |
||
| 23 | { |
||
| 24 | Schema::create(config('admin.database.users_table'), function (Blueprint $table) { |
||
| 25 | $table->increments('id'); |
||
| 26 | $table->string('username', 190)->unique(); |
||
| 27 | $table->string('password', 60); |
||
| 28 | $table->string('name'); |
||
| 29 | $table->string('avatar')->nullable(); |
||
| 30 | $table->string('remember_token', 100)->nullable(); |
||
| 31 | $table->timestamps(); |
||
| 32 | }); |
||
| 33 | |||
| 34 | Schema::create(config('admin.database.roles_table'), function (Blueprint $table) { |
||
| 35 | $table->increments('id'); |
||
| 36 | $table->string('name', 50)->unique(); |
||
| 37 | $table->string('slug', 50)->unique(); |
||
| 38 | $table->timestamps(); |
||
| 39 | }); |
||
| 40 | |||
| 41 | Schema::create(config('admin.database.permissions_table'), function (Blueprint $table) { |
||
| 42 | $table->increments('id'); |
||
| 43 | $table->string('name', 50)->unique(); |
||
| 44 | $table->string('slug', 50)->unique(); |
||
| 45 | $table->string('http_method')->nullable(); |
||
| 46 | $table->text('http_path')->nullable(); |
||
| 47 | $table->timestamps(); |
||
| 48 | }); |
||
| 49 | |||
| 50 | Schema::create(config('admin.database.menu_table'), function (Blueprint $table) { |
||
| 51 | $table->increments('id'); |
||
| 52 | $table->integer('parent_id')->default(0); |
||
| 53 | $table->integer('order')->default(0); |
||
| 54 | $table->string('title', 50); |
||
| 55 | $table->string('icon', 50); |
||
| 56 | $table->string('uri', 50)->nullable(); |
||
| 57 | $table->string('permission')->nullable(); |
||
| 58 | |||
| 59 | $table->timestamps(); |
||
| 60 | }); |
||
| 61 | |||
| 62 | Schema::create(config('admin.database.role_users_table'), function (Blueprint $table) { |
||
| 63 | $table->integer('role_id'); |
||
| 64 | $table->integer('user_id'); |
||
| 65 | $table->index(['role_id', 'user_id']); |
||
| 66 | $table->timestamps(); |
||
| 67 | }); |
||
| 68 | |||
| 69 | Schema::create(config('admin.database.role_permissions_table'), function (Blueprint $table) { |
||
| 70 | $table->integer('role_id'); |
||
| 71 | $table->integer('permission_id'); |
||
| 72 | $table->index(['role_id', 'permission_id']); |
||
| 73 | $table->timestamps(); |
||
| 74 | }); |
||
| 75 | |||
| 76 | Schema::create(config('admin.database.user_permissions_table'), function (Blueprint $table) { |
||
| 77 | $table->integer('user_id'); |
||
| 78 | $table->integer('permission_id'); |
||
| 79 | $table->index(['user_id', 'permission_id']); |
||
| 80 | $table->timestamps(); |
||
| 81 | }); |
||
| 82 | |||
| 83 | Schema::create(config('admin.database.role_menu_table'), function (Blueprint $table) { |
||
| 84 | $table->integer('role_id'); |
||
| 85 | $table->integer('menu_id'); |
||
| 86 | $table->index(['role_id', 'menu_id']); |
||
| 87 | $table->timestamps(); |
||
| 88 | }); |
||
| 89 | |||
| 90 | Schema::create(config('admin.database.operation_log_table'), function (Blueprint $table) { |
||
| 91 | $table->increments('id'); |
||
| 92 | $table->integer('user_id'); |
||
| 93 | $table->string('path'); |
||
| 94 | $table->string('method', 10); |
||
| 95 | $table->string('ip'); |
||
| 96 | $table->text('input'); |
||
| 97 | $table->index('user_id'); |
||
| 98 | $table->timestamps(); |
||
| 99 | }); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Reverse the migrations. |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | public function down() |
||
| 108 | { |
||
| 109 | Schema::dropIfExists(config('admin.database.users_table')); |
||
| 110 | Schema::dropIfExists(config('admin.database.roles_table')); |
||
| 111 | Schema::dropIfExists(config('admin.database.permissions_table')); |
||
| 112 | Schema::dropIfExists(config('admin.database.menu_table')); |
||
| 113 | Schema::dropIfExists(config('admin.database.user_permissions_table')); |
||
| 114 | Schema::dropIfExists(config('admin.database.role_users_table')); |
||
| 115 | Schema::dropIfExists(config('admin.database.role_permissions_table')); |
||
| 116 | Schema::dropIfExists(config('admin.database.role_menu_table')); |
||
| 117 | Schema::dropIfExists(config('admin.database.operation_log_table')); |
||
| 118 | } |
||
| 119 | } |
||
| 120 |