Completed
Push — master ( 792881...40073e )
by
unknown
9s
created

SiswaController::edit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Siswa\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Siswa\Facades\SiswaFacade;
9
10
/* Models */
11
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
12
use Laravolt\Indonesia\Models\Province;
13
use Laravolt\Indonesia\Models\City;
14
use Laravolt\Indonesia\Models\District;
15
use Laravolt\Indonesia\Models\Village;
16
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
17
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\ProdiSekolah;
18
use App\User;
19
20
/* Etc */
21
use Validator;
22
use Auth;
23
24
/**
25
 * The SiswaController class.
26
 *
27
 * @package Bantenprov\Siswa
28
 * @author  bantenprov <[email protected]>
29
 */
30
class SiswaController extends Controller
31
{
32
    protected $siswa;
33
    protected $province;
34
    protected $city;
35
    protected $district;
36
    protected $village;
37
    protected $sekolah;
38
    protected $prodi_sekolah;
39
    protected $user;
40
41
    /**
42
     * Create a new controller instance.
43
     *
44
     * @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...
45
     */
46
    public function __construct()
47
    {
48
        $this->siswa            = new Siswa;
49
        $this->province         = new Province;
50
        $this->city             = new City;
51
        $this->district         = new District;
52
        $this->village          = new Village;
53
        $this->sekolah          = new Sekolah;
54
        $this->prodi_sekolah    = new ProdiSekolah;
55
        $this->user             = new User;
56
    }
57
58
    /**
59
     * Display a listing of the resource.
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function index(Request $request)
64
    {
65
        if (request()->has('sort')) {
66
            list($sortCol, $sortDir) = explode('|', request()->sort);
67
68
            $query = $this->siswa->orderBy($sortCol, $sortDir);
69
        } else {
70
            $query = $this->siswa->orderBy('id', 'asc');
71
        }
72
73
        if ($request->exists('filter')) {
74
            $query->where(function($q) use($request) {
75
                $value = "%{$request->filter}%";
76
77
                $q->where('nomor_un', 'like', $value)
78
                    ->orWhere('nik', 'like', $value)
79
                    ->orWhere('nama_siswa', 'like', $value)
80
                    ->orWhere('no_kk', 'like', $value)
81
                    ->orWhere('nisn', 'like', $value);
82
            });
83
        }
84
85
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
86
87
        $response   = $query->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user', 'kegiatan'])->paginate($perPage);
88
89
        foreach($response as $siswa){
90
            if (isset($siswa->prodi_sekolah->program_keahlian)) {
91
                $siswa->prodi_sekolah->program_keahlian;
92
            }
93
        }
94
95
        return response()->json($response)
96
            ->header('Access-Control-Allow-Origin', '*')
97
            ->header('Access-Control-Allow-Methods', 'GET');
98
    }
99
100
    /**
101
     * Display a listing of the resource.
102
     *
103
     * @return \Illuminate\Http\Response
104
     */
105 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...
106
    {
107
        $siswas = $this->siswa->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user', 'kegiatan'])->get();
108
109
        $response['siswas']     = $siswas;
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...
110
        $response['error']      = false;
111
        $response['message']    = 'Success';
112
        $response['status']     = true;
113
114
        return response()->json($response);
115
    }
116
117
    /**
118
     * Show the form for creating a new resource.
119
     *
120
     * @return \Illuminate\Http\Response
121
     */
122
    public function create(Request $request)
123
    {
124
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
125
        $siswa          = $this->siswa->getAttributes();
126
        $provinces      = $this->province->getAttributes();
127
        $cities         = $this->city->getAttributes();
128
        $districts      = $this->district->getAttributes();
129
        $villages       = $this->village->getAttributes();
130
        $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...
131
        $users_special  = $this->user->all();
132
        $users_standar  = $this->user->findOrFail($user_id);
133
        $current_user   = Auth::User();
134
135
        foreach($provinces as $province){
136
            array_set($province, 'label', $province->name);
137
        }
138
139
        foreach($cities as $city){
140
            array_set($city, 'label', $city->name);
141
        }
142
143
        foreach($districts as $district){
144
            array_set($district, 'label', $district->name);
145
        }
146
147
        foreach($villages as $village){
148
            array_set($village, 'label', $village->name);
149
        }
150
151
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
152
153
        if($role_check){
154
            $user_special = true;
155
156
            foreach($users_special as $user){
157
                array_set($user, 'label', $user->name);
158
            }
159
160
            $users = $users_special;
161
        }else{
162
            $user_special = false;
163
164
            array_set($users_standar, 'label', $users_standar->name);
165
166
            $users = $users_standar;
167
        }
168
169
        array_set($current_user, 'label', $current_user->name);
170
171
        $response['siswa']          = $siswa;
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...
172
        $response['provinces']      = $provinces;
173
        $response['cities']         = $cities;
174
        $response['districts']      = $districts;
175
        $response['villages']       = $villages;
176
        $response['users']          = $users;
177
        $response['user_special']   = $user_special;
178
        $response['current_user']   = $current_user;
179
        $response['error']          = false;
180
        $response['message']        = 'Success';
181
        $response['status']         = true;
182
183
        return response()->json($response);
184
    }
185
186
    /**
187
     * Store a newly created resource in storage.
188
     *
189
     * @param  \Illuminate\Http\Request  $request
190
     * @return \Illuminate\Http\Response
191
     */
192
    public function store(Request $request)
193
    {
194
        $siswa      = $this->siswa;
195
        $validator  = Validator::make($request->all(), [
196
            'nomor_un'          => "required|max:255|unique:{$this->siswa->getTable()},nomor_un,NULL,id,deleted_at,NULL",
197
            'nik'               => "required|digits:16|unique:{$this->siswa->getTable()},nik,NULL,id,deleted_at,NULL",
198
            'nama_siswa'        => 'required|max:255',
199
            'no_kk'             => "required|digits:16|unique:{$this->siswa->getTable()},no_kk,NULL,id,deleted_at,NULL",
200
            'alamat_kk'         => 'required|max:255',
201
            'province_id'       => "required|exists:{$this->province->getTable()},id",
202
            'city_id'           => "required|exists:{$this->city->getTable()},id",
203
            'district_id'       => "required|exists:{$this->district->getTable()},id",
204
            'village_id'        => "required|exists:{$this->village->getTable()},id",
205
            'tempat_lahir'      => 'required|max:255',
206
            'tgl_lahir'         => 'required|date',
207
            'jenis_kelamin'     => 'required|max:255',
208
            'agama'             => 'required|max:255',
209
            'nisn'              => "required|between:6,15|unique:{$this->siswa->getTable()},nisn,NULL,id,deleted_at,NULL",
210
            'tahun_lulus'       => 'required|date_format:Y',
211
            'sekolah_id'        => "required|exists:{$this->sekolah->getTable()},id",
212
            'prodi_sekolah_id'  => "required|exists:{$this->prodi_sekolah->getTable()},id",
213
            'user_id'           => "required|exists:{$this->user->getTable()},id",
214
        ]);
215
216 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...
217
            $error      = true;
218
            $message    = $validator->errors()->first();
219
        } else {
220
            $siswa->user_id             = $request->input('user_id');
221
            $siswa->nomor_un            = $request->input('nomor_un');
222
            $siswa->nik                 = $request->input('nik');
223
            $siswa->nama_siswa          = $request->input('nama_siswa');
224
            $siswa->no_kk               = $request->input('no_kk');
225
            $siswa->alamat_kk           = $request->input('alamat_kk');
226
            $siswa->province_id         = $request->input('province_id');
227
            $siswa->city_id             = $request->input('city_id');
228
            $siswa->district_id         = $request->input('district_id');
229
            $siswa->village_id          = $request->input('village_id');
230
            $siswa->tempat_lahir        = $request->input('tempat_lahir');
231
            $siswa->tgl_lahir           = $request->input('tgl_lahir');
232
            $siswa->jenis_kelamin       = $request->input('jenis_kelamin');
233
            $siswa->agama               = $request->input('agama');
234
            $siswa->nisn                = $request->input('nisn');
235
            $siswa->sekolah_id          = $request->input('sekolah_id');
236
            $siswa->prodi_sekolah_id    = $request->input('prodi_sekolah_id');
237
            $siswa->tahun_lulus         = $request->input('tahun_lulus');
238
            $siswa->save();
239
240
            $error      = false;
241
            $message    = 'Success';
242
        }
243
244
        $response['siswa']      = $siswa;
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...
245
        $response['error']      = $error;
246
        $response['message']    = $message;
247
        $response['status']     = true;
248
249
        return response()->json($response);
250
    }
251
252
    /**
253
     * Display the specified resource.
254
     *
255
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. 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...
256
     * @return \Illuminate\Http\Response
257
     */
258 View Code Duplication
    public function show($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...
259
    {
260
        $siswa = $this->siswa->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user', 'kegiatan'])->findOrFail($id);
261
262
        $response['siswa']      = $siswa;
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...
263
        $response['error']      = false;
264
        $response['message']    = 'Success';
265
        $response['status']     = true;
266
267
        return response()->json($response);
268
    }
269
270
    /**
271
     * Show the form for editing the specified resource.
272
     *
273
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. 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...
274
     * @return \Illuminate\Http\Response
275
     */
276
    public function edit($id)
277
    {
278
        $siswa = $this->siswa->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user', 'kegiatan'])->findOrFail($id);
279
280
        $response['siswa']['province']      = array_add($siswa->province, 'label', $siswa->province->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...
281
        $response['siswa']['city']          = array_add($siswa->city, 'label', $siswa->city->name);
282
        $response['siswa']['district']      = array_add($siswa->district, 'label', $siswa->district->name);
283
        $response['siswa']['village']       = array_add($siswa->village, 'label', $siswa->village->name);
284
        $response['siswa']['sekolah']       = array_add($siswa->sekolah, 'label', $siswa->sekolah->nama);
285
        $response['siswa']['prodi_sekolah'] = array_add($siswa->prodi_sekolah, 'label', $siswa->prodi_sekolah->program_keahlian->label);
286
        $response['siswa']                  = $siswa;
287
        $response['error']                  = false;
288
        $response['message']                = 'Success';
289
        $response['status']                 = true;
290
291
        return response()->json($response);
292
    }
293
294
    /**
295
     * Update the specified resource in storage.
296
     *
297
     * @param  \Illuminate\Http\Request  $request
298
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. 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...
299
     * @return \Illuminate\Http\Response
300
     */
301
    public function update(Request $request, $id)
302
    {
303
        $siswa = $this->siswa->with(['province', 'city', 'district', 'village', 'sekolah', 'prodi_sekolah', 'user', 'kegiatan'])->findOrFail($id);
304
305
        $validator = Validator::make($request->all(), [
306
            'nomor_un'          => "required|max:255|unique:{$this->siswa->getTable()},nomor_un,{$id},id,deleted_at,NULL",
307
            'nik'               => "required|digits:16|unique:{$this->siswa->getTable()},nik,{$id},id,deleted_at,NULL",
308
            'nama_siswa'        => 'required|max:255',
309
            'no_kk'             => "required|digits:16|unique:{$this->siswa->getTable()},no_kk,{$id},id,deleted_at,NULL",
310
            'alamat_kk'         => 'required|max:255',
311
            'province_id'       => "required|exists:{$this->province->getTable()},id",
312
            'city_id'           => "required|exists:{$this->city->getTable()},id",
313
            'district_id'       => "required|exists:{$this->district->getTable()},id",
314
            'village_id'        => "required|exists:{$this->village->getTable()},id",
315
            'tempat_lahir'      => 'required|max:255',
316
            'tgl_lahir'         => 'required|date',
317
            'jenis_kelamin'     => 'required|max:255',
318
            'agama'             => 'required|max:255',
319
            'nisn'              => "required|between:4,17|unique:{$this->siswa->getTable()},nisn,{$id},id,deleted_at,NULL",
320
            'tahun_lulus'       => 'required|date_format:Y',
321
            'sekolah_id'        => "required|exists:{$this->sekolah->getTable()},id",
322
            'prodi_sekolah_id'  => "required|exists:{$this->prodi_sekolah->getTable()},id",
323
            'user_id'           => "required|exists:{$this->user->getTable()},id",
324
        ]);
325
326 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...
327
            $error      = true;
328
            $message    = $validator->errors()->first();
329
        } else {
330
            $siswa->user_id             = $request->input('user_id');
331
            $siswa->nomor_un            = $request->input('nomor_un');
332
            $siswa->nik                 = $request->input('nik');
333
            $siswa->nama_siswa          = $request->input('nama_siswa');
334
            $siswa->no_kk               = $request->input('no_kk');
335
            $siswa->alamat_kk           = $request->input('alamat_kk');
336
            $siswa->province_id         = $request->input('province_id');
337
            $siswa->city_id             = $request->input('city_id');
338
            $siswa->district_id         = $request->input('district_id');
339
            $siswa->village_id          = $request->input('village_id');
340
            $siswa->tempat_lahir        = $request->input('tempat_lahir');
341
            $siswa->tgl_lahir           = $request->input('tgl_lahir');
342
            $siswa->jenis_kelamin       = $request->input('jenis_kelamin');
343
            $siswa->agama               = $request->input('agama');
344
            $siswa->nisn                = $request->input('nisn');
345
            $siswa->sekolah_id          = $request->input('sekolah_id');
346
            $siswa->prodi_sekolah_id    = $request->input('prodi_sekolah_id');
347
            $siswa->tahun_lulus         = $request->input('tahun_lulus');
348
            $siswa->save();
349
350
            $error      = false;
351
            $message    = 'Success';
352
        }
353
354
        $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...
355
        $response['message']    = $message;
356
        $response['status']     = true;
357
358
        return response()->json($response);
359
    }
360
361
    /**
362
     * Remove the specified resource from storage.
363
     *
364
     * @param  \App\Siswa  $siswa
0 ignored issues
show
Bug introduced by
There is no parameter named $siswa. 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...
365
     * @return \Illuminate\Http\Response
366
     */
367
    public function destroy($id)
368
    {
369
        $siswa = $this->siswa->findOrFail($id);
370
371
        if ($siswa->delete()) {
372
            $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...
373
            $response['success']    = true;
374
            $response['status']     = true;
375
        } else {
376
            $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...
377
            $response['success']    = false;
378
            $response['status']     = false;
379
        }
380
381
        return json_encode($response);
382
    }
383
}
384