ForumController::view()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php namespace Taskforcedev\LaravelForum\Http\Controllers;
2
3
use \Exception;
4
use Taskforcedev\LaravelForum\Models\Forum;
5
use Taskforcedev\LaravelForum\Models\ForumPost;
6
7
/**
8
 * Class ForumController
9
 * @package Taskforcedev\LaravelForum\Http\Controllers
10
 */
11
class ForumController extends BaseController
12
{
13
    public function index()
14
    {
15
        $data = $this->buildData();
16
        return view('laravel-forum::forum.index', $data);
17
    }
18
19 View Code Duplication
    public function view($id)
20
    {
21
        $forum = $this->validateForumId($id);
22
23
        if ($forum === false) {
24
            return view('laravel-forum::errors.404');
25
        }
26
27
        $data = $this->buildData();
28
        $data['forum'] = $forum;
29
30
        return view('laravel-forum::forum.forum', $data);
31
    }
32
33 View Code Duplication
    public function createPost($forum_id)
34
    {
35
        $forum = $this->validateForumId($forum_id);
36
37
        if ($forum === false) {
38
            return view('laravel-forum::errors.404');
39
        }
40
41
        $data = $this->buildData();
42
        $data['forum'] = $forum;
43
44
        return view('laravel-forum::forum.createPost', $data);
45
    }
46
47
    public function viewPost($forum_id, $post_id)
48
    {
49
        try {
50
            $post = ForumPost::with('replies')->where('id', $post_id)->firstOrFail();
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
51
52
            $data = $this->buildData();
53
            $data['post'] = $post;
54
55
            return view('laravel-forum::forum.thread', $data);
56
        } catch (Exception $e) {
57
            return view('laravel-forum::errors.404');
58
        }
59
    }
60
61
    /**
62
     * Validates if the forum exists, if so returns it.
63
     *
64
     * @param integer $id
65
     *
66
     * @return mixed
67
     */
68
    private function validateForumId($id)
69
    {
70
        try {
71
            $forum = Forum::where('id', $id)->firstOrFail();
72
            return $forum;
73
        } catch (\Exception $e) {
74
            return false;
75
        }
76
    }
77
}
78