Completed
Push — master ( bd8614...a0c260 )
by
unknown
06:02 queued 04:03
created

VueTrustController   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 313
Duplicated Lines 49.52 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 155
loc 313
rs 9
c 0
b 0
f 0
wmc 35
lcom 1
cbo 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A demo() 0 4 1
B roleIndex() 23 23 4
B roleStore() 24 24 2
A roleShow() 9 9 1
A roleEdit() 9 9 1
B roleUpdate() 26 26 2
A roleDestroy() 0 11 1
B permissionIndex() 22 22 4
B permissionStore() 24 24 2
A permissionShow() 9 9 1
A permissionEdit() 9 9 1
B setPermission() 0 32 6
A setPermissionStore() 0 21 2
A removePermissionFromRole() 0 20 2
A removeSamePermission() 0 12 3
A macineName() 0 7 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 namespace Bantenprov\VueTrust\Http\Controllers;
2
3
use App\Http\Controllers\Controller;
4
use Illuminate\Http\Request;
5
use Bantenprov\VueTrust\Facades\VueTrust;
6
use Bantenprov\VueTrust\Models\VueTrustModel;
7
use App\Role;
8
use App\Permission;
9
10
use Validator;
11
12
/**
13
 * The VueTrustController class.
14
 *
15
 * @package Bantenprov\VueTrust
16
 * @author  bantenprov <[email protected]>
17
 */
18
class VueTrustController extends Controller
19
{
20
21
    protected $role;
22
    protected $permission;
23
    
24
    
25
26
    //[Function] __construct
27
    public function __construct(Role $role, Permission $permission){
28
        
29
        $this->role         = $role;
30
        $this->permission   = $permission;
31
32
    }    
33
34
    public function demo()
35
    {
36
        return VueTrust::welcome();
37
    }
38
39
    //[Function] roleIndex
40 View Code Duplication
    public function roleIndex(Request $request){        
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41
42
        if (request()->has('sort')) {
43
            list($sortCol, $sortDir) = explode('|', request()->sort);
44
45
            $query = $this->role->orderBy($sortCol, $sortDir);
46
        } else {
47
            $query = $this->role->orderBy('id', 'asc');
48
        }
49
50
        if ($request->exists('filter')) {
51
            $query->where(function($q) use($request) {
52
                $value = "%{$request->filter}%";
53
                $q->where('name', 'like', $value)
54
                    ->orWhere('description', 'like', $value);
55
            });
56
        }
57
58
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
59
        $response = $query->paginate($perPage);                
60
61
        return response()->json($response);
62
    }
63
64
    //[Function] roleStore
65 View Code Duplication
    public function roleStore(Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
66
        
67
        $validator = Validator::make($request->all(), [
68
            'name'          => 'required',
69
            'display_name'  => 'required',
70
            'description'   => 'required'
71
        ]);
72
73
        if($validator->fails()){
74
            $response['message']    = "failed add role";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
75
            $response['status']     = false;
76
        }else{
77
            $response['message']    = "success add new role";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
78
            $response['status']     = true;
79
80
            $save['name']           = $this->macineName($request->name);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$save was never initialized. Although not strictly required by PHP, it is generally a good practice to add $save = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
81
            $save['display_name']   = title_case($request->display_name);
82
            $save['description']    = $request->description;
83
84
            $this->role->create($save);
85
        }
86
        
87
        return response()->json($response);
88
    }
89
90
    //[Function] roleShow
91 View Code Duplication
    public function roleShow($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
92
        
93
        $role = $this->role->findOrFail($id);
94
95
        $response['status'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
96
        $response['role']   = $role;
97
        
98
        return response()->json($response);
99
    }
100
101
    //[Function] roleEdit
102 View Code Duplication
    public function roleEdit($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
103
        
104
        $role = $this->role->findOrFail($id);
105
106
        $response['status'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
107
        $response['role']   = $role;
108
        
109
        return response()->json($response);
110
    }
111
112
    //[Function] roleUpdate
113 View Code Duplication
    public function roleUpdate($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
114
        
115
        $validator = Validator::make($request->all(), [
116
            'name'          => 'required',
117
            'display_name'  => 'required',
118
            'description'   => 'required'
119
        ]);
120
121
        if($validator->fails()){
122
            $response['message']    = "failed update role";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
123
            $response['status']     = false;
124
        }else{
125
126
            $save['name']           = $this->macineName($request->name);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$save was never initialized. Although not strictly required by PHP, it is generally a good practice to add $save = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
127
            $save['display_name']   = title_case($request->display_name);
128
            $save['description']    = $request->description; 
129
130
            $this->role->findOrFail($id)->update($save);
131
132
            $response['message']    = "success update role";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
133
            $response['status']     = true;                       
134
135
        }
136
        
137
        return response()->json($response);
138
    }   
139
    
140
    //[Function] roleDestroy
141
    public function roleDestroy($id, Request $request){
142
        
143
        $role = $this->role->findOrFail($id);
144
145
        $response['message']    = "success delete role [" . $role->name . "]";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
146
        $response['status']     = true;
147
148
        $role->delete();
149
        
150
        return response()->json($response);
151
    }
152
153
    //[Function] permissionIndex
154 View Code Duplication
    public function permissionIndex(Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
155
        if (request()->has('sort')) {
156
            list($sortCol, $sortDir) = explode('|', request()->sort);
157
158
            $query = $this->permission->orderBy($sortCol, $sortDir);
159
        } else {
160
            $query = $this->permission->orderBy('id', 'asc');
161
        }
162
163
        if ($request->exists('filter')) {
164
            $query->where(function($q) use($request) {
165
                $value = "%{$request->filter}%";
166
                $q->where('name', 'like', $value)
167
                    ->orWhere('description', 'like', $value);
168
            });
169
        }
170
171
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
172
        $response = $query->paginate($perPage);                
173
174
        return response()->json($response);
175
    }
176
177
    //[Function] permissionStore
178 View Code Duplication
    public function permissionStore(Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
179
        
180
        $validator = Validator::make($request->all(), [
181
            'name'          => 'required',
182
            'display_name'  => 'required',
183
            'description'   => 'required'
184
        ]);
185
186
        if($validator->fails()){
187
            $response['message']    = "failed add permission";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
188
            $response['status']     = false;
189
        }else{
190
            $response['message']    = "success add new permission";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
191
            $response['status']     = true;
192
193
            $save['name']           = $this->macineName($request->name);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$save was never initialized. Although not strictly required by PHP, it is generally a good practice to add $save = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
194
            $save['display_name']   = title_case($request->display_name);
195
            $save['description']    = $request->description;
196
197
            $this->permission->create($save);
198
        }
199
        
200
        return response()->json($response);
201
    }
202
203
    //[Function] permissionShow
204 View Code Duplication
    public function permissionShow($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
205
        
206
        $role = $this->permission->findOrFail($id);
207
208
        $response['status']       = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
209
        $response['permission']   = $role;
210
        
211
        return response()->json($response);
212
    }
213
214
    //[Function] permissionEdit
215 View Code Duplication
    public function permissionEdit($id, Request $request){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
216
        
217
        $role = $this->permission->findOrFail($id);
218
219
        $response['status'] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
220
        $response['permission']   = $role;
221
        
222
        return response()->json($response);
223
    }
224
225
    //[Function] setPermission
226
    public function setPermission($id, Request $request){
227
        
228
        $role = $this->role->findOrFail($id);
229
        $current_permissions = $role->permissions()->get();
230
        $permissions = $this->permission->all();
231
        
232
        foreach ($permissions as $permission) {
233
234
            array_set($permission, 'disable', false);
235
236
            foreach($current_permissions as $current_permision){
237
                if($permission->id == $current_permision->id){
238
                    //
239
                    //echo $current_permision->id;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
240
                    if($permission->id == $current_permision->id){
241
                        array_set($permission, 'disable', true);
242
                    }
243
                }else{
244
                    if($permission->id == $current_permision->id){
245
                        array_set($permission, 'disable', false);
246
                    }
247
                }
248
            }
249
        }
250
        
251
252
        $response['status']                 = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
253
        $response['current_permissions']    = $current_permissions;
254
        $response['permissions']            = $permissions; 
255
        
256
        return response()->json($response);
257
    }
258
259
    //[Function] setPermissionStore
260
    public function setPermissionStore($id, Request $request){
261
        
262
        $validator = Validator::make($request->all(), [
263
            'permission_id'          => 'required',
264
        ]);
265
266
        if($validator->fails()){
267
            $response['message']    = "failed add permission";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
268
            $response['status']     = false;
269
        }else{
270
            $response['message']    = "success add new permission";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
271
            $response['status']     = true;   
272
273
            $current_permissions = $this->role->findOrFail($id)->permissions()->get();   
274
            $save = $this->removeSamePermission($request->permission_id, $current_permissions);
275
            //dd($current_permissions);
276
            $this->role->findOrFail($id)->attachPermissions($save);
277
        }
278
        
279
        return response()->json($response);
280
    }
281
282
    //[Function] removePermissionFromRole
283
    public function removePermissionFromRole($id, Request $request){
284
        
285
        $validator = Validator::make($request->all(), [
286
            'permission_id'          => 'required',
287
        ]);
288
289
        if($validator->fails()){
290
            $response['message']    = "failed remove permission";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
291
            $response['status']     = false;
292
        }else{
293
            $response['message']    = "success remove new permission";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
294
            $response['status']     = true;   
295
             
296
            $data = $request->permission_id;
297
            
298
            $this->role->findOrFail($id)->detachPermission($data);
299
        }
300
        
301
        return response()->json($response);
302
    }
303
304
    //[Function] removeSamePermission
305
    protected function removeSamePermission($array, $search){                
306
        
307
        foreach($search as $value1){   
308
            print_r("[".$value1->id."]");   
309
                
310
            $key = array_search($value1->id, $array);
311
            if(!empty($key)){
312
                unset($array[$key]);
313
            }                        
314
        }
315
        return $array;        
316
    }
317
    
318
    
319
320
    //[Function] macineName
321
    protected function macineName($text){
322
        
323
        $final = strtolower(str_replace(' ', '_', $text));
324
325
        return $final;
326
327
    }
328
    
329
    
330
}
331