SiteController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 21.31 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 26
loc 122
ccs 0
cts 39
cp 0
rs 10
c 2
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A index() 0 5 1

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 App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\DataTables\SiteDataTable;
7
use App\Site;
8
9
class SiteController extends Controller
10
{
11
    /**
12
     * Create a new controller instance.
13
     */
14
    public function __construct()
15
    {
16
        $this->middleware('auth');
17
    }
18
    
19
    /**
20
     * Display a listing of the resource.
21
     *
22
     * @param  SiteDataTable   $dataTable
23
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
24
     */
25
    public function index(SiteDataTable $dataTable)
26
    {
27
        $this->authorize('index', Site::class);
28
        
29
        return $dataTable->render('site.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $dataTable->render('site.index') also could return the type Illuminate\View\View|callable which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
30
    }
31
    
32
    /**
33
     * Show the form for creating a new resource.
34
     *
35
     * @return \Illuminate\Http\Response
36
     */
37
    public function create()
38
    {
39
        $this->authorize('create', Site::class);
40
        
41
        return view('site.create');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('site.create') returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
42
    }
43
    
44
    /**
45
     * Store a newly created resource in storage.
46
     *
47
     * @param  \Illuminate\Http\Request  $request
48
     * @return \Illuminate\Http\RedirectResponse
49
     */
50 View Code Duplication
    public function store(Request $request)
51
    {
52
        $this->authorize('store', Site::class);
53
        
54
        $request->validate([
55
            'name' => 'required|min:2|max:190|name|unique:sites,name',
56
        ]);
57
        
58
        $site = Site::create([ 'name' => $request->name ]);
59
        
60
        return redirect()->route('site.show', $site->id)
0 ignored issues
show
Bug introduced by
$site->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        return redirect()->route('site.show', /** @scrutinizer ignore-type */ $site->id)
Loading history...
61
            ->with('success', 'Site created successfully');
62
    }
63
    
64
    /**
65
     * Display the specified resource.
66
     *
67
     * @param  string  $id
68
     * @return \Illuminate\Http\Response
69
     */
70
    public function show($id)
71
    {
72
        $this->authorize('show', Site::class);
73
        
74
        $site = Site::findOrFail($id);
75
        $locations = $site->locations()->orderBy('name', 'ASC')->paginate(15);
76
    
77
        if (\Request::ajax())
78
            return response()->json(['site' => $site, 'locations' => $locations]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...ations' => $locations)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
79
        else
80
            return view('site.show', [ 'site' => $site, 'locations' => $locations ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('site.show',...ations' => $locations)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
81
    }
82
    
83
    /**
84
     * Show the form for editing the specified resource.
85
     *
86
     * @param  string  $id
87
     * @return \Illuminate\Http\Response
88
     */
89
    public function edit($id)
90
    {
91
        $this->authorize('edit', Site::class);
92
        
93
        $site = Site::findOrFail($id);
94
        return view('site.edit', [ 'site' => $site ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('site.edit', array('site' => $site)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
95
    }
96
    
97
    /**
98
     * Update the specified resource in storage.
99
     *
100
     * @param  \Illuminate\Http\Request  $request
101
     * @param  Site  $site
102
     * @return \Illuminate\Http\RedirectResponse
103
     */
104 View Code Duplication
    public function update(Request $request, Site $site)
105
    {
106
        $this->authorize('update', Site::class);
107
        
108
        $request->validate([
109
            'name' => 'required|min:2|max:190|name|unique:sites,name,'.$site->id,
110
        ]);
111
        
112
        $site->update([ 'name' => $request->name ]);
113
        
114
        return redirect()->route('site.show', $site->id)
0 ignored issues
show
Bug introduced by
$site->id of type integer is incompatible with the type array expected by parameter $parameters of Illuminate\Routing\Redirector::route(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
        return redirect()->route('site.show', /** @scrutinizer ignore-type */ $site->id)
Loading history...
115
            ->with('success', 'Site updated successfully');
116
    }
117
    
118
    /**
119
     * Remove the specified resource from storage.
120
     *
121
     * @param  string  $id
122
     * @return \Illuminate\Http\RedirectResponse
123
     */
124
    public function destroy($id)
125
    {
126
        $this->authorize('destroy', Site::class);
127
        
128
        Site::findOrFail($id)->delete();
129
        return redirect()->route('site.index')
130
            ->with('success', 'Site deleted successfully');
131
    }
132
}
133