Issues (76)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Http/Controllers/ZonaController.php (39 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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
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
$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
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
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
                    'kegiatan_id'   => null,
244
                    'total'         => null,
245
                    'user_id'       => $zona->user_id,
246
                ]
247
            );
248
249
            DB::beginTransaction();
250
251
            if ($zona->save() && $nilai->save())
252
            {
253
                DB::commit();
254
255
                $error      = false;
256
                $message    = 'Success';
257
            } else {
258
                DB::rollBack();
259
260
                $error      = true;
261
                $message    = 'Failed';
262
            }
263
        }
264
265
        $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...
266
        $response['error']      = $error;
267
        $response['message']    = $message;
268
        $response['status']     = true;
269
270
        return response()->json($response);
271
    }
272
273
    /**
274
     * Display the specified resource.
275
     *
276
     * @param  \App\Zona  $zona
0 ignored issues
show
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...
277
     * @return \Illuminate\Http\Response
278
     */
279 View Code Duplication
    public function show($id)
0 ignored issues
show
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...
280
    {
281
        $zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
282
283
        $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...
284
        $response['error']      = false;
285
        $response['message']    = 'Success';
286
        $response['status']     = true;
287
288
        return response()->json($response);
289
    }
290
291
    /**
292
     * Show the form for editing the specified resource.
293
     *
294
     * @param  \App\Zona  $zona
0 ignored issues
show
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...
295
     * @return \Illuminate\Http\Response
296
     */
297
    public function edit($id)
298
    {
299
        $user_id        = isset(Auth::User()->id) ? Auth::User()->id : null;
300
        $zona           = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
301
        $siswas         = $this->siswa->getAttributes();
302
        $sekolahs       = $this->sekolah->getAttributes();
303
        $users          = $this->user->getAttributes();
0 ignored issues
show
$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...
304
        $users_special  = $this->user->all();
305
        $users_standar  = $this->user->findOrFail($user_id);
306
        $current_user   = Auth::User();
307
308
        $role_check = Auth::User()->hasRole(['superadministrator','administrator']);
309
310
        if ($zona->user !== null) {
311
            array_set($zona->user, 'label', $zona->user->name);
312
        }
313
314 View Code Duplication
        if ($role_check) {
0 ignored issues
show
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...
315
            $user_special = true;
316
317
            foreach ($users_special as $user) {
318
                array_set($user, 'label', $user->name);
319
            }
320
321
            $users = $users_special;
322
        } else {
323
            $user_special = false;
324
325
            array_set($users_standar, 'label', $users_standar->name);
326
327
            $users = $users_standar;
328
        }
329
330
        array_set($current_user, 'label', $current_user->name);
331
332
        $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...
333
        $response['siswas']         = $siswas;
334
        $response['sekolahs']       = $sekolahs;
335
        $response['users']          = $users;
336
        $response['user_special']   = $user_special;
337
        $response['current_user']   = $current_user;
338
        $response['error']          = false;
339
        $response['message']        = 'Success';
340
        $response['status']         = true;
341
342
        return response()->json($response);
343
    }
344
345
    /**
346
     * Update the specified resource in storage.
347
     *
348
     * @param  \Illuminate\Http\Request  $request
349
     * @param  \App\Zona  $zona
0 ignored issues
show
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...
350
     * @return \Illuminate\Http\Response
351
     */
352
    public function update(Request $request, $id)
353
    {
354
        $zona = $this->zona->with(['siswa', 'sekolah', 'user'])->findOrFail($id);
355
356
        $validator = Validator::make($request->all(), [
357
            // '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...
358
            // '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...
359
            // '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...
360
            // '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...
361
            // '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...
362
            // '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...
363
            // '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...
364
            'user_id'           => "required|exists:{$this->user->getTable()},id",
365
        ]);
366
367 View Code Duplication
        if ($validator->fails()) {
0 ignored issues
show
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...
368
            $error      = true;
369
            $message    = $validator->errors()->first();
370
        } else {
371
            $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...
372
            $siswa          = $this->siswa->where('nomor_un', $nomor_un)->with(['sekolah'])->first();
373
            $zona_siswa     = substr($siswa->village_id, 0, 6);
374
            $zona_sekolah   = substr($siswa->sekolah->village_id, 0, 6);
375
            $lokasi_siswa   = $siswa->village_id;
376
            $lokasi_sekolah = $siswa->sekolah->village_id;
377
378
            $zona->nomor_un         = $nomor_un;
379
            $zona->sekolah_id       = $siswa->sekolah->id;
380
            $zona->zona_siswa       = $zona_siswa;
381
            $zona->zona_sekolah     = $zona_sekolah;
382
            $zona->lokasi_siswa     = $lokasi_siswa;
383
            $zona->lokasi_sekolah   = $lokasi_sekolah;
384
            $zona->nilai            = $this->zona->nilai($lokasi_siswa, $lokasi_sekolah);
385
            $zona->user_id          = $request->input('user_id');
386
387
            $nilai = $this->nilai->updateOrCreate(
388
                [
389
                    'nomor_un'  => $zona->nomor_un,
390
                ],
391
                [
392
                    'nomor_un'      => $zona->nomor_un,
393
                    'zona'          => $zona->nilai,
394
                    'kegiatan_id'   => null,
395
                    'total'         => null,
396
                    'user_id'       => $zona->user_id,
397
                ]
398
            );
399
400
            DB::beginTransaction();
401
402
            if ($zona->save() && $nilai->save())
403
            {
404
                DB::commit();
405
406
                $error      = false;
407
                $message    = 'Success';
408
            } else {
409
                DB::rollBack();
410
411
                $error      = true;
412
                $message    = 'Failed';
413
            }
414
        }
415
416
        $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...
417
        $response['error']      = $error;
418
        $response['message']    = $message;
419
        $response['status']     = true;
420
421
        return response()->json($response);
422
    }
423
424
    /**
425
     * Remove the specified resource from storage.
426
     *
427
     * @param  \App\Zona  $zona
0 ignored issues
show
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...
428
     * @return \Illuminate\Http\Response
429
     */
430 View Code Duplication
    public function destroy($id)
0 ignored issues
show
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...
431
    {
432
        $zona = $this->zona->findOrFail($id);
433
434
        if ($zona->delete()) {
435
            $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...
436
            $response['success']    = true;
437
            $response['status']     = true;
438
        } else {
439
            $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...
440
            $response['success']    = false;
441
            $response['status']     = false;
442
        }
443
444
        return json_encode($response);
445
    }
446
447
    protected function checkRole($role = array())
448
    {
449
        return Auth::user()->hasRole($role);
450
    }
451
}
452