SekolahController::create()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 47
Code Lines 34

Duplication

Lines 15
Ratio 31.91 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 34
nc 6
nop 0
dl 15
loc 47
rs 8.6845
c 1
b 0
f 0
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
        if (request()->has('sort')) {
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
        $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...
120
        $users_special  = $this->user->all();
121
        $users_standar  = $this->user->findOrFail($user_id);
122
        $current_user   = Auth::User();
123
124
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
125
126 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...
127
            $user_special = true;
128
129
            foreach ($users_special as $user) {
130
                array_set($user, 'label', $user->name);
131
            }
132
133
            $users = $users_special;
134
        } else {
135
            $user_special = false;
136
137
            array_set($users_standar, 'label', $users_standar->name);
138
139
            $users = $users_standar;
140
        }
141
142
        array_set($current_user, 'label', $current_user->name);
143
144
        $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...
145
        $response['provinces']      = $provinces;
146
        $response['cities']         = $cities;
147
        $response['districts']      = $districts;
148
        $response['villages']       = $villages;
149
        $response['users']          = $users;
150
        $response['user_special']   = $user_special;
151
        $response['current_user']   = $current_user;
152
        $response['error']          = false;
153
        $response['message']        = 'Success';
154
        $response['status']         = true;
155
156
        return response()->json($response);
157
    }
158
159
    /**
160
     * Store a newly created resource in storage.
161
     *
162
     * @param  \Illuminate\Http\Request  $request
163
     * @return \Illuminate\Http\Response
164
     */
165
    public function store(Request $request)
166
    {
167
        $sekolah = $this->sekolah;
168
169
        $validator = Validator::make($request->all(), [
170
            'nama'              => 'required|max:255',
171
            'npsn'              => "required|between:4,17|unique:{$this->sekolah->getTable()},npsn,NULL,id,deleted_at,NULL",
172
            'jenis_sekolah_id'  => "required|exists:{$this->jenis_sekolah->getTable()},id",
173
            'alamat'            => 'required|max:255',
174
            'logo'              => 'max:255',
175
            'foto_gedung'       => 'max:255',
176
            'province_id'       => "required|exists:{$this->province->getTable()},id",
177
            'city_id'           => "required|exists:{$this->city->getTable()},id",
178
            'district_id'       => "required|exists:{$this->district->getTable()},id",
179
            'village_id'        => "required|exists:{$this->village->getTable()},id",
180
            'no_telp'           => 'required|digits_between:10,12',
181
            'email'             => 'required|email|max:255',
182
            'uuid'              => 'required|max:255',
183
            'kode_zona'         => "required|exists:{$this->master_zona->getTable()},id",
184
            'user_id'           => "required|exists:{$this->user->getTable()},id",
185
        ]);
186
187 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...
188
            $error      = true;
189
            $message    = $validator->errors()->first();
190
        } else {
191
            $sekolah->id                = $request->input('npsn');
192
            $sekolah->nama              = $request->input('nama');
193
            $sekolah->npsn              = $request->input('npsn');
194
            $sekolah->jenis_sekolah_id  = $request->input('jenis_sekolah_id');
195
            $sekolah->alamat            = $request->input('alamat');
196
            $sekolah->logo              = $request->input('logo');
197
            $sekolah->foto_gedung       = $request->input('foto_gedung');
198
            $sekolah->province_id       = $request->input('province_id');
199
            $sekolah->city_id           = $request->input('city_id');
200
            $sekolah->district_id       = $request->input('district_id');
201
            $sekolah->village_id        = $request->input('village_id');
202
            $sekolah->no_telp           = $request->input('no_telp');
203
            $sekolah->email             = $request->input('email');
204
            $sekolah->uuid              = $request->input('uuid');
205
            $sekolah->kode_zona         = $request->input('kode_zona');
206
            $sekolah->user_id           = $request->input('user_id');
207
            $sekolah->save();
208
209
            $error      = false;
210
            $message    = 'Success';
211
        }
212
213
        $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...
214
        $response['message']    = $message;
215
        $response['status']     = true;
216
217
        return response()->json($response);
218
    }
219
220
    /**
221
     * Display the specified resource.
222
     *
223
     * @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...
224
     * @return \Illuminate\Http\Response
225
     */
226 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...
227
    {
228
        $sekolah = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->findOrFail($id);
229
230
        $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...
231
        $response['error']      = false;
232
        $response['message']    = 'Success';
233
        $response['status']     = true;
234
235
        return response()->json($response);
236
    }
237
238
    /**
239
     * Show the form for editing the specified resource.
240
     *
241
     * @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...
242
     * @return \Illuminate\Http\Response
243
     */
244
    public function edit($id)
245
    {
246
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
247
        $sekolah        = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->findOrFail($id);
248
        $provinces      = $this->province->getAttributes();
249
        $cities         = $this->city->getAttributes();
250
        $districts      = $this->district->getAttributes();
251
        $villages       = $this->village->getAttributes();
252
        $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...
253
        $users_special  = $this->user->all();
254
        $users_standar  = $this->user->findOrFail($user_id);
255
        $current_user   = Auth::User();
256
257
        if ($sekolah->province !== null) {
258
            array_set($sekolah->province, 'label', $sekolah->province->name);
259
        }
260
261
        if ($sekolah->city !== null) {
262
            array_set($sekolah->city, 'label', $sekolah->city->name);
263
        }
264
265
        if ($sekolah->district !== null) {
266
            array_set($sekolah->district, 'label', $sekolah->district->name);
267
        }
268
269
        if ($sekolah->village !== null) {
270
            array_set($sekolah->village, 'label', $sekolah->village->name);
271
        }
272
273
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
274
275
        if ($sekolah->user !== null) {
276
            array_set($sekolah->user, 'label', $sekolah->user->name);
277
        }
278
279
        if ($role_check) {
280
            $user_special = true;
281
282
            foreach($users_special as $user){
283
                array_set($user, 'label', $user->name);
284
            }
285
286
            $users = $users_special;
287
        } else {
288
            $user_special = false;
289
290
            array_set($users_standar, 'label', $users_standar->name);
291
292
            $users = $users_standar;
293
        }
294
295
        array_set($current_user, 'label', $current_user->name);
296
297
        $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...
298
        $response['provinces']      = $provinces;
299
        $response['cities']         = $cities;
300
        $response['districts']      = $districts;
301
        $response['villages']       = $villages;
302
        $response['users']          = $users;
303
        $response['user_special']   = $user_special;
304
        $response['current_user']   = $current_user;
305
        $response['error']          = false;
306
        $response['message']        = 'Success';
307
        $response['status']         = true;
308
309
        return response()->json($response);
310
    }
311
312
    /**
313
     * Update the specified resource in storage.
314
     *
315
     * @param  \Illuminate\Http\Request  $request
316
     * @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...
317
     * @return \Illuminate\Http\Response
318
     */
319
    public function update(Request $request, $id)
320
    {
321
        $sekolah = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->findOrFail($id);
322
323
        $validator = Validator::make($request->all(), [
324
            'nama'              => 'required|max:255',
325
            'npsn'              => "required|between:4,17|unique:{$this->sekolah->getTable()},npsn,{$id},id,deleted_at,NULL",
326
            'jenis_sekolah_id'  => "required|exists:{$this->jenis_sekolah->getTable()},id",
327
            'alamat'            => 'required|max:255',
328
            'logo'              => 'max:255',
329
            'foto_gedung'       => 'max:255',
330
            'province_id'       => "required|exists:{$this->province->getTable()},id",
331
            'city_id'           => "required|exists:{$this->city->getTable()},id",
332
            'district_id'       => "required|exists:{$this->district->getTable()},id",
333
            'village_id'        => "required|exists:{$this->village->getTable()},id",
334
            'no_telp'           => 'required|digits_between:10,12',
335
            'email'             => 'required|email|max:255',
336
            'uuid'              => "required|unique:{$this->sekolah->getTable()},uuid,{$id},id,deleted_at,NULL",
337
            'kode_zona'         => "required|exists:{$this->master_zona->getTable()},id",
338
            'user_id'           => "required|exists:{$this->user->getTable()},id",
339
        ]);
340
341 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...
342
            $error      = true;
343
            $message    = $validator->errors()->first();
344
        } else {
345
            $sekolah->id                = $request->input('npsn');
346
            $sekolah->nama              = $request->input('nama');
347
            $sekolah->npsn              = $request->input('npsn');
348
            $sekolah->jenis_sekolah_id  = $request->input('jenis_sekolah_id');
349
            $sekolah->alamat            = $request->input('alamat');
350
            $sekolah->logo              = $request->input('logo');
351
            $sekolah->foto_gedung       = $request->input('foto_gedung');
352
            $sekolah->province_id       = $request->input('province_id');
353
            $sekolah->city_id           = $request->input('city_id');
354
            $sekolah->district_id       = $request->input('district_id');
355
            $sekolah->village_id        = $request->input('village_id');
356
            $sekolah->no_telp           = $request->input('no_telp');
357
            $sekolah->email             = $request->input('email');
358
            $sekolah->uuid              = $request->input('uuid');
359
            $sekolah->kode_zona         = $request->input('kode_zona');
360
            $sekolah->user_id           = $request->input('user_id');
361
            $sekolah->save();
362
363
            $error      = false;
364
            $message    = 'Success';
365
        }
366
367
        $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...
368
        $response['message']    = $message;
369
        $response['status']     = true;
370
371
        return response()->json($response);
372
    }
373
374
    /**
375
     * Remove the specified resource from storage.
376
     *
377
     * @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...
378
     * @return \Illuminate\Http\Response
379
     */
380 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...
381
    {
382
        $sekolah = $this->sekolah->findOrFail($id);
383
384
        if ($sekolah->delete()) {
385
            $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...
386
            $response['success']    = true;
387
            $response['status']     = true;
388
        } else {
389
            $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...
390
            $response['success']    = false;
391
            $response['status']     = false;
392
        }
393
394
        return json_encode($response);
395
    }
396
}
397