Completed
Push — master ( bccaf2...83a3cc )
by
unknown
10s
created

SekolahController::create()   B

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 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
        $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
            'kode_zona'         => "required|exists:{$this->master_zona->getTable()},id",
183
            'user_id'           => "required|exists:{$this->user->getTable()},id",
184
        ]);
185
186 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...
187
            $error      = true;
188
            $message    = $validator->errors()->first();
189
        } else {
190
            $sekolah->id                = $request->input('npsn');
191
            $sekolah->nama              = $request->input('nama');
192
            $sekolah->npsn              = $request->input('npsn');
193
            $sekolah->jenis_sekolah_id  = $request->input('jenis_sekolah_id');
194
            $sekolah->alamat            = $request->input('alamat');
195
            $sekolah->logo              = $request->input('logo');
196
            $sekolah->foto_gedung       = $request->input('foto_gedung');
197
            $sekolah->province_id       = $request->input('province_id');
198
            $sekolah->city_id           = $request->input('city_id');
199
            $sekolah->district_id       = $request->input('district_id');
200
            $sekolah->village_id        = $request->input('village_id');
201
            $sekolah->no_telp           = $request->input('no_telp');
202
            $sekolah->email             = $request->input('email');
203
            $sekolah->kode_zona         = $request->input('kode_zona');
204
            $sekolah->user_id           = $request->input('user_id');
205
            $sekolah->save();
206
207
            $error      = false;
208
            $message    = 'Success';
209
        }
210
211
        $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...
212
        $response['message']    = $message;
213
        $response['status']     = true;
214
215
        return response()->json($response);
216
    }
217
218
    /**
219
     * Display the specified resource.
220
     *
221
     * @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...
222
     * @return \Illuminate\Http\Response
223
     */
224 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...
225
    {
226
        $sekolah = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->findOrFail($id);
227
228
        $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...
229
        $response['error']      = false;
230
        $response['message']    = 'Success';
231
        $response['status']     = true;
232
233
        return response()->json($response);
234
    }
235
236
    /**
237
     * Show the form for editing the specified resource.
238
     *
239
     * @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...
240
     * @return \Illuminate\Http\Response
241
     */
242
    public function edit($id)
243
    {
244
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
245
        $sekolah        = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->findOrFail($id);
246
        $provinces      = $this->province->getAttributes();
247
        $cities         = $this->city->getAttributes();
248
        $districts      = $this->district->getAttributes();
249
        $villages       = $this->village->getAttributes();
250
        $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...
251
        $users_special  = $this->user->all();
252
        $users_standar  = $this->user->findOrFail($user_id);
253
        $current_user   = Auth::User();
254
255
        if ($sekolah->province !== null) {
256
            array_set($sekolah->province, 'label', $sekolah->province->name);
257
        }
258
259
        if ($sekolah->city !== null) {
260
            array_set($sekolah->city, 'label', $sekolah->city->name);
261
        }
262
263
        if ($sekolah->district !== null) {
264
            array_set($sekolah->district, 'label', $sekolah->district->name);
265
        }
266
267
        if ($sekolah->village !== null) {
268
            array_set($sekolah->village, 'label', $sekolah->village->name);
269
        }
270
271
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
272
273
        if ($sekolah->user !== null) {
274
            array_set($sekolah->user, 'label', $sekolah->user->name);
275
        }
276
277
        if ($role_check) {
278
            $user_special = true;
279
280
            foreach($users_special as $user){
281
                array_set($user, 'label', $user->name);
282
            }
283
284
            $users = $users_special;
285
        } else {
286
            $user_special = false;
287
288
            array_set($users_standar, 'label', $users_standar->name);
289
290
            $users = $users_standar;
291
        }
292
293
        array_set($current_user, 'label', $current_user->name);
294
295
        $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...
296
        $response['provinces']      = $provinces;
297
        $response['cities']         = $cities;
298
        $response['districts']      = $districts;
299
        $response['villages']       = $villages;
300
        $response['users']          = $users;
301
        $response['user_special']   = $user_special;
302
        $response['current_user']   = $current_user;
303
        $response['error']          = false;
304
        $response['message']        = 'Success';
305
        $response['status']         = true;
306
307
        return response()->json($response);
308
    }
309
310
    /**
311
     * Update the specified resource in storage.
312
     *
313
     * @param  \Illuminate\Http\Request  $request
314
     * @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...
315
     * @return \Illuminate\Http\Response
316
     */
317
    public function update(Request $request, $id)
318
    {
319
        $sekolah = $this->sekolah->with(['jenis_sekolah', 'province', 'city', 'district', 'village', 'master_zona', 'user'])->findOrFail($id);
320
321
        $validator = Validator::make($request->all(), [
322
            'nama'              => 'required|max:255',
323
            'npsn'              => "required|between:4,17|unique:{$this->sekolah->getTable()},npsn,{$id},id,deleted_at,NULL",
324
            'jenis_sekolah_id'  => "required|exists:{$this->jenis_sekolah->getTable()},id",
325
            'alamat'            => 'required|max:255',
326
            'logo'              => 'max:255',
327
            'foto_gedung'       => 'max:255',
328
            'province_id'       => "required|exists:{$this->province->getTable()},id",
329
            'city_id'           => "required|exists:{$this->city->getTable()},id",
330
            'district_id'       => "required|exists:{$this->district->getTable()},id",
331
            'village_id'        => "required|exists:{$this->village->getTable()},id",
332
            'no_telp'           => 'required|digits_between:10,12',
333
            'email'             => 'required|email|max:255',
334
            'kode_zona'         => "required|exists:{$this->master_zona->getTable()},id",
335
            'user_id'           => "required|exists:{$this->user->getTable()},id",
336
        ]);
337
338 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...
339
            $error      = true;
340
            $message    = $validator->errors()->first();
341
        } else {
342
            $sekolah->id                = $request->input('npsn');
343
            $sekolah->nama              = $request->input('nama');
344
            $sekolah->npsn              = $request->input('npsn');
345
            $sekolah->jenis_sekolah_id  = $request->input('jenis_sekolah_id');
346
            $sekolah->alamat            = $request->input('alamat');
347
            $sekolah->logo              = $request->input('logo');
348
            $sekolah->foto_gedung       = $request->input('foto_gedung');
349
            $sekolah->province_id       = $request->input('province_id');
350
            $sekolah->city_id           = $request->input('city_id');
351
            $sekolah->district_id       = $request->input('district_id');
352
            $sekolah->village_id        = $request->input('village_id');
353
            $sekolah->no_telp           = $request->input('no_telp');
354
            $sekolah->email             = $request->input('email');
355
            $sekolah->kode_zona         = $request->input('kode_zona');
356
            $sekolah->user_id           = $request->input('user_id');
357
            $sekolah->save();
358
359
            $error      = false;
360
            $message    = 'Success';
361
        }
362
363
        $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...
364
        $response['message']    = $message;
365
        $response['status']     = true;
366
367
        return response()->json($response);
368
    }
369
370
    /**
371
     * Remove the specified resource from storage.
372
     *
373
     * @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...
374
     * @return \Illuminate\Http\Response
375
     */
376 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...
377
    {
378
        $sekolah = $this->sekolah->findOrFail($id);
379
380
        if ($sekolah->delete()) {
381
            $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...
382
            $response['success']    = true;
383
            $response['status']     = true;
384
        } else {
385
            $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...
386
            $response['success']    = false;
387
            $response['status']     = false;
388
        }
389
390
        return json_encode($response);
391
    }
392
}
393