Completed
Push — master ( 719c85...1f94db )
by
unknown
01:26
created

PendaftaranController::checkRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Pendaftaran\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Carbon\Carbon;
9
/* Models */
10
use Bantenprov\Pendaftaran\Models\Bantenprov\Pendaftaran\Pendaftaran;
11
use Bantenprov\Kegiatan\Models\Bantenprov\Kegiatan\Kegiatan;
12
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
13
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah;
14
use App\User;
15
16
/* Etc */
17
use Validator;
18
use Auth;
19
20
/**
21
 * The PendaftaranController class.
22
 *
23
 * @package Bantenprov\Pendaftaran
24
 * @author  bantenprov <[email protected]>
25
 */
26
class PendaftaranController extends Controller
27
{
28
    /**
29
     * Create a new controller instance.
30
     *
31
     * @return void
32
     */
33
    protected $kegiatanModel;
34
    protected $pendaftaran;
35
    protected $user;
36
    protected $sekolah;
37
    protected $admin_sekolah;
38
39
    public function __construct(Pendaftaran $pendaftaran, Kegiatan $kegiatan, User $user, Sekolah $sekolah, AdminSekolah $admin_sekolah)
40
    {
41
        $this->pendaftaran      = $pendaftaran;
42
        $this->kegiatanModel    = $kegiatan;
43
        $this->user             = $user;
44
        $this->sekolah          = $sekolah;
45
        $this->admin_sekolah    = $admin_sekolah;
46
    }
47
48
    /**
49
     * Display a listing of the resource.
50
     *
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function index(Request $request)
54
    {
55
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
56
57
        if(is_null($admin_sekolah) && $this->checkRole(['superadministrator']) === false){
58
            $response = [];
59
            return response()->json($response)
60
            ->header('Access-Control-Allow-Origin', '*')
61
            ->header('Access-Control-Allow-Methods', 'GET');
62
        }
63
64
65
        if (request()->has('sort')) {
66
            list($sortCol, $sortDir) = explode('|', request()->sort);
67
68 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...
69
                $query = $this->pendaftaran->with('kegiatan')->with('user')->orderBy($sortCol, $sortDir);
70
            }else{
71
                $query = $this->pendaftaran->where('sekolah_id', $admin_sekolah->sekolah_id)->with('kegiatan')->with('user')->orderBy($sortCol, $sortDir);
72
            }
73
74 View Code Duplication
        } else {
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...
75
            if($this->checkRole(['superadministrator'])){
76
                $query = $this->pendaftaran->with('kegiatan')->with('user')->orderBy($sortCol, $sortDir);
0 ignored issues
show
Bug introduced by
The variable $sortCol seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $sortDir seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
77
            }else{
78
                $query = $this->pendaftaran->where('sekolah_id', $admin_sekolah->sekolah_id)->with('kegiatan')->with('user')->orderBy('id', 'asc');
79
            }
80
81
        }
82
83
        if ($request->exists('filter')) {
84
            $query->where(function($q) use($request) {
85
                $value = "%{$request->filter}%";
86
                $q->where('tanggal_pendaftaran', 'like', $value);
87
            });
88
        }
89
90
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
91
        $response = $query->with(['sekolah'])->paginate($perPage);
92
93
        return response()->json($response)
94
            ->header('Access-Control-Allow-Origin', '*')
95
            ->header('Access-Control-Allow-Methods', 'GET');
96
    }
97
98
    /**
99
     * Show the form for creating a new resource.
100
     *
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function create()
104
    {
105
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
106
107
        $response = [];
108
109
        $kegiatan       = $this->kegiatanModel->all();
110
        if($this->checkRole(['superadministrator'])){
111
            $sekolahs       = $this->sekolah->all();
112
        }else{
113
            $sekolahs       = $this->sekolah->where('id',$admin_sekolah->sekolah_id)->get();
114
        }
115
116
        $users_special  = $this->user->all();
117
        $users_standar  = $this->user->find(\Auth::User()->id);
118
        $current_user   = \Auth::User();
119
120
        foreach($sekolahs as $sekolah){
121
            array_set($sekolah, 'label', $sekolah->nama);
122
        }
123
124
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
125
126
        if($role_check){
127
            $response['user_special'] = true;
128
            foreach($users_special as $user){
129
                array_set($user, 'label', $user->name);
130
            }
131
            $response['user'] = $users_special;
132
        }else{
133
            $response['user_special'] = false;
134
            array_set($users_standar, 'label', $users_standar->name);
135
            $response['user'] = $users_standar;
136
        }
137
138
        array_set($current_user, 'label', $current_user->name);
139
140
        $response['current_user']   = $current_user;
141
        $response['kegiatan']       = $kegiatan;
142
        //$response['user']           = $users;
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...
143
        $response['sekolah']        = $sekolahs;
144
        $response['error']          = false;
145
        $response['message']        = 'Success';
146
        $response['status']         = true;
147
148
        return response()->json($response);
149
    }
150
151
    /**
152
     * Display the specified resource.
153
     *
154
     * @param  \App\Pendaftaran  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. 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...
155
     * @return \Illuminate\Http\Response
156
     */
157
    public function store(Request $request)
158
    {
159
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
160
161
        $pendaftaran        = $this->pendaftaran;
162
        $current_user_id    = $request->user_id;
163
164
        $validator = Validator::make($request->all(), [
165
            'kegiatan_id'           => 'required',
166
            'user_id'               => "required|exists:{$this->user->getTable()},id",
167
            'tanggal_pendaftaran'   => 'required',
168
            'sekolah_id'            => 'required',
169
        ]);
170
171
        if($admin_sekolah->sekolah_id != $request->sekolah_id && $this->checkRole(['superadministrator']) === false){
172
            $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...
173
            $response['message']    = 'Terjadi kesalahan, mohon ulangi lagi pengisian data yang benar.';
174
            $response['status']     = true;
175
176
            return response()->json($response);
177
        }
178
179 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...
180
            $error      = true;
181
            $message    = $validator->errors()->first();
182
183
            } else {
184
                $pendaftaran->kegiatan_id           = $request->input('kegiatan_id');
185
                $pendaftaran->user_id               = $current_user_id;
186
                $pendaftaran->tanggal_pendaftaran   = $request->input('tanggal_pendaftaran');
187
                $pendaftaran->sekolah_id            = $request->input('sekolah_id');
188
                $pendaftaran->save();
189
190
                $error      = false;
191
                $message    = 'Success';
192
            }
193
194
        $response['error']      = $error;
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...
195
        $response['message']    = $message;
196
        $response['status']     = true;
197
198
        return response()->json($response);
199
    }
200
201
    /**
202
     * Store a newly created resource in storage.
203
     *
204
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. 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...
205
     * @return \Illuminate\Http\Response
206
     */
207
    public function show($id)
208
    {
209
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
210
211
        if($this->checkRole(['superadministrator'])){
212
            $pendaftaran = $this->pendaftaran->findOrFail($id);
213
        }else{
214
            $pendaftaran = $this->pendaftaran->where('sekolah_id', $admin_sekolah->sekolah_id)->findOrFail($id);
215
        }
216
217
218
219
        $response['pendaftaran']    = $pendaftaran;
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['kegiatan']       = $pendaftaran->kegiatan;
221
        $response['sekolah']        = $pendaftaran->sekolah;
222
        $response['user']           = $pendaftaran->user;
223
        $response['status']         = true;
224
225
        return response()->json($response);
226
    }
227
228
    /**
229
     * Show the form for editing the specified resource.
230
     *
231
     * @param  \App\Pendaftaran  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. 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...
232
     * @return \Illuminate\Http\Response
233
     */
234
    public function edit($id)
235
    {
236
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
237
238
        if($this->checkRole(['superadministrator'])){
239
            $pendaftaran = $this->pendaftaran->with(['kegiatan', 'sekolah', 'user' ])->findOrFail($id);
240
        }else{
241
            $pendaftaran = $this->pendaftaran->where('sekolah_id', $admin_sekolah->sekolah_id)->with(['kegiatan', 'sekolah', 'user' ])->findOrFail($id);
242
        }
243
244
245
        $response['pendaftaran']['user']     = array_add($pendaftaran->user, 'label', $pendaftaran->user->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...
246
247
248
249
        $response['pendaftaran']    = $pendaftaran;
250
        //$response['kegiatan']       = $pendaftaran->kegiatan;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
251
        //$response['sekolah']        = $pendaftaran->sekolah->nama;
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
252
        //$response['user']           = $pendaftaran->user;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% 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...
253
        $response['error']          = false;
254
        $response['message']        = 'Success';
255
        $response['status']         = true;
256
257
        return response()->json($response);
258
    }
259
260
    /**
261
     * Update the specified resource in storage.
262
     *
263
     * @param  \Illuminate\Http\Request  $request
264
     * @param  \App\Pendaftaran  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. 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...
265
     * @return \Illuminate\Http\Response
266
     */
267
    public function update(Request $request, $id)
268
    {
269
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
270
271
        if($this->checkRole(['superadministrator'])){
272
            $pendaftaran = $this->pendaftaran->with(['kegiatan', 'sekolah', 'user' ])->findOrFail($id);
273
        }else{
274
            $pendaftaran = $this->pendaftaran->where('sekolah_id', $admin_sekolah->sekolah_id)->with(['kegiatan', 'sekolah', 'user' ])->findOrFail($id);
275
276
            if(is_null($pendaftaran)){
277
                $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...
278
                $response['message']    = 'Terjadi kesalahan saat update data.';
279
                $response['status']     = true;
280
281
                return response()->json($response);
282
            }
283
        }
284
285
286
        $validator = Validator::make($request->all(), [
287
            'kegiatan_id'           => 'required',
288
            'tanggal_pendaftaran'   => 'required',
289
            'sekolah_id'            => 'required',
290
        ]);
291
292 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...
293
            $error      = true;
294
            $message    = $validator->errors()->first();
295
296
        } else {
297
            $pendaftaran->kegiatan_id           = $request->input('kegiatan_id');
298
            $pendaftaran->tanggal_pendaftaran   = $request->input('tanggal_pendaftaran');
299
            $pendaftaran->sekolah_id            = $request->input('sekolah_id');
300
            $pendaftaran->save();
301
302
            $error      = false;
303
            $message    = 'Success';
304
        }
305
306
        $response['error']      = $error;
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...
307
        $response['message']    = $message;
308
        $response['status']     = true;
309
310
        return response()->json($response);
311
    }
312
313
    /**
314
     * Remove the specified resource from storage.
315
     *
316
     * @param  \App\Pendaftaran  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. 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...
317
     * @return \Illuminate\Http\Response
318
     */
319
    public function destroy($id)
320
    {
321
        if(!$this->checkRole(['superadministrator'])){
322
            $response['status'] = false;
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...
323
            return json_encode($response);
324
        }
325
326
        $pendaftaran = $this->pendaftaran->findOrFail($id);
327
328
        if ($pendaftaran->delete()) {
329
            $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...
330
        } else {
331
            $response['status'] = false;
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...
332
        }
333
334
        return json_encode($response);
335
    }
336
337
    protected function checkRole($role = array())
338
    {
339
        return Auth::user()->hasRole($role);
340
    }
341
}
342