CreatePermissionTables   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 88
Duplicated Lines 43.18 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 38
loc 88
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 38 64 1
A down() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

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
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