UserListController::delete_game()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 9
nc 2
nop 2
1
<?php
2
3
/*
4
 * rmarchiv.tk
5
 * (c) 2016-2017 by Marcel 'ryg' Hering
6
 */
7
8
namespace App\Http\Controllers;
9
10
use Carbon\Carbon;
11
use App\Models\Game;
12
use App\Models\UserList;
13
use App\Models\UserListItem;
14
use Illuminate\Http\Request;
15
16
class UserListController extends Controller
17
{
18
    public function create()
19
    {
20
        return view('users.lists.create');
21
    }
22
23
    public function store(Request $request)
24
    {
25
        $this->validate($request, [
26
            'title' => 'required',
27
            'desc'  => 'required',
28
        ]);
29
30
        \DB::table('user_lists')->insert([
31
            'user_id'    => \Auth::id(),
32
            'title'      => $request->get('title'),
33
            'desc_md'    => $request->get('desc'),
34
            'desc_html'  => \Markdown::convertToHtml($request->get('desc')),
35
            'created_at' => Carbon::now(),
36
        ]);
37
38
        return \Redirect::back();
39
    }
40
41
    public function delete_game($listid, $itemid)
42
    {
43
        $list = UserList::whereId($listid)->first();
44
45
        if (\Auth::check() and \Auth::user()->id == $list->user_id) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
46
            \DB::table('user_list_items')
47
                ->where('list_id', '=', $listid)
48
                ->where('content_id', '=', $itemid)
49
                ->where('content_type', '=', 'game')
50
                ->delete();
51
        }
52
53
        return \Redirect::back();
54
    }
55
56
    public function delete($listid)
57
    {
58
        if (\Auth::check()) {
59
            if (\Auth::user()->hasRole('admin')) {
60
                \DB::table('user_list_items')
61
                    ->where('list_id', '=', $listid)
62
                    ->delete();
63
64
                \DB::table('user_lists')
65
                    ->where('id', '=', $listid)
66
                    ->delete();
67
            } else {
68
                $list = \DB::table('user_lists')
69
                    ->where('id', '=', $listid)
70
                    ->where('user_id', '=', \Auth::id())
71
                    ->get();
72
73
                if ($list->count() != 0) {
74
                    \DB::table('user_lists')
75
                        ->where('id', '=', $listid)
76
                        ->delete();
77
78
                    \DB::table('user_list_items')
79
                        ->where('list_id', '=', $listid)
80
                        ->delete();
81
                }
82
            }
83
        }
84
85
        return \Redirect::back();
86
    }
87
88
    public function add_game($listid, $gameid)
89
    {
90
        if (\Auth::check()) {
91
            $check = \DB::table('user_list_items')
92
                ->where('content_id', '=', $gameid)
93
                ->where('content_type', '=', 'game')
94
                ->where('list_id', '=', $listid)
95
                ->get();
96
            if ($check->count() == 0) {
97
                \DB::table('user_list_items')->insert([
98
                    'content_id'   => $gameid,
99
                    'content_type' => 'game',
100
                    'user_id'      => \Auth::id(),
101
                    'list_id'      => $listid,
102
                    'created_at'   => Carbon::now(),
103
                ]);
104
            }
105
        }
106
107
        return \Redirect::action('GameController@show', $gameid);
108
    }
109
110 View Code Duplication
    public function show($userid, $listid)
0 ignored issues
show
Unused Code introduced by
The parameter $userid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
111
    {
112
        $list = UserList::whereId($listid)->first();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
113
        $arr = UserListItem::whereListId($listid)->pluck('content_id')->toArray();
0 ignored issues
show
Bug introduced by
The method pluck does only exist in Illuminate\Database\Query\Builder, but not in App\Models\UserListItem.

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...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
114
        $games = Game::find($arr);
115
116
        return view('users.lists.show', [
117
            'list'      => $list,
118
            'games'     => $games,
119
            'orderby'   => '',
120
            'direction' => '',
121
        ]);
122
    }
123
124
    public function index($userid)
125
    {
126
        $lists = \DB::table('user_lists')
127
            ->where('user_id', '=', $userid)
128
            ->select([
129
                'id',
130
                'title',
131
                'user_id',
132
                'created_at',
133
            ])
134
            ->selectRaw('(SELECT COUNT(*) FROM user_list_items WHERE list_id = user_lists.id) as count')
135
            ->orderBy('title')
136
            ->get();
137
138
        return view('users.lists.index', [
139
            'lists' => $lists,
140
        ]);
141
    }
142
}
143