ProgramKeahlianController::store()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 36
Code Lines 24

Duplication

Lines 5
Ratio 13.89 %

Importance

Changes 0
Metric Value
cc 3
eloc 24
nc 3
nop 1
dl 5
loc 36
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Bantenprov\ProgramKeahlian\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\ProgramKeahlian\Facades\ProgramKeahlianFacade;
10
11
/* Models */
12
use Bantenprov\ProgramKeahlian\Models\Bantenprov\ProgramKeahlian\ProgramKeahlian;
13
use App\User;
14
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah;
15
16
/* Etc */
17
use Validator;
18
use Auth;
19
20
/**
21
 * The ProgramKeahlianController class.
22
 *
23
 * @package Bantenprov\ProgramKeahlian
24
 * @author  bantenprov <[email protected]>
25
 */
26
class ProgramKeahlianController extends Controller
27
{
28
    protected $program_keahlian;
29
    protected $user;
30
    protected $admin_sekolah;
31
    protected $user_id;
32
33
    /**
34
     * Create a new controller instance.
35
     *
36
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
37
     */
38
    public function __construct()
39
    {
40
        $this->program_keahlian = new ProgramKeahlian;
41
        $this->user             = new User;
42
        $this->admin_sekolah    = AdminSekolah::where('admin_sekolah_id', $this->user_id)->first();
43
        $this->user_id          = isset(Auth::User()->id) ? Auth::User()->id : null;
44
    }
45
46
    /**
47
     * Display a listing of the resource.
48
     *
49
     * @return \Illuminate\Http\Response
50
     */
51
    public function index(Request $request)
52
    {
53
        if (request()->has('sort')) {
54
            list($sortCol, $sortDir) = explode('|', request()->sort);
55
56
            $query = $this->program_keahlian->orderBy($sortCol, $sortDir);
57
58
            if (Auth::User()->hasRole(['superadministrator'])) {
59
                $query = $this->program_keahlian->orderBy($sortCol, $sortDir);
60
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
61
                //
62
            }
63
        } else {
64
            $query = $this->program_keahlian->orderBy('id', 'asc');
65
        }
66
67
        if ($request->exists('filter')) {
68
            $query->where(function($q) use($request) {
69
                $value = "%{$request->filter}%";
70
71
                if (Auth::User()->hasRole(['superadministrator'])) {
72
                    $q->where('label', 'like', $value)
73
                        ->orWhere('keterangan', 'like', $value);
74
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
75
                    //
76
                }
77
            });
78
        }
79
80
        $perPage    = request()->has('per_page') ? (int) request()->per_page : null;
81
        $response   = $query->with(['user'])->paginate($perPage);
0 ignored issues
show
Unused Code introduced by
$response 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...
82
83
        if (is_null($this->admin_sekolah) && !Auth::User()->hasRole(['superadministrator'])) {
84
            $response   = (object) [];
85
        } else {
86
            $response   = $query->with(['user'])->paginate($perPage);
87
        }
88
89
        return response()->json($response)
90
            ->header('Access-Control-Allow-Origin', '*')
91
            ->header('Access-Control-Allow-Methods', 'GET');
92
    }
93
94
    /**
95
     * Display a listing of the resource.
96
     *
97
     * @return \Illuminate\Http\Response
98
     */
99
    public function get()
100
    {
101
        if (Auth::User()->hasRole(['superadministrator'])) {
102
            $program_keahlians = $this->program_keahlian->with(['user'])->get();
103
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
104
            //
105
        }
106
107
        $response['program_keahlians']  = $program_keahlians;
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...
Bug introduced by
The variable $program_keahlians does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
108
        $response['error']              = false;
109
        $response['message']            = 'Success.';
110
        $response['status']             = true;
111
112
        if (is_null($this->admin_sekolah) && !Auth::User()->hasRole(['superadministrator'])) {
113
            return response()->json((object) []);
114
        } else {
115
            return response()->json($response);
116
        }
117
    }
118
119
    /**
120
     * Show the form for creating a new resource.
121
     *
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function create()
125
    {
126
        $program_keahlian   = $this->program_keahlian;
127
        $users              = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
128
        $users_special      = $this->user->all();
129
        $users_standar      = $this->user->findOrFail($this->user_id);
130
        $current_user       = Auth::User();
131
132
        $program_keahlian->label        = null;
133
        $program_keahlian->keterangan   = null;
134
        $program_keahlian->user_id      = null;
135
136 View Code Duplication
        if (Auth::User()->hasRole(['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...
137
            $user_special = true;
138
139
            foreach ($users_special as $user) {
140
                array_set($user, 'label', $user->name);
141
            }
142
143
            $users = $users_special;
144
        } else {
145
            $user_special = false;
146
147
            array_set($users_standar, 'label', $users_standar->name);
148
149
            $users = $users_standar;
150
        }
151
152
        array_set($current_user, 'label', $current_user->name);
153
154
        $response['program_keahlian']   = $program_keahlian;
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...
155
        $response['users']              = $users;
156
        $response['user_special']       = $user_special;
157
        $response['current_user']       = $current_user;
158
        $response['error']              = false;
159
        $response['message']            = 'Loaded.';
160
        $response['status']             = true;
161
162
        return response()->json($response);
163
    }
164
165
    /**
166
     * Store a newly created resource in storage.
167
     *
168
     * @param  \Illuminate\Http\Request  $request
169
     * @return \Illuminate\Http\Response
170
     */
171
    public function store(Request $request)
172
    {
173
        $program_keahlian = $this->program_keahlian;
174
175
        $validator = Validator::make($request->all(), [
176
            'label'         => "required|max:64|unique:{$this->program_keahlian->getTable()},label,NULL,id,deleted_at,NULL",
177
            'keterangan'    => 'required|max:255',
178
            'user_id'       => "required|exists:{$this->user->getTable()},id",
179
        ]);
180
181
        if ($validator->fails()) {
182
            $error      = true;
183
            $message    = $validator->errors()->first();
184
        } else {
185
            $program_keahlian->label        = $request->input('label');
186
            $program_keahlian->keterangan   = $request->input('keterangan');
187
188 View Code Duplication
            if (Auth::User()->hasRole(['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...
189
                $program_keahlian->user_id  = $request->input('user_id');
190
            } else {
191
                $program_keahlian->user_id  = $this->user_id;
192
            }
193
194
            $program_keahlian->save();
195
196
            $error      = false;
197
            $message    = 'Success.';
198
        }
199
200
        $response['program_keahlian']   = $program_keahlian;
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...
201
        $response['error']              = $error;
202
        $response['message']            = $message;
203
        $response['status']             = true;
204
205
        return response()->json($response);
206
    }
207
208
    /**
209
     * Display the specified resource.
210
     *
211
     * @param  \App\ProgramKeahlian  $program_keahlian
0 ignored issues
show
Bug introduced by
There is no parameter named $program_keahlian. 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...
212
     * @return \Illuminate\Http\Response
213
     */
214
    public function show($id)
215
    {
216
        $program_keahlian = $this->program_keahlian->with(['user'])->findOrFail($id);
217
218
        $response['program_keahlian']   = $program_keahlian;
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...
219
        $response['error']              = false;
220
        $response['message']            = 'Loaded.';
221
        $response['status']             = true;
222
223
        return response()->json($response);
224
    }
225
226
    /**
227
     * Show the form for editing the specified resource.
228
     *
229
     * @param  \App\ProgramKeahlian  $program_keahlian
0 ignored issues
show
Bug introduced by
There is no parameter named $program_keahlian. 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...
230
     * @return \Illuminate\Http\Response
231
     */
232
    public function edit($id)
233
    {
234
        $program_keahlian   = $this->program_keahlian->with(['user'])->findOrFail($id);
235
        $users              = $this->user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$users is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
236
        $users_special      = $this->user->all();
237
        $users_standar      = $this->user->findOrFail($this->user_id);
238
        $current_user       = Auth::User();
239
240
        if ($program_keahlian->user !== null) {
241
            array_set($program_keahlian->user, 'label', $program_keahlian->user->name);
242
        }
243
244 View Code Duplication
        if (Auth::User()->hasRole(['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...
245
            $user_special = true;
246
247
            foreach ($users_special as $user) {
248
                array_set($user, 'label', $user->name);
249
            }
250
251
            $users = $users_special;
252
        } else {
253
            $user_special = false;
254
255
            array_set($users_standar, 'label', $users_standar->name);
256
257
            $users = $users_standar;
258
        }
259
260
        array_set($current_user, 'label', $current_user->name);
261
262 View Code Duplication
        if (Auth::User()->hasRole(['superadministrator']) || $program_keahlian->user_id == $this->user_id) {
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...
263
            $error      = false;
264
            $message    = 'Loaded.';
265
        } else {
266
            $error      = true;
267
            $message    = 'The data can not be loaded because it is not yours.';
268
        }
269
270
        $response['program_keahlian']   = $program_keahlian;
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...
271
        $response['users']              = $users;
272
        $response['user_special']       = $user_special;
273
        $response['current_user']       = $current_user;
274
        $response['error']              = $error;
275
        $response['message']            = $message;
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\ProgramKeahlian  $program_keahlian
0 ignored issues
show
Bug introduced by
There is no parameter named $program_keahlian. 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
    public function update(Request $request, $id)
289
    {
290
        $program_keahlian = $this->program_keahlian->with(['user'])->findOrFail($id);
291
292
        $validator = Validator::make($request->all(), [
293
            'label'         => "required|max:64|unique:{$this->program_keahlian->getTable()},label,{$id},id,deleted_at,NULL",
294
            'keterangan'    => 'required|max:255',
295
            'user_id'       => "required|exists:{$this->user->getTable()},id",
296
        ]);
297
298
        if ($validator->fails()) {
299
            $error      = true;
300
            $message    = $validator->errors()->first();
301
        } else {
302
            $program_keahlian->label        = $request->input('label');
303
            $program_keahlian->keterangan   = $request->input('keterangan');
304
305 View Code Duplication
            if (Auth::User()->hasRole(['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...
306
                $program_keahlian->user_id  = $request->input('user_id');
307
            } else {
308
                $program_keahlian->user_id  = $this->user_id;
309
            }
310
311 View Code Duplication
            if (Auth::User()->hasRole(['superadministrator']) || $program_keahlian->user_id == $this->user_id) {
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...
312
                $program_keahlian->save();
313
314
                $error      = false;
315
                $message    = 'Success.';
316
            } else {
317
                $error      = true;
318
                $message    = 'The data can not be updated because it is not yours.';
319
            }
320
        }
321
322
        $response['error']      = $error;
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...
323
        $response['message']    = $message;
324
        $response['status']     = true;
325
326
        return response()->json($response);
327
    }
328
329
    /**
330
     * Remove the specified resource from storage.
331
     *
332
     * @param  \App\ProgramKeahlian  $program_keahlian
0 ignored issues
show
Bug introduced by
There is no parameter named $program_keahlian. 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...
333
     * @return \Illuminate\Http\Response
334
     */
335
    public function destroy($id)
336
    {
337
        $program_keahlian = $this->program_keahlian->findOrFail($id);
338
339
        if (Auth::User()->hasRole(['superadministrator']) || $program_keahlian->user_id == $this->user_id) {
340
            if ($program_keahlian->delete()) {
341
                $error      = false;
342
                $message    = 'Success.';
343
            } else {
344
                $error      = true;
345
                $message    = 'Failed';
346
            }
347
        } else {
348
            $error      = true;
349
            $message    = 'The data can not be deleted because it is not yours.';
350
        }
351
352
        $response['error']      = $error;
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...
353
        $response['message']    = $message;
354
        $response['status']     = true;
355
356
        return json_encode($response);
357
    }
358
}
359