Completed
Push — master ( 250808...71faef )
by
unknown
07:37 queued 05:50
created

DataAkademikController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\DataAkademik\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\DataAkademik\Facades\DataAkademikFacade;
9
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah;
10
use App\User;
11
12
/* Models */
13
use Bantenprov\DataAkademik\Models\Bantenprov\DataAkademik\DataAkademik;
14
15
/* Etc */
16
use Validator;
17
use Auth;
18
19
/**
20
 * The DataAkademikController class.
21
 *
22
 * @package Bantenprov\DataAkademik
23
 * @author  bantenprov <[email protected]>
24
 */
25
class DataAkademikController extends Controller
26
{
27
    /**
28
     * Create a new controller instance.
29
     *
30
     * @return void
31
     */
32
    protected $user;
33
    public function __construct(DataAkademik $data_akademik, User $user)
34
    {
35
        $this->data_akademik    = $data_akademik;
36
        $this->user             = $user;
37
    }
38
39
    /**
40
     * Display a listing of the resource.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function index(Request $request)
45
    {
46
        if ($request->has('sort')) {
47
            list($sortCol, $sortDir) = explode('|', $request->sort);
48
49
            $query = $this->data_akademik->with('user')->orderBy($sortCol, $sortDir);
50
        } else {
51
            $query = $this->data_akademik->with('user')->orderBy('id', 'asc');
52
        }
53
54
        if ($request->exists('filter')) {
55
            $query->where(function($q) use($request) {
56
                $value = "%{$request->filter}%";
57
                $q->where('nama_siswa', 'like', $value)
58
                    ->orWhere('nomor_un', 'like', $value);
59
            });
60
        }
61
62
63
        $perPage = $request->has('per_page') ? (int) $request->per_page : null;
64
65
        if($this->checkRole(['superadministrator','administrator'])){
66
            $response = $query->paginate($perPage);
67
        }else{
68
            $response = [];
69
        }
70
71
        return response()->json($response)
72
            ->header('Access-Control-Allow-Origin', '*')
73
            ->header('Access-Control-Allow-Methods', 'GET');
74
    }
75
76
    /**
77
     * Show the form for creating a new resource.
78
     *
79
     * @return \Illuminate\Http\Response
80
     */
81
    public function create()
82
    {
83
        $response = [];
84
85
        $users_special = $this->user->all();
86
        $users_standar = $this->user->find(\Auth::User()->id);
87
        $current_user = \Auth::User();
88
89
        $role_check = \Auth::User()->hasRole(['superadministrator']);
90
91
        if($role_check){
92
            $response['user_special'] = true;
93
            foreach($users_special as $user){
94
                array_set($user, 'label', $user->name);
95
            }
96
            $response['user'] = $users_special;
97 View Code Duplication
        }else{
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...
98
            $response['user_special'] = false;
99
            array_set($users_standar, 'label', $users_standar->name);
100
            if($this->checkRole(['superadministrator','administrator'])){
101
                $response['user'] = $users_standar;
102
            }else{
103
                $response['user'] = null;
104
            }
105
        }
106
107
        array_set($current_user, 'label', $current_user->name);
108
109 View Code Duplication
        if($this->checkRole(['superadministrator','administrator'])){
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...
110
            $response['current_user']   = $current_user;
111
            $response['status']         = true;
112
        }else{
113
            $response['current_user']   = null;
114
            $response['status']         = null;
115
        }
116
117
        $response['status']         = true;
118
119
        return response()->json($response);
120
    }
121
122
    /**
123
     * Display the specified resource.
124
     *
125
     * @param  \App\DataAkademik  $data_akademik
0 ignored issues
show
Bug introduced by
There is no parameter named $data_akademik. 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...
126
     * @return \Illuminate\Http\Response
127
     */
128
    public function store(Request $request)
129
    {
130 View Code Duplication
        if($this->checkRole(['superadministrator','administrator'])){
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...
131
            $response['message'] = 'Tidak mempunyai hak ases untuk ini.';
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...
132
            $response['status'] = true;
133
134
            return response()->json($response);
135
        }
136
137
138
        $data_akademik = $this->data_akademik;
139
140
        $validator = Validator::make($request->all(), [
141
            'nomor_un'          => 'required|unique:data_akademiks,nomor_un',
142
            'nama_siswa'        => 'required',
143
            'nomor_kk'          => 'required',
144
            'bahasa_indonesia'  => 'required|numeric',
145
            'bahasa_inggris'    => 'required|numeric',
146
            'matematika'        => 'required|numeric',
147
            'ipa'               => 'required|numeric',
148
            'user_id'           => 'required',
149
        ]);
150
151 View Code Duplication
        if($validator->fails()){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
            $check = $data_akademik->where('nomor_un',$request->nomor_un)->whereNull('deleted_at')->count();
153
154
            if ($check > 0) {
155
                $response['message'] = 'Failed Nomor UN : ' . $request->nomor_un . ' already exists';
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...
156
157
            } else {
158
                $data_akademik->nomor_un          = $request->input('nomor_un');
159
                $data_akademik->nama_siswa        = $request->input('nama_siswa');
160
                $data_akademik->nomor_kk          = $request->input('nomor_kk');
161
                $data_akademik->user_id           = $request->input('user_id');
162
                $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
163
                $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
164
                $data_akademik->matematika        = $request->input('matematika');
165
                $data_akademik->ipa               = $request->input('ipa');
166
                $data_akademik->save();
167
168
                $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...
169
            }
170
        } else {
171
            $data_akademik->nomor_un          = $request->input('nomor_un');
172
            $data_akademik->nama_siswa        = $request->input('nama_siswa');
173
            $data_akademik->nomor_kk          = $request->input('nomor_kk');
174
            $data_akademik->user_id           = $request->input('user_id');
175
            $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
176
            $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
177
            $data_akademik->matematika        = $request->input('matematika');
178
            $data_akademik->ipa               = $request->input('ipa');
179
            $data_akademik->save();
180
181
            $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...
182
        }
183
184
        $response['status'] = true;
185
186
        return response()->json($response);
187
    }
188
189
    /**
190
     * Store a newly created resource in storage.
191
     *
192
     * @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...
193
     * @return \Illuminate\Http\Response
194
     */
195
    public function show($id)
196
    {
197
198
199
        $data_akademik = $this->data_akademik->findOrFail($id);
200
201
        array_set($data_akademik, 'user', $data_akademik->user->name);
202
203
        if($this->checkRole(['superadministrator','administrator'])){
204
            $response['data_akademik']      = $data_akademik;
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
            $response['status']             = true;
206
        }else{
207
            $response['data_akademik']      = [];
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...
208
            $response['massege']            = 'Tidak mempunyai hak akses untuk ini.';
209
            $response['status']             = true;
210
        }
211
212
213
        return response()->json($response);
214
    }
215
216
    /**
217
     * Show the form for editing the specified resource.
218
     *
219
     * @param  \App\DataAkademik  $data_akademik
0 ignored issues
show
Bug introduced by
There is no parameter named $data_akademik. 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...
220
     * @return \Illuminate\Http\Response
221
     */
222
    public function edit($id)
223
    {
224
        $data_akademik = $this->data_akademik->findOrFail($id);
225
226
        array_set($data_akademik->user, 'label', $data_akademik->user->name);
227
228
        if($this->checkRole(['superadministrator'])){
229
            $response['data_akademik']      = $data_akademik;
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
            $response['user']               = $data_akademik->user;
231
            $response['status']             = true;
232
        }else{
233
            $response['massege']            = 'Tidak mempunyai hak akses untuk ini.';
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...
234
            $response['data_akademik']      = null;
235
            $response['user']               = null;
236
            $response['status']             = true;
237
        }
238
239
240
241
        return response()->json($response);
242
    }
243
244
    /**
245
     * Update the specified resource in storage.
246
     *
247
     * @param  \Illuminate\Http\Request  $request
248
     * @param  \App\DataAkademik  $data_akademik
0 ignored issues
show
Bug introduced by
There is no parameter named $data_akademik. 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...
249
     * @return \Illuminate\Http\Response
250
     */
251
    public function update(Request $request, $id)
252
    {
253 View Code Duplication
        if($this->checkRole(['superadministrator'])){
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...
254
            $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...
255
            $response['message'] = 'Tidak mempunyai akses untuk ini.';
256
257
            return response()->json($response);
258
        }
259
260
        $data_akademik = $this->data_akademik->findOrFail($id);
261
262
        if ($request->input('old_nomor_un') == $request->input('nomor_un'))
263
        {
264
            $validator = Validator::make($request->all(), [
265
                'nomor_un'          => 'required',
266
                'nama_siswa'        => 'required',
267
                'nomor_kk'          => 'required',
268
                'bahasa_indonesia'  => 'required|numeric',
269
                'bahasa_inggris'    => 'required|numeric',
270
                'matematika'        => 'required|numeric',
271
                'ipa'               => 'required|numeric',
272
                'user_id'           => 'required',
273
274
            ]);
275
        } else {
276
            $validator = Validator::make($request->all(), [
277
                'nomor_un'          => 'required|unique:data_akademiks,nomor_un',
278
                'nama_siswa'        => 'required',
279
                'nomor_kk'          => 'required',
280
                'bahasa_indonesia'  => 'required|numeric',
281
                'bahasa_inggris'    => 'required|numeric',
282
                'matematika'        => 'required|numeric',
283
                'ipa'               => 'required|numeric',
284
                'user_id'           => 'required',
285
            ]);
286
        }
287
288 View Code Duplication
        if ($validator->fails()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
289
            $check = $data_akademik->where('nomor_un',$request->nomor_un)->whereNull('deleted_at')->count();
290
291
            if ($check > 0) {
292
                $response['message'] = 'Failed Nomor UN : ' . $request->nomor_un . ' already exists';
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...
293
            } else {
294
                $data_akademik->nomor_un          = $request->input('nomor_un');
295
                $data_akademik->nomor_kk          = $request->input('nomor_kk');
296
                $data_akademik->nama_siswa        = $request->input('nama_siswa');
297
                $data_akademik->user_id           = $request->input('user_id');
298
                $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
299
                $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
300
                $data_akademik->matematika        = $request->input('matematika');
301
                $data_akademik->ipa               = $request->input('ipa');
302
                $data_akademik->save();
303
304
                $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...
305
            }
306
        } else {
307
            $data_akademik->nomor_un          = $request->input('nomor_un');
308
            $data_akademik->nomor_kk          = $request->input('nomor_kk');
309
            $data_akademik->nama_siswa        = $request->input('nama_siswa');
310
            $data_akademik->user_id           = $request->input('user_id');
311
            $data_akademik->bahasa_indonesia  = $request->input('bahasa_indonesia');
312
            $data_akademik->bahasa_inggris    = $request->input('bahasa_inggris');
313
            $data_akademik->matematika        = $request->input('matematika');
314
            $data_akademik->ipa               = $request->input('ipa');
315
            $data_akademik->save();
316
317
            $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...
318
        }
319
320
        $response['status'] = true;
321
322
        return response()->json($response);
323
    }
324
325
    /**
326
     * Remove the specified resource from storage.
327
     *
328
     * @param  \App\DataAkademik  $data_akademik
329
     * @return \Illuminate\Http\Response
0 ignored issues
show
Bug introduced by
There is no parameter named $data_akademik. 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...
330
     */
331
332
    public function destroy($id)
333
    {
334 View Code Duplication
        if($this->checkRole(['superadministrator'])){
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...
335
            $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...
336
            $response['message']    = 'Tidak mempunyai akses untuk ini.';
337
338
            return response()->json($response);
339
        }
340
341
        $data_akademik = $this->data_akademik->findOrFail($id);
342
343
        if ($data_akademik->delete()) {
344
            $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...
345
        } else {
346
            $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...
347
        }
348
349
        return json_encode($response);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return json_encode($response); (string) is incompatible with the return type documented by Bantenprov\DataAkademik\...emikController::destroy of type Illuminate\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
350
    }
351
352
    /* Check Role */
353
354
    protected function checkRole($role = array())
355
    {
356
        return Auth::user()->hasRole($role);
357
    }
358
}
359