Conditions | 11 |
Paths | 3 |
Total Lines | 105 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
15 | public function up() |
||
16 | { |
||
17 | $tableNames = config('permission.table_names'); |
||
18 | $columnNames = config('permission.column_names'); |
||
19 | $teams = config('permission.teams'); |
||
20 | |||
21 | if (empty($tableNames)) { |
||
22 | throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.'); |
||
23 | } |
||
24 | if ($teams && empty($columnNames['team_foreign_key'] ?? null)) { |
||
25 | throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.'); |
||
26 | } |
||
27 | |||
28 | Schema::create($tableNames['permissions'], function (Blueprint $table) { |
||
29 | $table->bigIncrements('id'); |
||
30 | $table->string('name'); // For MySQL 8.0 use string('name', 125); |
||
31 | $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); |
||
32 | $table->timestamps(); |
||
33 | |||
34 | $table->unique(['name', 'guard_name']); |
||
35 | }); |
||
36 | |||
37 | Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) { |
||
38 | $table->bigIncrements('id'); |
||
39 | if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing |
||
40 | $table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable(); |
||
41 | $table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index'); |
||
42 | } |
||
43 | $table->string('name'); // For MySQL 8.0 use string('name', 125); |
||
44 | $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); |
||
45 | $table->timestamps(); |
||
46 | if ($teams || config('permission.testing')) { |
||
47 | $table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']); |
||
48 | } else { |
||
49 | $table->unique(['name', 'guard_name']); |
||
50 | } |
||
51 | }); |
||
52 | |||
53 | Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) { |
||
54 | $table->unsignedBigInteger(PermissionRegistrar::$pivotPermission); |
||
55 | |||
56 | $table->string('model_type'); |
||
57 | $table->unsignedBigInteger($columnNames['model_morph_key']); |
||
58 | $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index'); |
||
59 | |||
60 | $table->foreign(PermissionRegistrar::$pivotPermission) |
||
61 | ->references('id') |
||
62 | ->on($tableNames['permissions']) |
||
63 | ->onDelete('cascade'); |
||
64 | if ($teams) { |
||
65 | $table->unsignedBigInteger($columnNames['team_foreign_key']); |
||
66 | $table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index'); |
||
67 | |||
68 | $table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'], |
||
69 | 'model_has_permissions_permission_model_type_primary'); |
||
70 | } else { |
||
71 | $table->primary([PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'], |
||
72 | 'model_has_permissions_permission_model_type_primary'); |
||
73 | } |
||
74 | |||
75 | }); |
||
76 | |||
77 | Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) { |
||
78 | $table->unsignedBigInteger(PermissionRegistrar::$pivotRole); |
||
79 | |||
80 | $table->string('model_type'); |
||
81 | $table->unsignedBigInteger($columnNames['model_morph_key']); |
||
82 | $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index'); |
||
83 | |||
84 | $table->foreign(PermissionRegistrar::$pivotRole) |
||
85 | ->references('id') |
||
86 | ->on($tableNames['roles']) |
||
87 | ->onDelete('cascade'); |
||
88 | if ($teams) { |
||
89 | $table->unsignedBigInteger($columnNames['team_foreign_key']); |
||
90 | $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index'); |
||
91 | |||
92 | $table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'], |
||
93 | 'model_has_roles_role_model_type_primary'); |
||
94 | } else { |
||
95 | $table->primary([PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'], |
||
96 | 'model_has_roles_role_model_type_primary'); |
||
97 | } |
||
98 | }); |
||
99 | |||
100 | Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { |
||
101 | $table->unsignedBigInteger(PermissionRegistrar::$pivotPermission); |
||
102 | $table->unsignedBigInteger(PermissionRegistrar::$pivotRole); |
||
103 | |||
104 | $table->foreign(PermissionRegistrar::$pivotPermission) |
||
105 | ->references('id') |
||
106 | ->on($tableNames['permissions']) |
||
107 | ->onDelete('cascade'); |
||
108 | |||
109 | $table->foreign(PermissionRegistrar::$pivotRole) |
||
110 | ->references('id') |
||
111 | ->on($tableNames['roles']) |
||
112 | ->onDelete('cascade'); |
||
113 | |||
114 | $table->primary([PermissionRegistrar::$pivotPermission, PermissionRegistrar::$pivotRole], 'role_has_permissions_permission_id_role_id_primary'); |
||
115 | }); |
||
116 | |||
117 | app('cache') |
||
118 | ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null) |
||
119 | ->forget(config('permission.cache.key')); |
||
120 | } |
||
142 |