Completed
Pull Request — master (#73)
by Tim
02:41
created

RolesController::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Http\Requests;
7
8
use App\Roles;
9
10
class RolesController extends Controller
11
{
12
    /**
13
     * Get the role overview.
14
     *
15
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
16
     */
17
    public function index()
18
    {
19
    	$data['roles'] = Roles::all();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = 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...
20
    	return view('roles.index', $data);
21
    }
22
23
    /**
24
     * Search for a specific role.
25
     *
26
     * @param  Request $request
27
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
28
     */
29
    public function search(Request $request)
30
    {
31
        $term = $request->get('term');
32
        $data['roles'] = Roles::where('name', 'LIKE', "$term")->get();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = 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...
33
        return view('roles.index', $data);
34
    }
35
36
    /**
37
     * Create a role in the database.
38
     *
39
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
40
     */
41
    public function store()
42
    {
43
44
    }
45
46
    
47
48
    /**
49
     * Destroy a role out off the database.
50
     *
51
     * @param  int $id The id in the database for the role.
52
     * @return \Illuminate\Http\RedirectResponse
53
     */
54
    public function destroy($id)
55
    {
56
        Roles::destroy($id);
57
        session()->flash('message', 'Role deleted');
58
        return redirect()->back(302);
59
    }
60
}
61