CreatePermissionTables::up()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 64

Duplication

Lines 38
Ratio 59.38 %

Importance

Changes 0
Metric Value
dl 38
loc 64
c 0
b 0
f 0
rs 8.7853
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
use Illuminate\Database\Migrations\Migration;
4
use Illuminate\Database\Schema\Blueprint;
5
6
class CreatePermissionTables extends Migration
7
{
8
    /**
9
     * Run the migrations.
10
     *
11
     * @return void
12
     */
13
    public function up()
14
    {
15
        $tableNames = config('permission.table_names');
16
        $foreignKeys = config('permission.foreign_keys');
17
18 View Code Duplication
        Schema::create($tableNames['permissions'], function (Blueprint $table) {
0 ignored issues
show
Duplication introduced by Mahmoud Zalt
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
            $table->increments('id');
20
            $table->string('name');
21
            $table->string('guard_name');
22
            $table->string('display_name')->nullable();
23
            $table->string('description')->nullable();
24
            $table->timestamps();
25
        });
26
27 View Code Duplication
        Schema::create($tableNames['roles'], function (Blueprint $table) {
0 ignored issues
show
Duplication introduced by Mahmoud Zalt
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
            $table->increments('id');
29
            $table->string('name');
30
            $table->string('guard_name');
31
            $table->string('display_name')->nullable();
32
            $table->string('description')->nullable();
33
            $table->timestamps();
34
        });
35
36 View Code Duplication
        Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $foreignKeys) {
0 ignored issues
show
Duplication introduced by Mahmoud Zalt
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
            $table->integer('permission_id')->unsigned();
38
            $table->morphs('model');
39
40
            $table->foreign('permission_id')
41
                ->references('id')
42
                ->on($tableNames['permissions'])
43
                ->onDelete('cascade');
44
45
            $table->primary(['model_type', 'model_id', 'permission_id']);
46
        });
47
48 View Code Duplication
        Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $foreignKeys) {
0 ignored issues
show
Duplication introduced by Mahmoud Zalt
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            $table->integer('role_id')->unsigned();
50
            $table->morphs('model');
51
52
            $table->foreign('role_id')
53
                ->references('id')
54
                ->on($tableNames['roles'])
55
                ->onDelete('cascade');
56
57
            $table->primary(['model_id', 'role_id', 'model_type']);
58
        });
59
60
        Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
61
            $table->integer('permission_id')->unsigned();
62
            $table->integer('role_id')->unsigned();
63
64
            $table->foreign('permission_id')
65
                ->references('id')
66
                ->on($tableNames['permissions'])
67
                ->onDelete('cascade');
68
69
            $table->foreign('role_id')
70
                ->references('id')
71
                ->on($tableNames['roles'])
72
                ->onDelete('cascade');
73
74
            $table->primary(['role_id', 'permission_id']);
75
        });
76
    }
77
78
    /**
79
     * Reverse the migrations.
80
     *
81
     * @return void
82
     */
83
    public function down()
84
    {
85
        $tableNames = config('permission.table_names');
86
87
        Schema::drop($tableNames['role_has_permissions']);
88
        Schema::drop($tableNames['model_has_roles']);
89
        Schema::drop($tableNames['model_has_permissions']);
90
        Schema::drop($tableNames['roles']);
91
        Schema::drop($tableNames['permissions']);
92
    }
93
}
94