BoardController::update_vote()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
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\Events\Obyx;
12
use App\Models\BoardCat;
13
use App\Models\BoardPoll;
14
use App\Models\BoardPost;
15
use App\Models\BoardThread;
16
use Illuminate\Http\Request;
17
use App\Models\BoardPollVote;
18
use App\Helpers\DatabaseHelper;
19
use App\Models\BoardPollAnswer;
20
use Cmgmyr\Messenger\Models\Thread;
21
use Illuminate\Support\Facades\Input;
22
23
class BoardController extends Controller
24
{
25
    public function index()
26
    {
27
        $cats = BoardCat::with('last_user', 'threads')->orderBy('order')->get();
28
29
        return view('board.index', [
30
            'cats' => $cats,
31
        ]);
32
    }
33
34
    public function show_cat($catid)
35
    {
36
        $thr = BoardThread::with('user', 'cat', 'last_user', 'posts')
37
            ->whereCatId($catid)
38
            ->orderBy('board_threads.pinned', 'desc')
39
            ->orderBy('board_threads.last_created_at', 'desc')
40
            ->orderBy('board_threads.id', 'desc')
41
            ->paginate(25);
42
43
        $cat = BoardCat::whereId($catid)->first();
44
45
        return view('board.threads.index', [
46
            'threads' => $thr,
47
            'cat'     => $cat,
48
        ]);
49
    }
50
51
    public function create_cat()
52
    {
53
        if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) {
54
            $cats = \DB::table('board_cats')
55
                ->select([
56
                    'id as catid',
57
                    'title as cattitle',
58
                    'order as catorder',
59
                    'created_at as catdate',
60
                ])
61
                ->selectRaw('(SELECT COUNT(id) FROM board_threads WHERE cat_id = board_cats.id) as catthreads')
62
                ->selectRaw('(SELECT COUNT(id) FROM board_posts WHERE cat_id = board_cats.id) as catposts')
63
                ->orderBy('board_cats.order')
64
                ->get();
65
66
            return view('board.cats.create', [
67
                'cats' => $cats,
68
            ]);
69
        }
70
    }
71
72
    public function order_cat($catid, $direction)
73
    {
74
        if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) {
75
            if ($direction == 'up') {
76
                \DB::table('board_cats')
77
                    ->where('id', '=', $catid)
78
                    ->increment('order');
79
            } else {
80
                \DB::table('board_cats')
81
                    ->where('id', '=', $catid)
82
                    ->decrement('order');
83
            }
84
        }
85
86
        return redirect()->action('BoardController@create_cat');
87
    }
88
89
    public function store_cat(Request $request)
90
    {
91
        $this->validate($request, [
92
            'name' => 'required',
93
            'desc' => 'required',
94
        ]);
95
96
        if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) {
97
            \DB::table('board_cats')->insert([
98
                'order'      => 0,
99
                'title'      => $request->get('name'),
100
                'desc'       => $request->get('desc'),
101
                'created_at' => \Auth::id(),
102
            ]);
103
        }
104
105
        return redirect()->action('BoardController@create_cat');
106
    }
107
108
    public function show_thread($threadid)
109
    {
110
        $posts = BoardPost::with('user', 'thread', 'cat')->whereThreadId($threadid)->orderBy('id')->paginate(25);
111
        $poll = BoardPoll::whereThreadId($threadid)->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...
112
113
        $pollanswers = null;
114
        $canvote = 1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
115
        $votecount = 0;
0 ignored issues
show
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...
116
        $votes = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
117
118
        if ($poll) {
119
            $pollanswers = BoardPollAnswer::with('votes')->wherePollId($poll->id)->get();
120
            $polls = BoardPollVote::wherePollId($poll->id)->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Query\Builder, but not in App\Models\BoardPollVote.

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 7 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...
121
            $votecount = $polls->count();
0 ignored issues
show
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...
122
123
            $votes = $polls->where('user_id', '=', \Auth::id());
124
125
            if ($votes->count() != 0) {
126
                $canvote = 0;
127
            }
128
        }
129
130
        DatabaseHelper::setThreadViewDate($threadid);
131
132
        if (! Input::get('page')) {
133
            return redirect('board/thread/'.$threadid.'?page='.$posts->lastPage());
134
        } else {
135
            return view('board.threads.show', [
136
                'posts'     => $posts,
137
                'poll'      => $poll,
138
                'answers'   => $pollanswers,
139
                'votecount' => $votecount,
140
                'canvote'   => $canvote,
141
                'votes'     => $votes,
142
            ]);
143
        }
144
    }
145
146
    public function store_thread(Request $request)
147
    {
148
        $date = Carbon::now();
149
150
        $threadid = \DB::table('board_threads')->insertGetId([
151
            'cat_id'          => $request->get('category'),
152
            'user_id'         => \Auth::id(),
153
            'title'           => $request->get('topic'),
154
            'closed'          => 0,
155
            'pinned'          => 0,
156
            'last_user_id'    => \Auth::id(),
157
            'created_at'      => $date,
158
            'last_created_at' => $date,
159
        ]);
160
161
        event(new Obyx('thread-add', \Auth::id()));
162
163
        \DB::table('board_posts')->insert([
164
            'cat_id'       => $request->get('category'),
165
            'thread_id'    => $threadid,
166
            'user_id'      => \Auth::id(),
167
            'content_md'   => $request->get('msg'),
168
            'content_html' => \Markdown::convertToHtml($request->get('msg')),
169
            'created_at'   => $date,
170
        ]);
171
172
        DatabaseHelper::setThreadViewDate($threadid);
173
174
        return redirect()->action('BoardController@show_thread', [$threadid]);
175
    }
176
177
    public function store_post(Request $request, $threadid)
178
    {
179
        $this->validate($request, [
180
            'catid'   => 'required',
181
            'msg'     => 'required',
182
        ]);
183
184
        $check = BoardThread::whereId($threadid)->first();
185
        if ($check->closed == 0) {
186
            $date = Carbon::now();
187
188
            $pid = \DB::table('board_posts')->insertGetId([
189
                'user_id'      => \Auth::id(),
190
                'cat_id'       => $request->get('catid'),
191
                'thread_id'    => $threadid,
192
                'content_md'   => $request->get('msg'),
193
                'content_html' => \Markdown::convertToHtml($request->get('msg')),
194
                'created_at'   => $date,
195
            ]);
196
197
            \DB::table('board_threads')
198
                ->where('id', '=', $threadid)
199
                ->update([
200
                    'last_created_at' => $date,
201
                    'last_user_id'    => \Auth::id(),
202
                ]);
203
204
            \DB::table('board_cats')
205
                ->where('id', '=', $request->get('catid'))
206
                ->update([
207
                    'last_created_at' => $date,
208
                    'last_user_id'    => \Auth::id(),
209
                ]);
210
211
            event(new Obyx('post-add', \Auth::id()));
212
213
            $url = \URL::route('board.thread.show', [$threadid]).'#c'.$pid;
214
        } else {
215
            $url = \URL::route('board.thread.show', [$threadid]);
216
        }
217
218
        return redirect()->to($url);
219
    }
220
221
    public function thread_close_switch($id, $state)
222
    {
223
        if (\Auth::check()) {
224
            if (\Auth::user()->can('mod-threads')) {
225
                if (is_numeric($id)) {
226
                    if ($state == 1 || $state == 0) {
227
                        \DB::table('board_threads')
228
                            ->where('id', '=', $id)
229
                            ->update([
230
                                'closed' => $state,
231
                            ]);
232
                    }
233
                }
234
            }
235
        }
236
237
        return redirect()->action('BoardController@show_thread', $id);
238
    }
239
240
    public function post_edit($threadid, $postid)
0 ignored issues
show
Unused Code introduced by
The parameter $threadid 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...
241
    {
242
        $post = BoardPost::whereId($postid)->first();
243
244
        return view('board.post.edit', [
245
            'post' => $post,
246
        ]);
247
    }
248
249
    public function post_update(Request $request, $threadid, $postid)
250
    {
251
        if (\Auth::check()) {
252
            $this->validate($request, [
253
                'thread_id' => 'required',
254
                'post_id'   => 'required',
255
                'msg'       => 'required',
256
                'title'     => 'required',
257
            ]);
258
259
            $post = BoardPost::whereId($postid)->first();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 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...
260
            $post->content_md = $request->get('msg');
0 ignored issues
show
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...
261
            $post->content_html = \Markdown::convertToHtml($request->get('msg'));
262
            $post->save();
263
264
            $thread = BoardThread::whereId($threadid)->first();
265
266
            $ttitle = $request->get('title');
267
268
            if ($ttitle != $thread->title and $ttitle != '') {
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...
269
                $thread->title = $request->get('title');
270
                $thread->save();
271
            }
272
        }
273
274
        return redirect()->action('BoardController@show_thread', $threadid);
275
    }
276
277
    public function create_vote($threadid)
278
    {
279
        $check = BoardPoll::whereThreadId($threadid)->get();
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Query\Builder, but not in App\Models\BoardPoll.

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 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...
280
        $thread = BoardThread::whereId($threadid)->first();
281
        $cat = $thread->cat;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
282
283
        $edit = 0;
284
285
        if ($check) {
286
            $edit = 1;
287
        }
288
289
        return view('board.threads.vote', [
290
            'edit'      => $edit,
291
            'thread_id' => $threadid,
292
            'thread'    => $thread,
293
            'cat'       => $cat,
294
        ]);
295
    }
296
297
    public function store_vote(Request $request, $threadid)
0 ignored issues
show
Unused Code introduced by
The parameter $threadid 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...
298
    {
299
        $this->validate($request, [
300
            'thread_id' => 'required',
301
            'question'  => 'required',
302
            'answer0'   => 'required',
303
            'answer1'   => 'required',
304
        ]);
305
306
        $poll = new BoardPoll();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
307
        $poll->user_id = \Auth::id();
0 ignored issues
show
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...
308
        $poll->title = $request->get('question');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
309
        $poll->thread_id = $request->get('thread_id');
310
        $poll->save();
311
312
        for ($i = 0; $i < 10; $i++) {
313
            $pollAnswers = new BoardPollAnswer();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
314
            $pollAnswers->title = $request->get('answer'.$i);
0 ignored issues
show
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...
315
            $pollAnswers->user_id = \Auth::id();
316
            $pollAnswers->poll_id = $poll->id;
317
            $pollAnswers->save();
318
        }
319
320
        return redirect()->action('BoardController@show_thread', $request->get('thread_id'));
321
    }
322
323
    public function add_vote(Request $request)
324
    {
325
        if (\Auth::check()) {
326
            $c = BoardPollVote::where('poll_id', '=', $request->get('poll_id'))
327
                ->where('user_id', '=', \Auth::id())->first();
328
329
            if ($c) {
330
                $c->poll_id = $request->get('poll_id');
0 ignored issues
show
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...
331
                $c->user_id = \Auth::id();
0 ignored issues
show
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...
332
                $c->answer_id = $request->get('answer_id');
333
                $c->save();
334
            } else {
335
                $vote = new BoardPollVote();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
336
                $vote->poll_id = $request->get('poll_id');
0 ignored issues
show
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...
337
                $vote->user_id = \Auth::id();
0 ignored issues
show
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...
338
                $vote->answer_id = $request->get('answer_id');
339
                $vote->save();
340
            }
341
        }
342
343
        return redirect()->action('BoardController@show_thread', $request->get('thread_id'));
344
    }
345
346
    public function update_vote(Request $request, $threadid)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $threadid 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...
347
    {
348
    }
349
}
350