Completed
Pull Request — master (#26)
by
unknown
01:29
created

SekolahController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 42

Duplication

Lines 23
Ratio 45.1 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 42
c 1
b 0
f 0
nc 2
nop 2
dl 23
loc 51
rs 9.4109

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