AdminSekolahController::index()   B
last analyzed

Complexity

Conditions 9
Paths 25

Size

Total Lines 54
Code Lines 33

Duplication

Lines 11
Ratio 20.37 %

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 25
nop 1
dl 11
loc 54
rs 7.255
c 0
b 0
f 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
namespace Bantenprov\Sekolah\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Sekolah\Facades\SekolahFacade;
9
10
/* Models */
11
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah;
12
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
13
use App\User;
14
15
/* Etc */
16
use Validator;
17
use Auth;
18
19
/**
20
 * The ProdiSekolahController class.
21
 *
22
 * @package Bantenprov\Sekolah
23
 * @author  bantenprov <[email protected]>
24
 */
25
class AdminSekolahController extends Controller
26
{
27
    protected $admin_sekolah;
28
    protected $sekolah;
29
    protected $user;
30
31
    /**
32
     * Create a new controller instance.
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36
    public function __construct()
37
    {
38
        $this->admin_sekolah    = new AdminSekolah;
39
        $this->sekolah          = new Sekolah;
40
        $this->user             = new User;
41
    }
42
43
    /**
44
     * Display a listing of the resource.
45
     *
46
     * @return \Illuminate\Http\Response
47
     */
48
    public function index(Request $request)
49
    {
50
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
51
52
        if(is_null($admin_sekolah) && $this->checkRole(['superadministrator']) === false){
53
            $response = [];
54
            return response()->json($response)
55
            ->header('Access-Control-Allow-Origin', '*')
56
            ->header('Access-Control-Allow-Methods', 'GET');
57
        }
58
59
        if (request()->has('sort')) {
60
            list($sortCol, $sortDir) = explode('|', request()->sort);
61
62
            if($this->checkRole(['superadministrator'])){
63
                $query = $this->admin_sekolah->orderBy($sortCol, $sortDir);
64
            }else{
65
                $query = $this->admin_sekolah->where('sekolah_id', $admin_sekolah->sekolah_id)->orderBy($sortCol, $sortDir);
66
            }
67
        } else {
68
            if($this->checkRole(['superadministrator'])){
69
                $query = $this->admin_sekolah->orderBy('id', 'asc');
70
            }else{
71
                $query = $this->admin_sekolah->where('admin_sekolah_id', $admin_sekolah->sekolah_id)->orderBy('id', 'asc');
72
            }
73
        }
74
75
        if ($request->exists('filter')) {
76
            if($this->checkRole(['superadministrator'])){
77 View Code Duplication
                $query->where(function($q) use($request) {
0 ignored issues
show
Duplication introduced by
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...
78
                    $value = "%{$request->filter}%";
79
80
                    $q->where('sekolah_id', 'like', $value)
81
                        ->orWhere('admin_sekolah_id', 'like', $value);
82
                });
83
            }else{
84 View Code Duplication
                $query->where(function($q) use($request, $admin_sekolah) {
0 ignored issues
show
Duplication introduced by
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...
85
                    $value = "%{$request->filter}%";
86
87
                    $q->where('sekolah_id', $admin_sekolah->sekolah_id)->where('sekolah_id', 'like', $value);
88
                });
89
            }
90
91
        }
92
93
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
94
95
        $response = $query->with(['admin_sekolah', 'sekolah', 'user'])->paginate($perPage);
96
97
        return response()->json($response)
98
            ->header('Access-Control-Allow-Origin', '*')
99
            ->header('Access-Control-Allow-Methods', 'GET');
100
101
    }
102
103
    /**
104
     * Display a listing of the resource.
105
     *
106
     * @return \Illuminate\Http\Response
107
     */
108 View Code Duplication
    public function get()
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...
109
    {
110
        $admin_sekolahs = $this->admin_sekolah->with(['sekolah', 'user'])->get();
111
112
        $response['admin_sekolahs'] = $admin_sekolahs;
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...
113
        $response['error']          = false;
114
        $response['message']        = 'Success';
115
        $response['status']         = true;
116
117
        return response()->json($response);
118
    }
119
120
    /**
121
     * Display a listing of the resource.
122
     *
123
     * @return \Illuminate\Http\Response
124
     */
125 View Code Duplication
    public function getBySekolah($id)
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...
126
    {
127
        $admin_sekolahs = $this->admin_sekolah->where('sekolah_id', '=', $id)->with(['sekolah', 'user'])->get();
128
129
        $response['admin_sekolahs'] = $admin_sekolahs;
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...
130
        $response['message']        = 'Success';
131
        $response['error']          = false;
132
        $response['status']         = true;
133
134
        return response()->json($response);
135
    }
136
137
    /**
138
     * Show the form for creating a new resource.
139
     *
140
     * @return \Illuminate\Http\Response
141
     */
142
    public function create()
143
    {
144
        $user_id            = isset(Auth::User()->id) ? Auth::User()->id : null;
145
        $admin_sekolah      = $this->admin_sekolah->getAttributes();
146
        $users              = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
147
        $users_special      = $this->user->all();
148
        $users_standar      = $this->user->findOrFail($user_id);
0 ignored issues
show
Unused Code introduced by
$users_standar is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
149
        $current_user       = Auth::User();
150
        $admins             = [];
151
152
        foreach($users_special as $user){
153
            if($user->hasRole(['superadministrator','admin_sekolah'])){
154
                array_set($user, 'label', $user->name);
155
                array_push($admins, $user);
156
            }
157
        }
158
159
        array_set($current_user, 'label', $current_user->name);
160
161
        $response['admin_sekolah']      = $admin_sekolah;
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...
162
        $response['users']              = $admins;
163
        $response['user_special']       = true;
164
        $response['current_user']       = $current_user;
165
        $response['error']              = false;
166
        $response['message']            = 'Success';
167
        $response['status']             = true;
168
169
        return response()->json($response);
170
    }
171
172
    /**
173
     * Store a newly created resource in storage.
174
     *
175
     * @param  \Illuminate\Http\Request  $request
176
     * @return \Illuminate\Http\Response
177
     */
178
    public function store(Request $request)
179
    {
180
        if($this->checkRole(['superadministrator'])){
181
            $admin_sekolah = $this->admin_sekolah;
182
183
            $validator = Validator::make($request->all(), [
184
                'sekolah_id'            => "required|exists:{$this->sekolah->getTable()},id",
185
                'admin_sekolah_id'      => "required|unique:{$this->admin_sekolah->getTable()},admin_sekolah_id,NULL,id,deleted_at,NULL",
186
            ]);
187
188 View Code Duplication
            if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
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...
189
                $error      = true;
190
                $message    = $validator->errors()->first();
191
            } else {
192
                $admin_sekolah->sekolah_id          = $request->input('sekolah_id');
193
                $admin_sekolah->admin_sekolah_id    = $request->input('admin_sekolah_id');
194
                $admin_sekolah->user_id             = Auth::user()->id;
195
                $admin_sekolah->save();
196
197
                $error      = false;
198
                $message    = 'Success';
199
            }
200
201
            $response['admin_sekolah']  = $admin_sekolah;
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...
202
            $response['error']          = $error;
203
            $response['message']        = $message;
204
            $response['status']         = true;
205
206
            return response()->json($response);
207
        }else{
208
209
            $response['error']          = 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...
210
            $response['message']        = 'Maaf anda Tidak mempunyai hak akses untuk ini.';
211
            $response['status']         = true;
212
213
            return response()->json($response);
214
        }
215
216
    }
217
218
    /**
219
     * Display the specified resource.
220
     *
221
     * @param  \App\ProdiSekolah  $prodi_sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $prodi_sekolah. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
222
     * @return \Illuminate\Http\Response
223
     */
224
    public function show($id)
225
    {
226 View Code Duplication
        if($this->checkRole(['superadministrator'])){
0 ignored issues
show
Duplication introduced by
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...
227
            $admin_sekolah = $this->admin_sekolah->with(['sekolah', 'admin_sekolah', 'user'])->findOrFail($id);
228
        }else{
229
            $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->with(['sekolah', 'admin_sekolah', 'user'])->findOrFail($id);
230
        }
231
232
233
        $response['admin_sekolah']  = $admin_sekolah;
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...
234
        $response['error']          = false;
235
        $response['message']        = 'Success';
236
        $response['status']         = true;
237
238
        return response()->json($response);
239
    }
240
241
    /**
242
     * Show the form for editing the specified resource.
243
     *
244
     * @param  \App\Sekolah  $sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $sekolah. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
245
     * @return \Illuminate\Http\Response
246
     */
247
    public function edit($id)
248
    {
249
        $user_id = isset(Auth::User()->id) ? Auth::User()->id : null;
250
251 View Code Duplication
        if($this->checkRole(['superadministrator'])){
0 ignored issues
show
Duplication introduced by
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...
252
            $admin_sekolah = $this->admin_sekolah->with(['sekolah', 'admin_sekolah', 'user'])->findOrFail($id);
253
        }else{
254
            $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->with(['sekolah', 'admin_sekolah', 'user'])->findOrFail($id);
255
        }
256
257
258
        $users              = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
259
        $users_special      = $this->user->all();
260
        $users_standar      = $this->user->findOrFail($user_id);
0 ignored issues
show
Unused Code introduced by
$users_standar is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
261
        $current_user       = Auth::User();
262
        $admins             = [];
263
264
265
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
0 ignored issues
show
Unused Code introduced by
$role_check is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
266
267
        if ($admin_sekolah->user !== null) {
268
            array_set($admin_sekolah->user, 'label', $admin_sekolah->user->name);
269
        }
270
271
        if ($admin_sekolah->admin_sekolah !== null) {
272
            array_set($admin_sekolah->admin_sekolah, 'label', $admin_sekolah->admin_sekolah->name);
273
        }
274
275
        foreach($users_special as $user){
276
            if($user->hasRole(['superadministrator','admin_sekolah'])){
277
                array_set($user, 'label', $user->name);
278
                array_push($admins, $user);
279
            }
280
        }
281
282
        array_set($current_user, 'label', $current_user->name);
283
284
        $response['admin_sekolah']      = $admin_sekolah;
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...
285
        $response['users']              = $admins;
286
        $response['user_special']       = true;
287
        $response['current_user']       = $current_user;
288
        $response['error']              = false;
289
        $response['message']            = 'Success';
290
        $response['status']             = true;
291
292
        return response()->json($response);
293
    }
294
295
    /**
296
     * Update the specified resource in storage.
297
     *
298
     * @param  \Illuminate\Http\Request  $request
299
     * @param  \App\Sekolah  $sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $sekolah. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
300
     * @return \Illuminate\Http\Response
301
     */
302
    public function update(Request $request, $id)
303
    {
304
        if($this->checkRole(['superadministrator'])){
305
            $admin_sekolah = $this->admin_sekolah->with(['sekolah', 'admin_sekolah', 'user'])->findOrFail($id);
306
307
            $validator = Validator::make($request->all(), [
308
                'sekolah_id'            => "required|exists:{$this->sekolah->getTable()},id",
309
                'admin_sekolah_id'      => "required|unique:{$this->admin_sekolah->getTable()},admin_sekolah_id,{$id},id,deleted_at,NULL",
310
            ]);
311
312 View Code Duplication
            if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
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...
313
                $error      = true;
314
                $message    = $validator->errors()->first();
315
            } else {
316
                $admin_sekolah->sekolah_id          = $request->input('sekolah_id');
317
                $admin_sekolah->admin_sekolah_id    = $request->input('admin_sekolah_id');
318
                $admin_sekolah->user_id             = Auth::user()->id;
319
                $admin_sekolah->save();
320
321
                $error      = false;
322
                $message    = 'Success';
323
            }
324
325
            $response['admin_sekolah']  = $admin_sekolah;
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...
326
            $response['error']          = $error;
327
            $response['message']        = $message;
328
            $response['status']         = true;
329
330
            return response()->json($response);
331
        }else{
332
            $response['error']          = 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...
333
            $response['message']        = 'Maaf anda Tidak mempunyai hak akses untuk ini.';
334
            $response['status']         = true;
335
336
            return response()->json($response);
337
        }
338
339
    }
340
341
    /**
342
     * Remove the specified resource from storage.
343
     *
344
     * @param  \App\Sekolah  $sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $sekolah. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
345
     * @return \Illuminate\Http\Response
346
     */
347
    public function destroy($id)
348
    {
349
        if($this->checkRole(['superadministrator'])){
350
            $admin_sekolah = $this->admin_sekolah->findOrFail($id);
351
352
            if ($admin_sekolah->delete()) {
353
                $response['message']    = 'Success';
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...
354
                $response['success']    = true;
355
                $response['status']     = true;
356
            } else {
357
                $response['message']    = 'Failed';
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...
358
                $response['success']    = false;
359
                $response['status']     = false;
360
            }
361
362
            return json_encode($response);
363
        }else{
364
            $response['message']    = 'Failed';
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...
365
            $response['success']    = false;
366
            $response['status']     = false;
367
            return json_encode($response);
368
        }
369
    }
370
371
    protected function checkRole($role = array())
372
    {
373
        return Auth::user()->hasRole($role);
374
    }
375
}
376