Completed
Push — master ( 5d60c7...e39782 )
by
unknown
9s
created

SekolahController::create()   C

Complexity

Conditions 9
Paths 192

Size

Total Lines 69
Code Lines 46

Duplication

Lines 15
Ratio 21.74 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 46
nc 192
nop 0
dl 15
loc 69
rs 5.7962
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Bantenprov\Sekolah\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Sekolah\Facades\SekolahFacade;
9
10
/* Models */
11
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\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 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...
95
    {
96
        $sekolahs = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->get();
97
98
        $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...
99
        $response['error']      = false;
100
        $response['message']    = 'Success';
101
        $response['status']     = true;
102
103
        return response()->json($response);
104
    }
105
106
    /**
107
     * Show the form for creating a new resource.
108
     *
109
     * @return \Illuminate\Http\Response
110
     */
111
    public function create()
112
    {
113
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
114
        $sekolah        = $this->sekolah->getAttributes();
115
        $provinces      = $this->province->getAttributes();
116
        $cities         = $this->city->getAttributes();
117
        $districts      = $this->district->getAttributes();
118
        $villages       = $this->village->getAttributes();
119
        $master_zonas   = $this->master_zona->all();
120
        $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...
121
        $users_special  = $this->user->all();
122
        $users_standar  = $this->user->findOrFail($user_id);
123
        $current_user   = Auth::User();
124
125
        foreach($provinces as $province){
126
            array_set($province, 'label', $province->name);
127
        }
128
129
        foreach($cities as $city){
130
            array_set($city, 'label', $city->name);
131
        }
132
133
        foreach($districts as $district){
134
            array_set($district, 'label', $district->name);
135
        }
136
137
        foreach($villages as $village){
138
            array_set($village, 'label', $village->name);
139
        }
140
141
        foreach($master_zonas as $master_zona){
142
            array_set($master_zona, 'label', $master_zona->label);
143
        }
144
145
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
146
147 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...
148
            $user_special = true;
149
150
            foreach($users_special as $user){
151
                array_set($user, 'label', $user->name);
152
            }
153
154
            $users = $users_special;
155
        }else{
156
            $user_special = false;
157
158
            array_set($users_standar, 'label', $users_standar->name);
159
160
            $users = $users_standar;
161
        }
162
163
        array_set($current_user, 'label', $current_user->name);
164
165
        $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...
166
        $response['provinces']      = $provinces;
167
        $response['cities']         = $cities;
168
        $response['districts']      = $districts;
169
        $response['villages']       = $villages;
170
        $response['master_zonas']   = $master_zonas;
171
        $response['users']          = $users;
172
        $response['user_special']   = $user_special;
173
        $response['current_user']   = $current_user;
174
        $response['error']          = false;
175
        $response['message']        = 'Success';
176
        $response['status']         = true;
177
178
        return response()->json($response);
179
    }
180
181
    /**
182
     * Store a newly created resource in storage.
183
     *
184
     * @param  \Illuminate\Http\Request  $request
185
     * @return \Illuminate\Http\Response
186
     */
187
    public function store(Request $request)
188
    {
189
        $sekolah = $this->sekolah;
190
191
        $validator = Validator::make($request->all(), [
192
            'nama'              => 'required|max:255',
193
            'npsn'              => "required|between:4,17|unique:{$this->sekolah->getTable()},npsn,NULL,id,deleted_at,NULL",
194
            'jenis_sekolah_id'  => "required|exists:{$this->jenis_sekolah->getTable()},id",
195
            'alamat'            => 'required|max:255',
196
            'logo'              => 'max:255',
197
            'foto_gedung'       => 'max:255',
198
            'province_id'       => "required|exists:{$this->province->getTable()},id",
199
            'city_id'           => "required|exists:{$this->city->getTable()},id",
200
            'district_id'       => "required|exists:{$this->district->getTable()},id",
201
            'village_id'        => "required|exists:{$this->village->getTable()},id",
202
            'no_telp'           => 'required|digits_between:10,12',
203
            'email'             => 'required|email|max:255',
204
            'kode_zona'         => "required|exists:{$this->master_zona->getTable()},id",
205
            'user_id'           => "required|exists:{$this->user->getTable()},id",
206
        ]);
207
208 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...
209
            $error      = true;
210
            $message    = $validator->errors()->first();
211
        } else {
212
            $sekolah->id                = $request->input('npsn');
213
            $sekolah->nama              = $request->input('nama');
214
            $sekolah->npsn              = $request->input('npsn');
215
            $sekolah->jenis_sekolah_id  = $request->input('jenis_sekolah_id');
216
            $sekolah->alamat            = $request->input('alamat');
217
            $sekolah->logo              = $request->input('logo');
218
            $sekolah->foto_gedung       = $request->input('foto_gedung');
219
            $sekolah->province_id       = $request->input('province_id');
220
            $sekolah->city_id           = $request->input('city_id');
221
            $sekolah->district_id       = $request->input('district_id');
222
            $sekolah->village_id        = $request->input('village_id');
223
            $sekolah->no_telp           = $request->input('no_telp');
224
            $sekolah->email             = $request->input('email');
225
            $sekolah->kode_zona         = $request->input('kode_zona');
226
            $sekolah->user_id           = $request->input('user_id');
227
            $sekolah->save();
228
229
            $error      = false;
230
            $message    = 'Success';
231
        }
232
233
        $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...
234
        $response['message']    = $message;
235
        $response['status']     = true;
236
237
        return response()->json($response);
238
    }
239
240
    /**
241
     * Display the specified resource.
242
     *
243
     * @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...
244
     * @return \Illuminate\Http\Response
245
     */
246 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...
247
    {
248
        $sekolah = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->findOrFail($id);
249
250
        $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...
251
        $response['error']      = false;
252
        $response['message']    = 'Success';
253
        $response['status']     = true;
254
255
        return response()->json($response);
256
    }
257
258
    /**
259
     * Show the form for editing the specified resource.
260
     *
261
     * @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...
262
     * @return \Illuminate\Http\Response
263
     */
264
    public function edit($id)
265
    {
266
        $sekolah = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->findOrFail($id);
267
268
        $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...
269
        $response['sekolah']['city']        = array_add($sekolah->city, 'label', $sekolah->city->name);
270
        $response['sekolah']['district']    = array_add($sekolah->district, 'label', $sekolah->district->name);
271
        $response['sekolah']['village']     = array_add($sekolah->village, 'label', $sekolah->village->name);
272
        $response['sekolah']                = $sekolah;
273
        $response['error']                  = false;
274
        $response['message']                = 'Success';
275
        $response['status']             = true;
276
277
        return response()->json($response);
278
    }
279
280
    /**
281
     * Update the specified resource in storage.
282
     *
283
     * @param  \Illuminate\Http\Request  $request
284
     * @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...
285
     * @return \Illuminate\Http\Response
286
     */
287
    public function update(Request $request, $id)
288
    {
289
        $sekolah = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->findOrFail($id);
290
291
        $validator = Validator::make($request->all(), [
292
            'nama'              => 'required|max:255',
293
            'npsn'              => "required|between:4,17|unique:{$this->sekolah->getTable()},npsn,{$id},id,deleted_at,NULL",
294
            'jenis_sekolah_id'  => "required|exists:{$this->jenis_sekolah->getTable()},id",
295
            'alamat'            => 'required|max:255',
296
            'logo'              => 'max:255',
297
            'foto_gedung'       => 'max:255',
298
            'province_id'       => "required|exists:{$this->province->getTable()},id",
299
            'city_id'           => "required|exists:{$this->city->getTable()},id",
300
            'district_id'       => "required|exists:{$this->district->getTable()},id",
301
            'village_id'        => "required|exists:{$this->village->getTable()},id",
302
            'no_telp'           => 'required|digits_between:10,12',
303
            'email'             => 'required|email|max:255',
304
            'kode_zona'         => "required|exists:{$this->master_zona->getTable()},id",
305
            'user_id'           => "required|exists:{$this->user->getTable()},id",
306
        ]);
307
308 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...
309
            $error      = true;
310
            $message    = $validator->errors()->first();
311
        } else {
312
            $sekolah->id                = $request->input('npsn');
313
            $sekolah->nama              = $request->input('nama');
314
            $sekolah->npsn              = $request->input('npsn');
315
            $sekolah->jenis_sekolah_id  = $request->input('jenis_sekolah_id');
316
            $sekolah->alamat            = $request->input('alamat');
317
            $sekolah->logo              = $request->input('logo');
318
            $sekolah->foto_gedung       = $request->input('foto_gedung');
319
            $sekolah->province_id       = $request->input('province_id');
320
            $sekolah->city_id           = $request->input('city_id');
321
            $sekolah->district_id       = $request->input('district_id');
322
            $sekolah->village_id        = $request->input('village_id');
323
            $sekolah->no_telp           = $request->input('no_telp');
324
            $sekolah->email             = $request->input('email');
325
            $sekolah->kode_zona         = $request->input('kode_zona');
326
            $sekolah->user_id           = $request->input('user_id');
327
            $sekolah->save();
328
329
            $error      = false;
330
            $message    = 'Success';
331
        }
332
333
        $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...
334
        $response['message']    = $message;
335
        $response['status']     = true;
336
337
        return response()->json($response);
338
    }
339
340
    /**
341
     * Remove the specified resource from storage.
342
     *
343
     * @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...
344
     * @return \Illuminate\Http\Response
345
     */
346 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...
347
    {
348
        $sekolah = $this->sekolah->findOrFail($id);
349
350
        if ($sekolah->delete()) {
351
            $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...
352
            $response['success']    = true;
353
            $response['status']     = true;
354
        } else {
355
            $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...
356
            $response['success']    = false;
357
            $response['status']     = false;
358
        }
359
360
        return json_encode($response);
361
    }
362
}
363