Completed
Push — master ( b49e4b...955280 )
by
unknown
10s
created

ZonaController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 47
Code Lines 31

Duplication

Lines 24
Ratio 51.06 %

Importance

Changes 0
Metric Value
cc 2
eloc 31
nc 2
nop 2
dl 24
loc 47
rs 9.0303
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\Zona\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Zona\Facades\ZonaFacade;
9
10
/* Models */
11
use Bantenprov\Zona\Models\Bantenprov\Zona\Zona;
12
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
13
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
14
use App\User;
15
16
/* Etc */
17
use Validator;
18
use Auth;
19
20
/**
21
 * The ZonaController class.
22
 *
23
 * @package Bantenprov\Zona
24
 * @author  bantenprov <[email protected]>
25
 */
26
class ZonaController extends Controller
27
{
28
    protected $zona;
29
    protected $siswa;
30
    protected $sekolah;
31
    protected $user;
32
33
    /**
34
     * Create a new controller instance.
35
     *
36
     * @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...
37
     */
38
    public function __construct()
39
    {
40
        $this->zona     = new Zona;
41
        $this->siswa    = new Siswa;
42
        $this->sekolah  = new Sekolah;
43
        $this->user     = new User;
44
    }
45
46
    /**
47
     * Display a listing of the resource.
48
     *
49
     * @return \Illuminate\Http\Response
50
     */
51
    public function index(Request $request)
52
    {
53 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...
54
            list($sortCol, $sortDir) = explode('|', request()->sort);
55
56
            $query = $this->zona->orderBy($sortCol, $sortDir);
57
        } else {
58
            $query = $this->zona->orderBy('id', 'asc');
59
        }
60
61
        if ($request->exists('filter')) {
62
            $query->where(function($q) use($request) {
63
                $value = "%{$request->filter}%";
64
65
                $q->where('nomor_un', 'like', $value)
66
                    ->orWhere('lokasi_siswa', 'like', $value)
67
                    ->orWhere('lokasi_sekolah', 'like', $value)
68
                    ->orWhere('nilai_zona', 'like', $value);
69
            });
70
        }
71
72
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
73
74
        $response   = $query->with(['siswa', 'sekolah', 'user'])->paginate($perPage);
75
76
        return response()->json($response)
77
            ->header('Access-Control-Allow-Origin', '*')
78
            ->header('Access-Control-Allow-Methods', 'GET');
79
    }
80
81
    /**
82
     * Show the form for creating a new resource.
83
     *
84
     * @return \Illuminate\Http\Response
85
     */
86
    public function create()
87
    {
88
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
89
        $zona           = $this->zona->getAttributes();
90
        $siswas         = $this->siswa->getAttributes();
91
        $sekolahs       = $this->sekolah->getAttributes();
92
        $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...
93
        $users_special  = $this->user->all();
94
        $users_standar  = $this->user->findOrFail($user_id);
95
        $current_user   = Auth::User();
96
97
        foreach($siswas as $siswa){
98
            array_set($siswa, 'label', $siswa->nama_siswa);
99
        }
100
101
        foreach($sekolahs as $sekolah){
102
            array_set($sekolah, 'label', $sekolah->nama);
103
        }
104
105
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
106
107 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...
108
            $user_special = true;
109
110
            foreach($users_special as $user){
111
                array_set($user, 'label', $user->name);
112
            }
113
114
            $users = $users_special;
115
        }else{
116
            $user_special = false;
117
118
            array_set($users_standar, 'label', $users_standar->name);
119
120
            $users = $users_standar;
121
        }
122
123
        array_set($current_user, 'label', $current_user->name);
124
125
        $response['zona']           = $zona;
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...
126
        $response['siswas']         = $siswas;
127
        $response['sekolahs']       = $sekolahs;
128
        $response['users']          = $users;
129
        $response['user_special']   = $user_special;
130
        $response['current_user']   = $current_user;
131
        $response['error']          = false;
132
        $response['message']        = 'Success';
133
        $response['status']         = true;
134
135
        return response()->json($response);
136
    }
137
138
    /**
139
     * Store a newly created resource in storage.
140
     *
141
     * @param  \Illuminate\Http\Request  $request
142
     * @return \Illuminate\Http\Response
143
     */
144
    public function store(Request $request)
145
    {
146
        $zona = $this->zona;
147
148
        $validator = Validator::make($request->all(), [
149
            'nomor_un'          => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,NULL,id,deleted_at,NULL",
150
            // 'sekolah_id'        => "required|exists:{$this->sekolah->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
151
            // 'zona_siswa'     => "required|exists:{$this->city->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
152
            // 'zona_sekolah'   => "required|exists:{$this->village->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
153
            // 'lokasi_siswa'   => "required|exists:{$this->district->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
154
            // 'lokasi_sekolah' => "required|exists:{$this->village->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
155
            // 'nilai_zona'        => 'required|numeric',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
156
            'user_id'           => "required|exists:{$this->user->getTable()},id",
157
        ]);
158
159 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...
160
            $error      = true;
161
            $message    = $validator->errors()->first();
162
        } else {
163
            $nomor_un       = $request->input('nomor_un');
164
            $siswa          = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first();
165
            $zona_siswa     = substr($siswa->village_id, 0, 6);
166
            $zona_sekolah   = substr($siswa->sekolah->village_id, 0, 6);
167
            $lokasi_siswa   = $siswa->village_id;
168
            $lokasi_sekolah = $siswa->sekolah->village_id;
169
170
            $zona->nomor_un          = $nomor_un;
171
            $zona->sekolah_id        = $siswa->sekolah->id;
172
            $zona->zona_siswa        = $zona_siswa;
173
            $zona->zona_sekolah      = $zona_sekolah;
174
            $zona->lokasi_siswa      = $lokasi_siswa;
175
            $zona->lokasi_sekolah    = $lokasi_sekolah;
176
            $zona->nilai_zona        = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah);
177
            $zona->user_id           = $request->input('user_id');
178
            $zona->save();
179
180
            $error      = false;
181
            $message    = 'Success';
182
        }
183
184
        $response['zona']       = $zona;
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...
185
        $response['error']      = $error;
186
        $response['message']    = $message;
187
        $response['status']     = true;
188
189
        return response()->json($response);
190
    }
191
192
    /**
193
     * Display the specified resource.
194
     *
195
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
196
     * @return \Illuminate\Http\Response
197
     */
198 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...
199
    {
200
        $zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
201
202
        $response['zona']       = $zona;
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...
203
        $response['error']      = false;
204
        $response['message']    = 'Success';
205
        $response['status']     = true;
206
207
        return response()->json($response);
208
    }
209
210
    /**
211
     * Show the form for editing the specified resource.
212
     *
213
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
214
     * @return \Illuminate\Http\Response
215
     */
216
    public function edit($id)
217
    {
218
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
219
        $zona           = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
220
        $siswas         = $this->siswa->getAttributes();
221
        $sekolahs       = $this->sekolah->getAttributes();
222
        $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...
223
        $users_special  = $this->user->all();
224
        $users_standar  = $this->user->findOrFail($user_id);
225
        $current_user   = Auth::User();
226
227
        if ($zona->siswa !== null) {
228
            array_set($zona->siswa, 'label', $zona->siswa->nama_siswa);
229
        }
230
231
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
232
233
        if ($zona->user !== null) {
234
            array_set($zona->user, 'label', $zona->user->name);
235
        }
236
237 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...
238
            $user_special = true;
239
240
            foreach($users_special as $user){
241
                array_set($user, 'label', $user->name);
242
            }
243
244
            $users = $users_special;
245
        }else{
246
            $user_special = false;
247
248
            array_set($users_standar, 'label', $users_standar->name);
249
250
            $users = $users_standar;
251
        }
252
253
        array_set($current_user, 'label', $current_user->name);
254
255
        $response['zona']           = $zona;
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...
256
        $response['siswas']         = $siswas;
257
        $response['sekolahs']       = $sekolahs;
258
        $response['users']          = $users;
259
        $response['user_special']   = $user_special;
260
        $response['current_user']   = $current_user;
261
        $response['error']          = false;
262
        $response['message']        = 'Success';
263
        $response['status']         = true;
264
265
        return response()->json($response);
266
    }
267
268
    /**
269
     * Update the specified resource in storage.
270
     *
271
     * @param  \Illuminate\Http\Request  $request
272
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
273
     * @return \Illuminate\Http\Response
274
     */
275
    public function update(Request $request, $id)
276
    {
277
        $zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
278
279
        $validator = Validator::make($request->all(), [
280
            'nomor_un'          => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,{$id},id,deleted_at,NULL",
281
            // 'sekolah_id'        => "required|exists:{$this->sekolah->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
282
            // 'zona_siswa'     => "required|exists:{$this->city->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
283
            // 'zona_sekolah'   => "required|exists:{$this->village->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
284
            // 'lokasi_siswa'   => "required|exists:{$this->district->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
285
            // 'lokasi_sekolah' => "required|exists:{$this->village->getTable()},id",
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
286
            // 'nilai_zona'        => 'required|numeric',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
287
            'user_id'           => "required|exists:{$this->user->getTable()},id",
288
        ]);
289
290 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...
291
            $error      = true;
292
            $message    = $validator->errors()->first();
293
        } else {
294
            $nomor_un       = $request->input('nomor_un');
295
            $siswa          = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first();
296
            $zona_siswa     = substr($siswa->village_id, 0, 6);
297
            $zona_sekolah   = substr($siswa->sekolah->village_id, 0, 6);
298
            $lokasi_siswa   = $siswa->village_id;
299
            $lokasi_sekolah = $siswa->sekolah->village_id;
300
301
            $zona->nomor_un          = $nomor_un;
302
            $zona->sekolah_id        = $siswa->sekolah->id;
303
            $zona->zona_siswa        = $zona_siswa;
304
            $zona->zona_sekolah      = $zona_sekolah;
305
            $zona->lokasi_siswa      = $lokasi_siswa;
306
            $zona->lokasi_sekolah    = $lokasi_sekolah;
307
            $zona->nilai_zona        = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah);
308
            $zona->user_id           = $request->input('user_id');
309
            $zona->save();
310
311
            $error      = false;
312
            $message    = 'Success';
313
        }
314
315
        $response['zona']       = $zona;
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...
316
        $response['error']      = $error;
317
        $response['message']    = $message;
318
        $response['status']     = true;
319
320
        return response()->json($response);
321
    }
322
323
    /**
324
     * Remove the specified resource from storage.
325
     *
326
     * @param  \App\Zona  $zona
0 ignored issues
show
Bug introduced by
There is no parameter named $zona. 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...
327
     * @return \Illuminate\Http\Response
328
     */
329 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...
330
    {
331
        $zona = $this->zona->findOrFail($id);
332
333
        if ($zona->delete()) {
334
            $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...
335
            $response['success']    = true;
336
            $response['status']     = true;
337
        } else {
338
            $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...
339
            $response['success']    = false;
340
            $response['status']     = false;
341
        }
342
343
        return json_encode($response);
344
    }
345
}
346