ForumController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 38.81 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 2
dl 26
loc 67
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 5 1
A view() 13 13 2
A createPost() 13 13 2
A validateForumId() 0 9 2
A viewPost() 0 13 2

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 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