CreateRequest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 41
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 4 1
A rules() 0 6 1
A failedAuthorization() 0 14 3
1
<?php
2
3
namespace Faithgen\Discussions\Http\Requests;
4
5
use Faithgen\Discussions\Models\Discussion;
6
use Faithgen\Discussions\Traits\SavesDiscussion;
7
use Illuminate\Auth\Access\AuthorizationException;
8
use Illuminate\Foundation\Http\FormRequest;
9
10
class CreateRequest extends FormRequest
11
{
12
    use SavesDiscussion;
13
14
    /**
15
     * Determine if the user is authorized to make this request.
16
     *
17
     * @return bool
18
     */
19
    public function authorize()
20
    {
21
        return $this->user()->can('create', Discussion::class);
22
    }
23
24
    /**
25
     * Get the validation rules that apply to the request.
26
     *
27
     * @return array
28
     */
29
    public function rules()
30
    {
31
        return array_merge([
32
            'title' => 'required|string',
33
        ], $this->getSaveRules());
34
    }
35
36
    public function failedAuthorization()
37
    {
38
        if ($user = auth('web')->user()) {
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
39
            if ($user->active) {
40
                $message = 'You cant post a discussion because either your ministry is out of tokens for this month';
41
            } else {
42
                $message = 'You are not allowed to posting anything on this app';
43
            }
44
        } else {
45
            $message = 'You are out of credit, consider upgrading your subscription to post more discussions';
46
        }
47
48
        throw new AuthorizationException($message);
49
    }
50
}
51