Completed
Push — master ( a66978...a49c04 )
by
unknown
10s
created

ZonaController::index()   C

Complexity

Conditions 9
Paths 25

Size

Total Lines 53
Code Lines 33

Duplication

Lines 11
Ratio 20.75 %

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 25
nop 1
dl 11
loc 53
rs 6.8963
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace Bantenprov\Zona\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\DB;
9
use Bantenprov\Zona\Facades\ZonaFacade;
10
11
/* Models */
12
use Bantenprov\Zona\Models\Bantenprov\Zona\Zona;
13
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
14
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
15
use App\User;
16
use Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai;
17
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah;
18
19
/* Etc */
20
use Validator;
21
use Auth;
22
23
/**
24
 * The ZonaController class.
25
 *
26
 * @package Bantenprov\Zona
27
 * @author  bantenprov <[email protected]>
28
 */
29
class ZonaController extends Controller
30
{
31
    protected $zona;
32
    protected $siswa;
33
    protected $sekolah;
34
    protected $user;
35
    protected $nilai;
36
    protected $admin_sekolah;
37
38
    /**
39
     * Create a new controller instance.
40
     *
41
     * @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...
42
     */
43
    public function __construct()
44
    {
45
        $this->zona          = new Zona;
46
        $this->siswa         = new Siswa;
47
        $this->sekolah       = new Sekolah;
48
        $this->user          = new User;
49
        $this->nilai         = new Nilai;
50
        $this->admin_sekolah = new AdminSekolah;
51
    }
52
53
    /**
54
     * Display a listing of the resource.
55
     *
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function index(Request $request)
59
    {
60
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
61
62
        if(is_null($admin_sekolah) && $this->checkRole(['superadministrator']) === false){
63
            $response = [];
64
            return response()->json($response)
65
            ->header('Access-Control-Allow-Origin', '*')
66
            ->header('Access-Control-Allow-Methods', 'GET');
67
        }
68
        
69
        if (request()->has('sort')) {
70
            list($sortCol, $sortDir) = explode('|', request()->sort);
71
72
            if($this->checkRole(['superadministrator'])){
73
                $query = $this->zona->orderBy($sortCol, $sortDir);
74
            }else{
75
                $query = $this->zona->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy($sortCol, $sortDir);
76
            }
77
        } else {
78
            if($this->checkRole(['superadministrator'])){
79
                $query = $this->zona->orderBy('id', 'asc');
80
            }else{
81
                $query = $this->zona->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy('id', 'asc');            
82
            }
83
        }
84
85
        if ($request->exists('filter')) {
86
            if($this->checkRole(['superadministrator'])){
87 View Code Duplication
                $query->where(function($q) use($request) {
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...
88
                    $value = "%{$request->filter}%";
89
90
                    $q->where('sekolah_id', 'like', $value)
91
                        ->orWhere('admin_sekolah_id', 'like', $value);
92
                });
93
            }else{
94 View Code Duplication
                $query->where(function($q) use($request, $admin_sekolah) {
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...
95
                    $value = "%{$request->filter}%";
96
97
                    $q->where('sekolah_id', $admin_sekolah->sekolah_id)->where('sekolah_id', 'like', $value);
98
                });
99
            }
100
101
        }
102
103
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
104
105
        $response   = $query->with(['siswa', 'sekolah', 'user'])->paginate($perPage);
106
107
        return response()->json($response)
108
            ->header('Access-Control-Allow-Origin', '*')
109
            ->header('Access-Control-Allow-Methods', 'GET');
110
    }
111
112
    /**
113
     * Display a listing of the resource.
114
     *
115
     * @return \Illuminate\Http\Response
116
     */
117 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...
118
    {
119
        $zonas = $this->zona->with(['siswa', 'sekolah', 'user'])->get();
120
121
        $response['zonas']      = $zonas;
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...
122
        $response['error']      = false;
123
        $response['message']    = 'Success';
124
        $response['status']     = true;
125
126
        return response()->json($response);
127
    }
128
129
    /**
130
     * Show the form for creating a new resource.
131
     *
132
     * @return \Illuminate\Http\Response
133
     */
134
    public function create()
135
    {
136
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
137
        $zona           = $this->zona->getAttributes();
138
        //$siswas         = $this->siswa->getAttributes();
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
139
        $sekolahs       = $this->sekolah->getAttributes();
140
        $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...
141
        $users_special  = $this->user->all();
142
        $users_standar  = $this->user->findOrFail($user_id);
143
        $current_user   = Auth::User();
144
145
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
146
147
        if($this->checkRole(['superadministrator'])){
148
            $siswas = $this->siswa->all();
149
        }else{
150
            $sekolah_id = $admin_sekolah->sekolah_id;
151
            $siswas     = $this->siswa->where('sekolah_id', $sekolah_id)->get();
152
        }
153
154
        foreach ($siswas as $siswa) {
155
            array_set($siswa, 'label', $siswa->nomor_un.' - '.$siswa->nama_siswa);
156
        }
157
158
        foreach ($sekolahs as $sekolah) {
159
            array_set($sekolah, 'label', $sekolah->nama);
160
        }
161
162
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
163
164 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...
165
            $user_special = true;
166
167
            foreach ($users_special as $user) {
168
                array_set($user, 'label', $user->name);
169
            }
170
171
            $users = $users_special;
172
        } else {
173
            $user_special = false;
174
175
            array_set($users_standar, 'label', $users_standar->name);
176
177
            $users = $users_standar;
178
        }
179
180
        array_set($current_user, 'label', $current_user->name);
181
182
        $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...
183
        $response['siswa']          = $siswas;
184
        $response['sekolahs']       = $sekolahs;
185
        $response['users']          = $users;
186
        $response['user_special']   = $user_special;
187
        $response['current_user']   = $current_user;
188
        $response['error']          = false;
189
        $response['message']        = 'Success';
190
        $response['status']         = true;
191
192
        return response()->json($response);
193
    }
194
195
    /**
196
     * Store a newly created resource in storage.
197
     *
198
     * @param  \Illuminate\Http\Request  $request
199
     * @return \Illuminate\Http\Response
200
     */
201
    public function store(Request $request)
202
    {
203
        $zona = $this->zona;
204
205
        $validator = Validator::make($request->all(), [
206
            'nomor_un'          => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,NULL,id,deleted_at,NULL",
207
            // '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...
208
            // '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...
209
            // '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...
210
            // '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...
211
            // '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...
212
            // 'nilai'             => '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...
213
            'user_id'           => "required|exists:{$this->user->getTable()},id",
214
        ]);
215
216 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...
217
            $error      = true;
218
            $message    = $validator->errors()->first();
219
        } else {
220
            $nomor_un       = $request->input('nomor_un');
221
            $siswa          = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first();
222
            $zona_siswa     = substr($siswa->village_id, 0, 6);
223
            $zona_sekolah   = substr($siswa->sekolah->village_id, 0, 6);
224
            $lokasi_siswa   = $siswa->village_id;
225
            $lokasi_sekolah = $siswa->sekolah->village_id;
226
227
            $zona->nomor_un         = $nomor_un;
228
            $zona->sekolah_id       = $siswa->sekolah->id;
229
            $zona->zona_siswa       = $zona_siswa;
230
            $zona->zona_sekolah     = $zona_sekolah;
231
            $zona->lokasi_siswa     = $lokasi_siswa;
232
            $zona->lokasi_sekolah   = $lokasi_sekolah;
233
            $zona->nilai            = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah);
234
            $zona->user_id          = $request->input('user_id');
235
236
            $nilai = $this->nilai->updateOrCreate(
237
                [
238
                    'nomor_un'  => $zona->nomor_un,
239
                ],
240
                [
241
                    'nomor_un'  => $zona->nomor_un,
242
                    'zona'      => $zona->nilai,
243
                    'total'     => null,
244
                    'user_id'   => $zona->user_id,
245
                ]
246
            );
247
248
            DB::beginTransaction();
249
250
            if ($zona->save() && $nilai->save())
251
            {
252
                DB::commit();
253
254
                $error      = false;
255
                $message    = 'Success';
256
            } else {
257
                DB::rollBack();
258
259
                $error      = true;
260
                $message    = 'Failed';
261
            }
262
        }
263
264
        $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...
265
        $response['error']      = $error;
266
        $response['message']    = $message;
267
        $response['status']     = true;
268
269
        return response()->json($response);
270
    }
271
272
    /**
273
     * Display the specified resource.
274
     *
275
     * @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...
276
     * @return \Illuminate\Http\Response
277
     */
278 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...
279
    {
280
        $zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
281
282
        $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...
283
        $response['error']      = false;
284
        $response['message']    = 'Success';
285
        $response['status']     = true;
286
287
        return response()->json($response);
288
    }
289
290
    /**
291
     * Show the form for editing the specified resource.
292
     *
293
     * @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...
294
     * @return \Illuminate\Http\Response
295
     */
296
    public function edit($id)
297
    {
298
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
299
        $zona           = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
300
        $siswas         = $this->siswa->getAttributes();
301
        $sekolahs       = $this->sekolah->getAttributes();
302
        $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...
303
        $users_special  = $this->user->all();
304
        $users_standar  = $this->user->findOrFail($user_id);
305
        $current_user   = Auth::User();
306
307
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
308
309
        if ($zona->user !== null) {
310
            array_set($zona->user, 'label', $zona->user->name);
311
        }
312
313 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...
314
            $user_special = true;
315
316
            foreach ($users_special as $user) {
317
                array_set($user, 'label', $user->name);
318
            }
319
320
            $users = $users_special;
321
        } else {
322
            $user_special = false;
323
324
            array_set($users_standar, 'label', $users_standar->name);
325
326
            $users = $users_standar;
327
        }
328
329
        array_set($current_user, 'label', $current_user->name);
330
331
        $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...
332
        $response['siswas']         = $siswas;
333
        $response['sekolahs']       = $sekolahs;
334
        $response['users']          = $users;
335
        $response['user_special']   = $user_special;
336
        $response['current_user']   = $current_user;
337
        $response['error']          = false;
338
        $response['message']        = 'Success';
339
        $response['status']         = true;
340
341
        return response()->json($response);
342
    }
343
344
    /**
345
     * Update the specified resource in storage.
346
     *
347
     * @param  \Illuminate\Http\Request  $request
348
     * @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...
349
     * @return \Illuminate\Http\Response
350
     */
351
    public function update(Request $request, $id)
352
    {
353
        $zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
354
355
        $validator = Validator::make($request->all(), [
356
            // 'nomor_un'          => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->zona->getTable()},nomor_un,{$id},id,deleted_at,NULL",
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...
357
            // '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...
358
            // '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...
359
            // '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...
360
            // '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...
361
            // '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...
362
            // 'nilai'             => '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...
363
            'user_id'           => "required|exists:{$this->user->getTable()},id",
364
        ]);
365
366 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...
367
            $error      = true;
368
            $message    = $validator->errors()->first();
369
        } else {
370
            $nomor_un       = $zona->nomor_un; // $request->input('nomor_un');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% 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...
371
            $siswa          = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first();
372
            $zona_siswa     = substr($siswa->village_id, 0, 6);
373
            $zona_sekolah   = substr($siswa->sekolah->village_id, 0, 6);
374
            $lokasi_siswa   = $siswa->village_id;
375
            $lokasi_sekolah = $siswa->sekolah->village_id;
376
377
            $zona->nomor_un         = $nomor_un;
378
            $zona->sekolah_id       = $siswa->sekolah->id;
379
            $zona->zona_siswa       = $zona_siswa;
380
            $zona->zona_sekolah     = $zona_sekolah;
381
            $zona->lokasi_siswa     = $lokasi_siswa;
382
            $zona->lokasi_sekolah   = $lokasi_sekolah;
383
            $zona->nilai            = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah);
384
            $zona->user_id          = $request->input('user_id');
385
386
            $nilai = $this->nilai->updateOrCreate(
387
                [
388
                    'nomor_un'  => $zona->nomor_un,
389
                ],
390
                [
391
                    'nomor_un'  => $zona->nomor_un,
392
                    'zona'      => $zona->nilai,
393
                    'total'     => null,
394
                    'user_id'   => $zona->user_id,
395
                ]
396
            );
397
398
            DB::beginTransaction();
399
400
            if ($zona->save() && $nilai->save())
401
            {
402
                DB::commit();
403
404
                $error      = false;
405
                $message    = 'Success';
406
            } else {
407
                DB::rollBack();
408
409
                $error      = true;
410
                $message    = 'Failed';
411
            }
412
        }
413
414
        $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...
415
        $response['error']      = $error;
416
        $response['message']    = $message;
417
        $response['status']     = true;
418
419
        return response()->json($response);
420
    }
421
422
    /**
423
     * Remove the specified resource from storage.
424
     *
425
     * @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...
426
     * @return \Illuminate\Http\Response
427
     */
428 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...
429
    {
430
        $zona = $this->zona->findOrFail($id);
431
432
        if ($zona->delete()) {
433
            $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...
434
            $response['success']    = true;
435
            $response['status']     = true;
436
        } else {
437
            $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...
438
            $response['success']    = false;
439
            $response['status']     = false;
440
        }
441
442
        return json_encode($response);
443
    }
444
445
    protected function checkRole($role = array())
446
    {
447
        return Auth::user()->hasRole($role);
448
    }
449
}
450