BaseController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 9
Bugs 1 Features 0
Metric Value
wmc 10
c 9
b 1
f 0
lcom 1
cbo 4
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A canModerate() 0 15 4
A canCreateForums() 0 13 3
A buildData() 0 16 2
1
<?php namespace Taskforcedev\LaravelForum\Http\Controllers;
2
3
use \Auth;
4
use \Schema;
5
use Taskforcedev\LaravelSupport\Http\Controllers\Controller;
6
use Taskforcedev\LaravelForum\Models\Forum;
7
use Taskforcedev\LaravelForum\Models\ForumCategory;
8
use Taskforcedev\LaravelForum\Helpers\UserHelper;
9
use Taskforcedev\LaravelForum\Helpers\Sanitizer;
10
11
/**
12
 * Class BaseController
13
 * @package Taskforcedev\LaravelForum\Http\Controllers
14
 */
15
class BaseController extends Controller
16
{
17
    public $sanitizer;
18
19
    public function __construct()
20
    {
21
        $this->sanitizer = new Sanitizer();
22
    }
23
24
    /**
25
     * Determine if the user can moderate the forums.
26
     * @return boolean
27
     */
28
    protected function canModerate()
29
    {
30
        $user = $this->getUser();
31
32
        if ($user->name == 'Guest' && $user->email == '[email protected]') {
33
            return false;
34
        }
35
36
        if (method_exists($user, 'can')) {
37
            return $user->can('forum-moderate');
38
        }
39
40
        // If no method of authorizing return false;
41
        return false;
42
    }
43
44
    public function canCreateForums()
45
    {
46
        if (!Auth::check()) {
47
            return false;
48
        }
49
50
        try {
51
            $user = Auth::user();
52
            return $user->can('create', Forum::class);
53
        } catch (\Exception $e) {
54
            return false;
55
        }
56
    }
57
58
    /**
59
     * Builds data required for views.
60
     * @return array
61
     */
62
    protected function buildData($data = [])
63
    {
64
        $user = $this->getUser();
65
        $data['user'] = $user;
66
        $data['layout'] = $this->getLayout();
67
        $data['categories'] = ForumCategory::with('forums')->get();
0 ignored issues
show
Bug introduced by
The method get 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...
68
        $data['sitename'] = $this->getSitename();
69
        $data['wysiwyg'] = config('laravel-forum.wysiwyg');
70
        $data['sanitizer'] = $this->sanitizer;
71
        $data['userHelper'] = new UserHelper();
72
        if (Auth::check()) {
73
            $data['isAdmin'] = $user->can('create', Forum::class);
74
        }
75
76
        return $data;
77
    }
78
}
79