Completed
Push — master ( 4eaaa2...06a8e1 )
by
unknown
10s
created

PrestasiController::update()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 56
Code Lines 34

Duplication

Lines 56
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 34
nc 3
nop 2
dl 56
loc 56
rs 9.0544
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
18
/* Etc */
19
use Validator;
20
use Auth;
21
22
/**
23
 * The PrestasiController class.
24
 *
25
 * @package Bantenprov\Prestasi
26
 * @author  bantenprov <[email protected]>
27
 */
28
class PrestasiController extends Controller
29
{
30
    /**
31
     * Create a new controller instance.
32
     *
33
     * @return void
34
     */
35
    protected $prestasi;
36
    protected $siswa;
37
    protected $master_prestasi;
38
    protected $user;
39
    protected $nilai;
40
41
    public function __construct()
42
    {
43
        $this->prestasi         = new Prestasi;
44
        $this->siswa            = new Siswa;
45
        $this->master_prestasi  = new MasterPrestasi;
46
        $this->user             = new User;
47
        $this->nilai            = new Nilai;
48
    }
49
50
    /**
51
     * Display a listing of the resource.
52
     *
53
     * @return \Illuminate\Http\Response
54
     */
55 View Code Duplication
    public function index(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...
56
    {
57
        if (request()->has('sort')) {
58
            list($sortCol, $sortDir) = explode('|', request()->sort);
59
60
            $query = $this->prestasi->orderBy($sortCol, $sortDir);
61
        } else {
62
            $query = $this->prestasi->orderBy('id', 'asc');
63
        }
64
65
        if ($request->exists('filter')) {
66
            $query->where(function($q) use($request) {
67
                $value = "%{$request->filter}%";
68
                $q->where('nomor_un', 'like', $value)
69
                    ->orWhere('nama_lomba', 'like', $value);
70
            });
71
        }
72
73
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
74
        $response = $query->with('user')->with('master_prestasi')->with('siswa')->paginate($perPage);
75
76
        return response()->json($response)
77
            ->header('Access-Control-Allow-Origin', '*')
78
            ->header('Access-Control-Allow-Methods', 'GET');
79
    }
80
81
    /**
82
     * Show the form for creating a new resource.
83
     *
84
     * @return \Illuminate\Http\Response
85
     */
86
87
    public function create()
88
    {
89
        $response = [];
90
91
        $master_prestasis = $this->master_prestasi->all();
92
        $siswas = $this->siswa->all();
93
        $users_special = $this->user->all();
94
        $users_standar = $this->user->find(\Auth::User()->id);
95
        $current_user = \Auth::User();
96
97
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
98
99 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...
100
            $response['user_special'] = true;
101
            foreach($users_special as $user){
102
                array_set($user, 'label', $user->name);
103
            }
104
            $response['user'] = $users_special;
105
        }else{
106
            $response['user_special'] = false;
107
            array_set($users_standar, 'label', $users_standar->name);
108
            $response['user'] = $users_standar;
109
        }
110
111
        array_set($current_user, 'label', $current_user->name);
112
113
        $response['current_user'] = $current_user;
114
115
        foreach($master_prestasis as $master_prestasi){
116
            if($master_prestasi->juara == 1){
117
                $juara = "Juara 1";
118
            }elseif($master_prestasi->juara == 2){
119
                $juara = "Juara 2";
120
            }elseif($master_prestasi->juara == 3){
121
                $juara = "Juara 3";
122
            }else{
123
                $juara = "Juara Harapan 1";
124
            }
125
126
            if($master_prestasi->tingkat == 1){
127
                $tingkat = "Tingkat Internasional";
128
            }elseif($master_prestasi->tingkat == 2){
129
                $tingkat = "Tingkat Nasional";
130
            }elseif($master_prestasi->tingkat == 3){
131
                $tingkat = "Tingkat Provinsi";
132
            }else{
133
                $tingkat = "Tingkat Kabupaten/Kota";
134
            }
135
136
            array_set($master_prestasi, 'label', "( ".$juara." ".$tingkat." ) - ".$master_prestasi->jenis_prestasi->nama);
137
        }
138
139
        foreach($siswas as $siswa){
140
            array_set($siswa, 'label', $siswa->nama_siswa);
141
        }
142
143
        $response['master_prestasi'] = $master_prestasis;
144
        $response['siswa'] = $siswas;
145
        $response['status'] = true;
146
147
        return response()->json($response);
148
    }
149
150
    /**
151
     * Display the specified resource.
152
     *
153
     * @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...
154
     * @return \Illuminate\Http\Response
155
     */
156 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...
157
    {
158
        $prestasi = $this->prestasi;
159
160
        $validator = Validator::make($request->all(), [
161
            'nomor_un'              => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->prestasi->getTable()},nomor_un,NULL,id,deleted_at,NULL",
162
            'master_prestasi_id'    => "required|exists:{$this->master_prestasi->getTable()},id",
163
            'nama_lomba'            => 'required|max:255',
164
            // '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...
165
            'user_id'               => "required|exists:{$this->user->getTable()},id",
166
        ]);
167
168
        if ($validator->fails()) {
169
            $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...
170
            $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...
171
        } else {
172
            $prestasi_master_prestasi_id    = $request->input('master_prestasi_id');
173
            $master_prestasi                = $this->master_prestasi->findOrFail($prestasi_master_prestasi_id);
174
175
            $prestasi->nomor_un             = $request->input('nomor_un');
176
            $prestasi->master_prestasi_id   = $prestasi_master_prestasi_id;
177
            $prestasi->nama_lomba           = $request->input('nama_lomba');
178
            $prestasi->nilai                = $master_prestasi->nilai;
179
            $prestasi->user_id              = $request->input('user_id');
180
181
            $nilai = $this->nilai->updateOrCreate(
182
                [
183
                    'nomor_un'  => $prestasi->nomor_un,
184
                ],
185
                [
186
                    'prestasi'  => $prestasi->nilai,
187
                    'total'     => null,
188
                    'user_id'   => $prestasi->user_id,
189
                ]
190
            );
191
192
            DB::beginTransaction();
193
194
            if ($prestasi->save() && $nilai->save())
195
            {
196
                DB::commit();
197
198
                $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...
199
                $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...
200
            } else {
201
                DB::rollBack();
202
203
                $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...
204
                $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...
205
            }
206
        }
207
208
        $response['status'] = true;
209
210
        return response()->json($response);
211
    }
212
213
    /**
214
     * Store a newly created resource in storage.
215
     *
216
     * @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...
217
     * @return \Illuminate\Http\Response
218
     */
219 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...
220
    {
221
        $prestasi = $this->prestasi->findOrFail($id);
222
223
        $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...
224
        $response['master_prestasi'] = $prestasi->master_prestasi;
225
        $response['siswa'] = $prestasi->siswa;
226
        $response['prestasi'] = $prestasi;
227
        $response['status'] = true;
228
229
        return response()->json($response);
230
    }
231
232
    /**
233
     * Show the form for editing the specified resource.
234
     *
235
     * @param  \App\Prestasi  $prestasi
236
     * @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...
237
     */
238
239
    public function edit($id)
240
    {
241
        $prestasi = $this->prestasi->findOrFail($id);
242
243
        array_set($prestasi->user, 'label', $prestasi->user->name);
244
        array_set($prestasi->master_prestasi, 'label', $prestasi->master_prestasi->juara);
245
        array_set($prestasi->siswa, 'label', $prestasi->siswa->nama_siswa);
246
247
        $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...
248
        $response['siswa'] = $prestasi->siswa;
249
        $response['prestasi'] = $prestasi;
250
        $response['user'] = $prestasi->user;
251
        $response['status'] = true;
252
253
        return response()->json($response);
254
    }
255
256
    /**
257
     * Update the specified resource in storage.
258
     *
259
     * @param  \Illuminate\Http\Request  $request
260
     * @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...
261
     * @return \Illuminate\Http\Response
262
     */
263 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...
264
    {
265
        $prestasi = $this->prestasi;
266
267
        $validator = Validator::make($request->all(), [
268
            'nomor_un'              => "required|exists:{$this->siswa->getTable()},nomor_un|unique:{$this->prestasi->getTable()},nomor_un,{$id},id,deleted_at,NULL",
269
            'master_prestasi_id'    => "required|exists:{$this->master_prestasi->getTable()},id",
270
            'nama_lomba'            => 'required|max:255',
271
            // '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...
272
            'user_id'               => "required|exists:{$this->user->getTable()},id",
273
        ]);
274
275
        if ($validator->fails()) {
276
            $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...
277
            $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...
278
        } else {
279
            $prestasi_master_prestasi_id    = $request->input('master_prestasi_id');
280
            $master_prestasi                = $this->master_prestasi->findOrFail($prestasi_master_prestasi_id);
281
282
            $prestasi->nomor_un             = $request->input('nomor_un');
283
            $prestasi->master_prestasi_id   = $prestasi_master_prestasi_id;
284
            $prestasi->nama_lomba           = $request->input('nama_lomba');
285
            $prestasi->nilai                = $master_prestasi->nilai;
286
            $prestasi->user_id              = $request->input('user_id');
287
288
            $nilai = $this->nilai->updateOrCreate(
289
                [
290
                    'nomor_un'  => $prestasi->nomor_un,
291
                ],
292
                [
293
                    'prestasi'  => $prestasi->nilai,
294
                    'total'     => null,
295
                    'user_id'   => $prestasi->user_id,
296
                ]
297
            );
298
299
            DB::beginTransaction();
300
301
            if ($prestasi->save() && $nilai->save())
302
            {
303
                DB::commit();
304
305
                $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...
306
                $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...
307
            } else {
308
                DB::rollBack();
309
310
                $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...
311
                $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...
312
            }
313
        }
314
315
        $response['status'] = true;
316
317
        return response()->json($response);
318
    }
319
320
    /**
321
     * Remove the specified resource from storage.
322
     *
323
     * @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...
324
     * @return \Illuminate\Http\Response
325
     */
326 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...
327
    {
328
        $prestasi = $this->prestasi->findOrFail($id);
329
330
        if ($prestasi->delete()) {
331
            $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...
332
        } else {
333
            $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...
334
        }
335
336
        return json_encode($response);
337
    }
338
}
339