Completed
Push — master ( 9f015b...a148e1 )
by
unknown
14s queued 10s
created

PrestasiController::index()   C

Complexity

Conditions 9
Paths 25

Size

Total Lines 52
Code Lines 33

Duplication

Lines 11
Ratio 21.15 %

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 25
nop 1
dl 11
loc 52
rs 6.5703
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\Prestasi\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\Prestasi\Facades\PrestasiFacade;
10
11
/* Models */
12
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\Prestasi;
13
use Bantenprov\Prestasi\Models\Bantenprov\Prestasi\MasterPrestasi;
14
use Bantenprov\Siswa\Models\Bantenprov\Siswa\Siswa;
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 PrestasiController class.
25
 *
26
 * @package Bantenprov\Prestasi
27
 * @author  bantenprov <[email protected]>
28
 */
29
class PrestasiController extends Controller
30
{
31
    /**
32
     * Create a new controller instance.
33
     *
34
     * @return void
35
     */
36
    protected $prestasi;
37
    protected $siswa;
38
    protected $master_prestasi;
39
    protected $user;
40
    protected $nilai;
41
    protected $admin_sekolah;
42
43
    public function __construct()
44
    {
45
        $this->prestasi         = new Prestasi;
46
        $this->siswa            = new Siswa;
47
        $this->master_prestasi  = new MasterPrestasi;
48
        $this->user             = new User;
49
        $this->nilai            = new Nilai;
50
        $this->admin_sekolah            = new AdminSekolah;
51
52
    }
53
54
    /**
55
     * Display a listing of the resource.
56
     *
57
     * @return \Illuminate\Http\Response
58
     */
59
    public function index(Request $request)
60
    {
61
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
62
63
        if(is_null($admin_sekolah) && $this->checkRole(['superadministrator']) === false){
64
            $response = [];
65
            return response()->json($response)
66
            ->header('Access-Control-Allow-Origin', '*')
67
            ->header('Access-Control-Allow-Methods', 'GET');
68
        }
69
70
        if (request()->has('sort')) {
71
            list($sortCol, $sortDir) = explode('|', request()->sort);
72
73
            if($this->checkRole(['superadministrator'])){
74
                $query = $this->prestasi->orderBy($sortCol, $sortDir);
75
            }else{
76
                $query = $this->prestasi->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy($sortCol, $sortDir);
77
            }
78
        } else {
79
            if($this->checkRole(['superadministrator'])){
80
                $query = $this->prestasi->orderBy('id', 'asc');
81
            }else{
82
                $query = $this->prestasi->where('user_id', $admin_sekolah->admin_sekolah_id)->orderBy('id', 'asc');            
83
            }
84
        }
85
86
        if ($request->exists('filter')) {
87
            if($this->checkRole(['superadministrator'])){
88 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...
89
                    $value = "%{$request->filter}%";
90
91
                    $q->where('sekolah_id', 'like', $value)
92
                        ->orWhere('admin_sekolah_id', 'like', $value);
93
                });
94
            }else{
95 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...
96
                    $value = "%{$request->filter}%";
97
98
                    $q->where('sekolah_id', $admin_sekolah->sekolah_id)->where('sekolah_id', 'like', $value);
99
                });
100
            }
101
102
        }
103
104
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
105
        $response = $query->with('user')->with('master_prestasi')->with('siswa')->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
     * Show the form for creating a new resource.
114
     *
115
     * @return \Illuminate\Http\Response
116
     */
117
118
    public function create()
119
    {
120
        $response = [];
121
122
        $master_prestasis = $this->master_prestasi->all();
123
        $users_special = $this->user->all();
0 ignored issues
show
Unused Code introduced by
$users_special 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...
124
        $users_standar = $this->user->find(\Auth::User()->id);
0 ignored issues
show
Unused Code introduced by
$users_standar 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...
125
        $current_user = \Auth::User();
126
       
127
        $admin_sekolah = $this->admin_sekolah->where('admin_sekolah_id', Auth::user()->id)->first();
128
        
129
        if($this->checkRole(['superadministrator'])){
130
            $siswas = $this->siswa->all();
131
        }else{
132
            $sekolah_id = $admin_sekolah->sekolah_id;
133
            $siswas     = $this->siswa->where('sekolah_id', $sekolah_id)->get();
134
        }
135
136
        array_set($current_user, 'label', $current_user->name);
137
138
        $response['current_user'] = $current_user;
139
140
        foreach($master_prestasis as $master_prestasi){
141
            if($master_prestasi->juara == 1){
142
                $juara = "Juara 1";
143
            }elseif($master_prestasi->juara == 2){
144
                $juara = "Juara 2";
145
            }elseif($master_prestasi->juara == 3){
146
                $juara = "Juara 3";
147
            }else{
148
                $juara = "Juara Harapan 1";
149
            }
150
151
            if($master_prestasi->tingkat == 1){
152
                $tingkat = "Tingkat Internasional";
153
            }elseif($master_prestasi->tingkat == 2){
154
                $tingkat = "Tingkat Nasional";
155
            }elseif($master_prestasi->tingkat == 3){
156
                $tingkat = "Tingkat Provinsi";
157
            }else{
158
                $tingkat = "Tingkat Kabupaten/Kota";
159
            }
160
161
            array_set($master_prestasi, 'label', "( ".$juara." ".$tingkat." ) - ".$master_prestasi->jenis_prestasi->nama);
162
        }
163
164
        foreach($siswas as $siswa){
165
            array_set($siswa, 'label', $siswa->nama_siswa);
166
        }
167
168
        $response['master_prestasi'] = $master_prestasis;
169
        $response['siswa'] = $siswas;
170
        $response['status'] = true;
171
172
        return response()->json($response);
173
    }
174
175
    /**
176
     * Display the specified resource.
177
     *
178
     * @param  \App\Prestasi  $prestasi
0 ignored issues
show
Bug introduced by
There is no parameter named $prestasi. 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...
179
     * @return \Illuminate\Http\Response
180
     */
181 View Code Duplication
    public function store(Request $request)
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...
182
    {
183
        $prestasi = $this->prestasi;
184
185
        $validator = Validator::make($request->all(), [
186
            'nomor_un'              => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->prestasi->getTable()},nomor_un,NULL,id,deleted_at,NULL",
187
            'master_prestasi_id'    => "required|exists:{$this->master_prestasi->getTable()},id",
188
            'nama_lomba'            => 'required|max:255',
189
            // 'nilai'             => 'required|numeric|min:0|max:100',
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...
190
            'user_id'               => "required|exists:{$this->user->getTable()},id",
191
        ]);
192
193
        if ($validator->fails()) {
194
            $error                  = true;
0 ignored issues
show
Unused Code introduced by
$error 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...
195
            $response['message']    = $validator->errors()->first();
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...
196
        } else {
197
            $prestasi_master_prestasi_id    = $request->input('master_prestasi_id');
198
            $master_prestasi                = $this->master_prestasi->findOrFail($prestasi_master_prestasi_id);
199
200
            $prestasi->nomor_un             = $request->input('nomor_un');
201
            $prestasi->master_prestasi_id   = $prestasi_master_prestasi_id;
202
            $prestasi->nama_lomba           = $request->input('nama_lomba');
203
            $prestasi->nilai                = $master_prestasi->nilai;
204
            $prestasi->user_id              = $request->input('user_id');
205
206
            $nilai = $this->nilai->updateOrCreate(
207
                [
208
                    'nomor_un'  => $prestasi->nomor_un,
209
                ],
210
                [
211
                    'prestasi'  => $prestasi->nilai,
212
                    'total'     => null,
213
                    'user_id'   => $prestasi->user_id,
214
                ]
215
            );
216
217
            DB::beginTransaction();
218
219
            if ($prestasi->save() && $nilai->save())
220
            {
221
                DB::commit();
222
223
                $error      = false;
0 ignored issues
show
Unused Code introduced by
$error 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...
224
                $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...
225
            } else {
226
                DB::rollBack();
227
228
                $error                  = true;
0 ignored issues
show
Unused Code introduced by
$error 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...
229
                $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...
230
            }
231
        }
232
233
        $response['status'] = true;
234
235
        return response()->json($response);
236
    }
237
238
    /**
239
     * Store a newly created resource in storage.
240
     *
241
     * @param  \Illuminate\Http\Request  $request
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
242
     * @return \Illuminate\Http\Response
243
     */
244 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...
245
    {
246
        $prestasi = $this->prestasi->findOrFail($id);
247
248
        $response['user'] = $prestasi->user;
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...
249
        $response['master_prestasi'] = $prestasi->master_prestasi;
250
        $response['siswa'] = $prestasi->siswa;
251
        $response['prestasi'] = $prestasi;
252
        $response['status'] = true;
253
254
        return response()->json($response);
255
    }
256
257
    /**
258
     * Show the form for editing the specified resource.
259
     *
260
     * @param  \App\Prestasi  $prestasi
261
     * @return \Illuminate\Http\Response
0 ignored issues
show
Bug introduced by
There is no parameter named $prestasi. 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...
262
     */
263
264
    public function edit($id)
265
    {
266
        $prestasi = $this->prestasi->findOrFail($id);
267
268
        array_set($prestasi->user, 'label', $prestasi->user->name);
269
        array_set($prestasi->master_prestasi, 'label', $prestasi->master_prestasi->juara);
270
        array_set($prestasi->siswa, 'label', $prestasi->siswa->nama_siswa);
271
272
        $response['master_prestasi'] = $prestasi->master_prestasi;
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...
273
        $response['siswa'] = $prestasi->siswa;
274
        $response['prestasi'] = $prestasi;
275
        $response['user'] = $prestasi->user;
276
        $response['status'] = true;
277
278
        return response()->json($response);
279
    }
280
281
    /**
282
     * Update the specified resource in storage.
283
     *
284
     * @param  \Illuminate\Http\Request  $request
285
     * @param  \App\Prestasi  $prestasi
0 ignored issues
show
Bug introduced by
There is no parameter named $prestasi. 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...
286
     * @return \Illuminate\Http\Response
287
     */
288 View Code Duplication
    public function update(Request $request, $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...
289
    {
290
        $prestasi = $this->prestasi;
291
292
        $validator = Validator::make($request->all(), [
293
            'nomor_un'              => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->prestasi->getTable()},nomor_un,{$id},id,deleted_at,NULL",
294
            'master_prestasi_id'    => "required|exists:{$this->master_prestasi->getTable()},id",
295
            'nama_lomba'            => 'required|max:255',
296
            // 'nilai'             => 'required|numeric|min:0|max:100',
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...
297
            'user_id'               => "required|exists:{$this->user->getTable()},id",
298
        ]);
299
300
        if ($validator->fails()) {
301
            $error                  = true;
0 ignored issues
show
Unused Code introduced by
$error 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...
302
            $response['message']    = $validator->errors()->first();
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...
303
        } else {
304
            $prestasi_master_prestasi_id    = $request->input('master_prestasi_id');
305
            $master_prestasi                = $this->master_prestasi->findOrFail($prestasi_master_prestasi_id);
306
307
            $prestasi->nomor_un             = $request->input('nomor_un');
308
            $prestasi->master_prestasi_id   = $prestasi_master_prestasi_id;
309
            $prestasi->nama_lomba           = $request->input('nama_lomba');
310
            $prestasi->nilai                = $master_prestasi->nilai;
311
            $prestasi->user_id              = $request->input('user_id');
312
313
            $nilai = $this->nilai->updateOrCreate(
314
                [
315
                    'nomor_un'  => $prestasi->nomor_un,
316
                ],
317
                [
318
                    'prestasi'  => $prestasi->nilai,
319
                    'total'     => null,
320
                    'user_id'   => $prestasi->user_id,
321
                ]
322
            );
323
324
            DB::beginTransaction();
325
326
            if ($prestasi->save() && $nilai->save())
327
            {
328
                DB::commit();
329
330
                $error      = false;
0 ignored issues
show
Unused Code introduced by
$error 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...
331
                $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...
332
            } else {
333
                DB::rollBack();
334
335
                $error                  = true;
0 ignored issues
show
Unused Code introduced by
$error 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...
336
                $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...
337
            }
338
        }
339
340
        $response['status'] = true;
341
342
        return response()->json($response);
343
    }
344
345
    /**
346
     * Remove the specified resource from storage.
347
     *
348
     * @param  \App\Prestasi  $prestasi
0 ignored issues
show
Bug introduced by
There is no parameter named $prestasi. 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 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...
352
    {
353
        $prestasi = $this->prestasi->findOrFail($id);
354
355
        if ($prestasi->delete()) {
356
            $response['status'] = true;
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...
357
        } else {
358
            $response['status'] = false;
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...
359
        }
360
361
        return json_encode($response);
362
    }
363
364
    protected function checkRole($role = array())
365
    {
366
        return Auth::user()->hasRole($role);
367
    }
368
}
369