Completed
Pull Request — master (#9)
by
unknown
02:32
created

ProdiSekolahController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 266
Duplicated Lines 39.85 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 106
loc 266
rs 10
c 0
b 0
f 0
wmc 19
lcom 2
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B index() 14 39 4
A create() 0 21 3
B store() 26 43 3
A show() 14 14 1
A edit() 15 15 1
A update() 25 55 4
A destroy() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bantenprov\Sekolah\Http\Controllers;
4
5
/* Require */
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Bantenprov\Sekolah\Facades\SekolahFacade;
9
use App\User;
10
/* Models */
11
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\Sekolah;
12
use Bantenprov\Sekolah\Models\Bantenprov\Sekolah\ProdiSekolah;
13
use Bantenprov\ProgramKeahlian\Models\Bantenprov\ProgramKeahlian\ProgramKeahlian;
14
15
/* Etc */
16
use Validator;
17
18
/**
19
 * The SekolahController class.
20
 *
21
 * @package Bantenprov\Sekolah
22
 * @author  bantenprov <[email protected]>
23
 */
24
class ProdiSekolahController extends Controller
25
{
26
    /**
27
     * Create a new controller instance.
28
     *
29
     * @return void
30
     */
31
    protected $user;
32
    protected $prodi_sekolah;
33
    protected $sekolah;
34
    protected $program_keahlian;
35
    public function __construct(Sekolah $sekolah, User $user, ProdiSekolah $prodi_sekolah, ProgramKeahlian $program_keahlian)
36
    {
37
        $this->sekolah          = $sekolah;
38
        $this->user             = $user;
39
        $this->prodi_sekolah    = $prodi_sekolah;
40
        $this->program_keahlian = $program_keahlian;
41
    }
42
43
    /**
44
     * Display a listing of the resource.
45
     *
46
     * @return \Illuminate\Http\Response
47
     */
48
    public function index(Request $request)
49
    {
50 View Code Duplication
        if (request()->has('sort')) {
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...
51
            list($sortCol, $sortDir) = explode('|', request()->sort);
52
53
            $query = $this->prodi_sekolah->orderBy($sortCol, $sortDir);
54
        } else {
55
            $query = $this->prodi_sekolah->orderBy('id', 'asc');
56
        }
57
58 View Code Duplication
        if ($request->exists('filter')) {
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...
59
            $query->where(function($q) use($request) {
60
                $value = "%{$request->filter}%";
61
                $q->where('keterangana', 'like', $value)                    
62
                    ->orWhere('kuota_siswa', 'like', $value);
63
            });
64
        }
65
66
        $perPage = request()->has('per_page') ? (int) request()->per_page : null;
67
        $response = $query->with('user')->with('sekolah')->with('program_keahlian')->paginate($perPage);
68
        
69
        
70
        /*foreach($response as $sekolah){
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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, 'sekolah', $sekolah->sekolah->label);
72
        }         
73
74
        foreach($response as $user){
75
            array_set($response->data, 'user', $user->user->name);
76
        }
77
78
        foreach($response as $program_keahlian){
79
            array_set($response->data, 'program_keahlian', $program_keahlian->program_keahlian->label);
80
        }*/
81
82
83
        return response()->json($response)
84
            ->header('Access-Control-Allow-Origin', '*')
85
            ->header('Access-Control-Allow-Methods', 'GET');
86
    }
87
88
    
89
     /** Show the form for creating a new resource.
90
     *
91
     * @return \Illuminate\Http\Response*/
92
     
93
    public function create()
94
    {        
95
        $users              = $this->user->all();
96
        $sekolahs           = $this->sekolah->all();
97
        $program_keahlians  = $this->program_keahlian->all();
98
99
        foreach($users as $user){
100
            array_set($user, 'label', $user->name);
101
        }
102
103
        foreach($program_keahlians as $program_keahlian){
104
            array_set($program_keahlian, 'program_keahlian', $program_keahlian->program_keahlian);
105
        }
106
107
        
108
        $response['sekolah'] = $sekolahs;
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...
109
        $response['program_keahlian'] = $program_keahlians;
110
        $response['user'] = $users;
111
        $response['status'] = true;
112
        return response()->json($response);
113
    }
114
115
    /**
116
     * Display the specified resource.
117
     *
118
     * @param  \App\Sekolah  $sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $sekolah. 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...
119
     * @return \Illuminate\Http\Response
120
     */
121
    public function store(Request $request)
122
    {
123
        $prodi_sekolah = $this->prodi_sekolah;
124
125
        $validator = Validator::make($request->all(), [
126
            'keterangan'            => 'required',
127
            'kuota_siswa'           => 'required',
128
            'program_keahlian_id'   => 'required',
129
            'sekolah_id'            => 'required',
130
            'user_id'               => 'required|unique:prodi_sekolahs,user_id',
131
        ]);
132
133 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...
134
            $check = $prodi_sekolah->where('user_id',$request->user_id)->whereNull('deleted_at')->count();
135
136
            if ($check > 0) {
137
                $response['message'] = 'Failed, user  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...
138
139
            } else {
140
                $prodi_sekolah->sekolah_id            = $request->input('sekolah_id');
141
                $prodi_sekolah->user_id               = $request->input('user_id');
142
                $prodi_sekolah->program_keahlian_id   = $request->input('program_keahlian_id');
143
                $prodi_sekolah->keterangan            = $request->input('keterangan');
144
                $prodi_sekolah->kuota_siswa           = $request->input('kuota_siswa');
145
                $prodi_sekolah->save();
146
147
                $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...
148
            }
149
        } else {
150
                $prodi_sekolah->sekolah_id            = $request->input('sekolah_id');
151
                $prodi_sekolah->user_id               = $request->input('user_id');
152
                $prodi_sekolah->program_keahlian_id   = $request->input('program_keahlian_id');
153
                $prodi_sekolah->keterangan            = $request->input('keterangan');
154
                $prodi_sekolah->kuota_siswa           = $request->input('kuota_siswa');
155
                $prodi_sekolah->save();
156
157
            $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...
158
        }
159
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
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...
169
     * @return \Illuminate\Http\Response
170
     */
171 View Code Duplication
    public function show($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
172
     {
173
         $prodi_sekolah = $this->prodi_sekolah->findOrFail($id);
174
175
         array_set($prodi_sekolah, 'user', $prodi_sekolah->user->name);
176
         array_set($prodi_sekolah, 'sekolah', $prodi_sekolah->sekolah->label);
177
         array_set($prodi_sekolah, 'program_keahlian', $prodi_sekolah->program_keahlian->label);
178
179
         $response['sekolah']           = $prodi_sekolah;
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...
180
         $response['program_keahlian']  = $prodi_sekolah;
181
         $response['status']            = true;
182
183
         return response()->json($response);
184
     }
185
186
    /**
187
     * Show the form for editing the specified resource.
188
     *
189
     * @param  \App\Sekolah  $sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $sekolah. 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...
190
     * @return \Illuminate\Http\Response
191
     */
192 View Code Duplication
    public function edit($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
193
    {
194
        $prodi_sekolah = $this->prodi_sekolah->findOrFail($id);
195
196
        array_set($prodi_sekolah->user, 'label', $prodi_sekolah->user->name);
197
        array_set($prodi_sekolah->program_keahlian, 'program_keahlian', $prodi_sekolah->program_keahlian->label);
198
199
        $response['user']               = $prodi_sekolah->user;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
200
        $response['sekolah']            = $prodi_sekolah;
201
        $response['prodi_sekolah']      = $prodi_sekolah->sekolah;
202
        $Response['program_keahlian']   = $prodi_sekolah->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...
203
        $response['status'] = true;
204
205
        return response()->json($response);
206
    }
207
208
    /**
209
     * Update the specified resource in storage.
210
     *
211
     * @param  \Illuminate\Http\Request  $request
212
     * @param  \App\Sekolah  $sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $sekolah. 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...
213
     * @return \Illuminate\Http\Response
214
     */
215
    public function update(Request $request, $id)
216
    {
217
        $prodi_sekolah = $this->prodi_sekolah->findOrFail($id);
218
219
        if ($request->input('old_user_id') == $request->input('user_id'))
220
        {
221
            $validator = Validator::make($request->all(), [
222
                'sekolah_id'             => 'required',
223
                'user_id'                => 'required',
224
                'keterangan'             => 'required',
225
                'kuota_siswa'            => 'required',
226
                'program_keahlian_id'    => 'required',
227
228
229
            ]);
230
        } else {
231
            $validator = Validator::make($request->all(), [
232
                'sekolah_id'             => 'required',
233
                'user_id'                => 'required|unique:prodi_sekolahs,user_id',
234
                'keterangan'             => 'required',
235
                'kuota_siswa'            => 'required',
236
                'program_keahlian_id'    => 'required',
237
            ]);
238
        }
239
240 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...
241
            $check = $prodi_sekolah->where('user_id',$request->user_id)->whereNull('deleted_at')->count();
242
243
            if ($check > 0) {
244
                $response['message'] = 'Failed, user  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...
245
            } else {
246
                $prodi_sekolah->sekolah_id          = $request->input('sekolah_id');
247
                $prodi_sekolah->user_id             = $request->input('user_id');
248
                $prodi_sekolah->program_keahlian_id = $request->input('program_keahlian_id');
249
                $prodi_sekolah->keterangan          = $request->input('keterangan');
250
                $prodi_sekolah->kuota_siswa         = $request->input('kuota_siswa');
251
                $prodi_sekolah->save();
252
253
                $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...
254
            }
255
        } else {
256
                $prodi_sekolah->sekolah_id      = $request->input('sekolah_id');
257
                $prodi_sekolah->user_id         = $request->input('user_id');
258
                $prodi_sekolah->program_keahlian_id = $request->input('program_keahlian_id');
259
                $prodi_sekolah->keterangan      = $request->input('keterangan');
260
                $prodi_sekolah->kuota_siswa     = $request->input('kuota_siswa');
261
                $prodi_sekolah->save();
262
263
            $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...
264
        }
265
266
        $response['status'] = true;
267
268
        return response()->json($response);
269
    }
270
271
    /**
272
     * Remove the specified resource from storage.
273
     *
274
     * @param  \App\Sekolah  $sekolah
0 ignored issues
show
Bug introduced by
There is no parameter named $sekolah. 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...
275
     * @return \Illuminate\Http\Response
276
     */
277 View Code Duplication
    public function destroy($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
278
    {
279
        $prodi_sekolah = $this->prodi_sekolah->findOrFail($id);
280
281
        if ($prodi_sekolah->delete()) {
282
            $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...
283
        } else {
284
            $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...
285
        }
286
287
        return json_encode($response);
288
    }
289
}
290