Completed
Push — master ( c1c44a...e20669 )
by
unknown
02:22 queued 18s
created

BaseController::canCreateForums()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 3
nop 0
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\ForumCategory;
7
use Taskforcedev\LaravelForum\Helpers\UserHelper;
8
use Taskforcedev\LaravelForum\Helpers\Sanitizer;
9
10
/**
11
 * Class BaseController
12
 * @package Taskforcedev\LaravelForum\Http\Controllers
13
 */
14
class BaseController extends Controller
15
{
16
    public $sanitizer;
17
18
    public function __construct()
19
    {
20
        $this->sanitizer = new Sanitizer();
21
    }
22
23
    /**
24
     * Determine if the user can moderate the forums.
25
     * @return boolean
26
     */
27
    protected function canModerate()
28
    {
29
        $user = $this->getUser();
30
31
        if ($user->name == 'Guest' && $user->email == '[email protected]') {
32
            return false;
33
        }
34
35
        if (method_exists($user, 'can')) {
36
            return $user->can('forum-moderate');
37
        }
38
39
        // If no method of authorizing return false;
40
        return false;
41
    }
42
43
    public function canCreateForums()
44
    {
45
        try {
46
            $user = Auth::user();
47
            return $user->can('create', Forum::class);
48
        } catch (\Exception $e) {
49
            return false;
50
        }
51
    }
52
53
    /**
54
     * Builds data required for views.
55
     * @return array
56
     */
57
    protected function buildData($data = [])
58
    {
59
        $data['user'] = $this->getUser();
60
        $data['layout'] = $this->getLayout();
61
        $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...
62
        $data['sitename'] = $this->getSitename();
63
        $data['wysiwyg'] = config('laravel-forum.wysiwyg');
64
        $data['sanitizer'] = $this->sanitizer;
65
        $data['userHelper'] = new UserHelper();
66
67
        return $data;
68
    }
69
}
70