SnippetsController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 71
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 14 1
A create() 0 4 1
A show() 0 6 1
A store() 0 14 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use DB;
6
use App\Snippet;
7
8
class SnippetsController extends Controller
9
{
10
    public function __construct()
11
    {
12
        $this->middleware('auth');
13
    }
14
15
    /**
16
     * List all of the snippets.
17
     *
18
     * @return \Response
19
     */
20
    public function index()
21
    {
22
        $snippets = Snippet::with('user')->latest()->paginate(5);
23
        $topUsers = DB::table('snippets_votes')
24
                        ->join('snippets', 'snippets.id', '=', 'snippets_votes.snippet_id')
25
                        ->join('users', 'users.id', '=', 'snippets.user_id')
26
                        ->select(DB::raw('count(*) as votes, name, username'))
27
                        ->groupBy('snippets.user_id')
28
                        ->orderBy('votes', 'desc')
29
                        ->limit(10)
30
                        ->get();
31
32
        return view('snippets.index', compact('snippets', 'topUsers'));
33
    }
34
35
    /**
36
     * Show a page to create a new snippet.
37
     *
38
     * @param  Snippet $snippet
39
     * @return \Response
40
     */
41
    public function create(Snippet $snippet)
42
    {
43
        return view('snippets.create', compact('snippet'));
44
    }
45
46
    /**
47
     * Show a single snippet.
48
     *
49
     * @param  Snippet $snippet
50
     * @return \Response
51
     */
52
    public function show(Snippet $snippet)
53
    {
54
        $snippet->load('forks');
55
56
        return view('snippets.show', compact('snippet'));
57
    }
58
59
    /**
60
     * Store a new snippet in the database.
61
     *
62
     * @return \RedirectResponse
63
     */
64
    public function store()
65
    {
66
        $this->validate(request(), [
67
            'title' => 'required',
68
            'body' => 'required',
69
        ]);
70
71
        $data = request()->only(['title', 'body', 'forked_id']);
72
        $data['user_id'] = auth()->user()->id;
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
73
74
        Snippet::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Snippet. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
75
76
        return redirect()->home();
77
    }
78
}
79