PendaftaranWizardController::update()   D
last analyzed

Complexity

Conditions 14
Paths 28

Size

Total Lines 85
Code Lines 72

Duplication

Lines 22
Ratio 25.88 %

Importance

Changes 0
Metric Value
cc 14
eloc 72
nc 28
nop 2
dl 22
loc 85
rs 4.9516
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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\PendaftaranWizard\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\BudgetAbsorption\Facades\PendaftaranWizardFacade;
9
10
11
/* Models */
12
use Bantenprov\PendaftaranWizard\Models\Bantenprov\PendaftaranWizard\PendaftaranWizard;
13
use Bantenprov\Kegiatan\Models\Bantenprov\Kegiatan\Kegiatan;
14
use App\User;
15
16
/* Etc */
17
use Validator;
18
19
/**
20
 * The PendaftaranWizardController class.
21
 *
22
 * @package Bantenprov\PendaftaranWizard
23
 * @author  bantenprov <[email protected]>
24
 */
25
class PendaftaranWizardController extends Controller
26
{  
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
27
    
28
    /**
29
     * Create a new controller instance.
30
     *
31
     * @return void
32
     */
33
    protected $kegiatanModel;
34
    protected $pendaftaran;
35
    protected $user;
36
37
    public function __construct(PendaftaranWizard $pendaftaran, Kegiatan $kegiatan, User $user)
38
    {
39
        $this->pendaftaran      = $pendaftaran;
40
        $this->kegiatanModel    = $kegiatan;
41
        $this->user             = $user;
42
    }
43
44
    /**
45
     * Display a listing of the resource.
46
     *
47
     * @return \Illuminate\Http\Response
48
     */
49
    public function index(Request $request)
50
    {
51
        if (request()->has('sort')) {
52
            list($sortCol, $sortDir) = explode('|', request()->sort);
53
54
            $query = $this->pendaftaran->with('kegiatan')->with('user')->orderBy($sortCol, $sortDir);
55
        } else {
56
            $query = $this->pendaftaran->with('kegiatan')->with('user')->orderBy('id', 'asc');
57
        }
58
59
        if ($request->exists('filter')) {
60
            $query->where(function($q) use($request) {
61
                $value = "%{$request->filter}%";
62
                $q->where('label', 'like', $value)
63
                    ->orWhere('description', 'like', $value);
64
            });
65
        }
66
67
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
68
        $response = $query->paginate($perPage);
69
70
        // foreach($response as $kegiatan){
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
71
        //     array_set($response->data, 'kegiatan', $kegiatan->kegiatan->label);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
72
        // }
73
74
        // foreach($response as $user){
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
75
        //     array_set($response->data, 'user', $user->user->name);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
76
        // }
77
78
        return response()->json($response)
79
            ->header('Access-Control-Allow-Origin', '*')
80
            ->header('Access-Control-Allow-Methods', 'GET');
81
    }
82
83
    /**
84
     * Show the form for creating a new resource.
85
     *
86
     * @return \Illuminate\Http\Response
87
     */
88
    public function create()
89
    {
90
        $kegiatan = $this->kegiatanModel->all();
91
        $users_special = $this->user->all();
92
        $users_standar = $this->user->find(\Auth::User()->id);
93
        
94
        $role_check = \Auth::User()->hasRole(['superadministrator','administrator']);
95
96
        if($role_check){
97
            $response['user_special'] = 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...
98
            foreach($users_special as $user){
99
                array_set($user, 'label', $user->name);
100
            }
101
            $response['user'] = $users_special;
102
        }else{
103
            $response['user_special'] = 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...
104
            array_set($users_standar, 'label', $users_standar->name);
105
            $response['user'] = $users_standar;
106
        } 
107
108
        $response['kegiatan'] = $kegiatan;        
109
        $response['status'] = true;
110
111
        return response()->json($response);
112
    }
113
114
    /**
115
     * Display the specified resource.
116
     *
117
     * @param  \App\PendaftaranWizard  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. 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...
118
     * @return \Illuminate\Http\Response
119
     */
120
    public function store(Request $request)
121
    {
122
        $pendaftaran = $this->pendaftaran;
123
124
        $validator = Validator::make($request->all(), [
125
            'kegiatan_id' => 'required',
126
            'user_id' => 'required|max:16|unique:pendaftarans,user_id',
127
            'label' => 'required|max:16|unique:pendaftarans,label',
128
            'description' => 'max:255',
129
        ]);
130
131
        if($validator->fails()){
132
            $check = $pendaftaran->where('label',$request->label)->orWhere('user_id', $request->user_id)->whereNull('deleted_at')->count();
133
134 View Code Duplication
            if ($check > 0) {
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...
135
                $response['message'] = 'Failed, label or user pendaftaran 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...
136
            } else {
137
                $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
138
                $pendaftaran->user_id = $request->input('user_id');
139
                $pendaftaran->label = $request->input('label');
140
                $pendaftaran->description = $request->input('description');                
141
                $pendaftaran->save();
142
143
                $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...
144
            }
145
        } else {
146
            $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
147
            $pendaftaran->user_id = $request->input('user_id');
148
            $pendaftaran->label = $request->input('label');
149
            $pendaftaran->description = $request->input('description');            
150
            $pendaftaran->save();
151
            $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...
152
        }
153
154
        $response['status'] = true;
155
156
        return response()->json($response);
157
    }
158
159
    /**
160
     * Store a newly created resource in storage.
161
     *
162
     * @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...
163
     * @return \Illuminate\Http\Response
164
     */
165
    public function show($id)
166
    {
167
        $pendaftaran = $this->pendaftaran->findOrFail($id);
168
169
        $response['pendaftaran'] = $pendaftaran;
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...
170
        $response['kegiatan'] = $pendaftaran->kegiatan;
171
        $response['user'] = $pendaftaran->user;
172
        $response['status'] = true;
173
174
        return response()->json($response);
175
    }
176
177
    /**
178
     * Show the form for editing the specified resource.
179
     *
180
     * @param  \App\PendaftaranWizard  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. 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...
181
     * @return \Illuminate\Http\Response
182
     */
183
    public function edit($id)
184
    {
185
        $pendaftaran = $this->pendaftaran->findOrFail($id);
186
187
        array_set($pendaftaran->user, 'label', $pendaftaran->user->name);
188
189
        $response['pendaftaran'] = $pendaftaran;
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...
190
        $response['kegiatan'] = $pendaftaran->kegiatan;
191
        $response['user'] = $pendaftaran->user;
192
        $response['status'] = true;
193
194
        return response()->json($response);
195
    }
196
197
    /**
198
     * Update the specified resource in storage.
199
     *
200
     * @param  \Illuminate\Http\Request  $request
201
     * @param  \App\PendaftaranWizard  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. 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...
202
     * @return \Illuminate\Http\Response
203
     */
204
    public function update(Request $request, $id)
205
    {      
206
        $response = [];
207
                  
208
        $pendaftaran = $this->pendaftaran->findOrFail($id);
209
        if($request->old_label == $request->label && $request->user_id != $request->old_user_id){
210
            $validator = Validator::make($request->all(), [
211
                'label' => 'required',
212
                'description' => 'max:255',
213
                'kegiatan_id' => 'required',
214
                'user_id' => 'required|unique:pendaftarans,user_id',
215
            ]);
216
            $fail = "user_id";
217
        }elseif($request->old_label != $request->label && $request->user_id == $request->old_user_id){
218
            $validator = Validator::make($request->all(), [
219
                'label' => 'required|unique:pendaftarans,label',
220
                'description' => 'max:255',
221
                'kegiatan_id' => 'required',
222
                'user_id' => 'required',
223
            ]);
224
            $fail = "label";
225
        }elseif($request->old_label == $request->label && $request->user_id == $request->old_user_id){
226
            $validator = Validator::make($request->all(), [
227
                'label' => 'required',
228
                'description' => 'max:255',
229
                'kegiatan_id' => 'required',
230
                'user_id' => 'required',
231
            ]);
232
        }else{
233
            $validator = Validator::make($request->all(), [
234
                'label' => 'required|unique:pendaftarans,label',
235
                'description' => 'max:255',
236
                'kegiatan_id' => 'required',
237
                'user_id' => 'required|unique:pendaftarans,label',
238
            ]);
239
            $fail = "label & user_id";
240
        }
241
        if ($validator->fails()) {
242
            $check_user = $pendaftaran->where('user_id', $request->user_id)->whereNull('deleted_at')->count();
243
            $check_label = $pendaftaran->where('label',$request->label)->whereNull('deleted_at')->count();
244
            if($fail == "label"){
245 View Code Duplication
                if ($check_label > 0) {                    
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...
246
                    $response['message'] = 'Failed, label already exists';
247
                }else{
248
                    $pendaftaran->label = $request->input('label');
249
                    $pendaftaran->description = $request->input('description');
250
                    $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
251
                    $pendaftaran->user_id = $request->input('user_id');
252
                    $pendaftaran->save();
253
                    $response['message'] = 'success'; 
254
                }
255 View Code Duplication
            }elseif($fail == "user_id"){
0 ignored issues
show
Bug introduced by
The variable $fail 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...
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...
256
                if ($check_user > 0) {                    
257
                    $response['message'] = 'Failed, user already exists';
258
                }else{
259
                    $pendaftaran->label = $request->input('label');
260
                    $pendaftaran->description = $request->input('description');
261
                    $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
262
                    $pendaftaran->user_id = $request->input('user_id');
263
                    $pendaftaran->save();
264
                    $response['message'] = 'success';
265
                }
266
            }else{
267
                if ($check_user > 0 && $check_label > 0) {                    
268
                    $response['message'] = 'Failed, user and label already exists';
269
                }else{
270
                    $pendaftaran->label = $request->input('label');
271
                    $pendaftaran->description = $request->input('description');
272
                    $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
273
                    $pendaftaran->user_id = $request->input('user_id');
274
                    $pendaftaran->save();
275
                    $response['message'] = 'success';
276
                }
277
            }                       
278
        } else {
279
            $pendaftaran->label = $request->input('label');
280
            $pendaftaran->description = $request->input('description');
281
            $pendaftaran->kegiatan_id = $request->input('kegiatan_id');
282
            $pendaftaran->user_id = $request->input('user_id');
283
            $pendaftaran->save();
284
            $response['message'] = 'success';
285
        }
286
        $response['status'] = true;
287
        return response()->json($response);
288
    }
289
290
    /**
291
     * Remove the specified resource from storage.
292
     *
293
     * @param  \App\PendaftaranWizard  $pendaftaran
0 ignored issues
show
Bug introduced by
There is no parameter named $pendaftaran. 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...
294
     * @return \Illuminate\Http\Response
295
     */
296
    public function destroy($id)
297
    {
298
        $pendaftaran = $this->pendaftaran->findOrFail($id);
299
300
        if ($pendaftaran->delete()) {
301
            $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...
302
        } else {
303
            $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...
304
        }
305
306
        return json_encode($response);
307
    }
308
}
309