TestimonyPolicy   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 68
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A viewAny() 0 4 1
A view() 0 4 1
A create() 0 4 1
A update() 0 11 4
A delete() 0 4 2
1
<?php
2
3
namespace Faithgen\Testimonies\Policies;
4
5
use FaithGen\SDK\Models\Ministry;
6
use Faithgen\Testimonies\Models\Testimony;
7
use Illuminate\Auth\Access\HandlesAuthorization;
8
9
class TestimonyPolicy
10
{
11
    use HandlesAuthorization;
12
13
    private $adminRouteNames = [
14
        'testimonies.toggle-approval',
15
        'testimonies.delete-image',
16
    ];
17
18
    public function viewAny(Ministry $ministry)
0 ignored issues
show
Unused Code introduced by
The parameter $ministry is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20
        //
21
    }
22
23
    /**
24
     * Determine whether the ministry can view the testimony.
25
     *
26
     * @param  \App\Models\Ministry  $ministry
27
     * @param  \App\Testimony  $testimony
28
     * @return mixed
29
     */
30
    public function view(Ministry $ministry, Testimony $testimony)
31
    {
32
        return $ministry->id === $testimony->ministry_id;
33
    }
34
35
    /**
36
     * Determine whether the ministry can create testimonies.
37
     *
38
     * @param  \App\Models\Ministry  $ministry
39
     * @return mixed
40
     */
41
    public function create(Ministry $ministry)
0 ignored issues
show
Unused Code introduced by
The parameter $ministry is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        //
44
    }
45
46
    /**
47
     * Determine whether the ministry can update the testimony.
48
     *
49
     * @param  \App\Models\Ministry  $ministry
50
     * @param  \App\Testimony  $testimony
51
     * @return mixed
52
     */
53
    public function update(Ministry $ministry, Testimony $testimony)
54
    {
55
        if (in_array(request()->route()->getName(), $this->adminRouteNames)) {
56
            return $ministry->id === $testimony->ministry_id;
57
        }
58
        $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...
59
60
        return $user->id === $testimony->user_id
61
            && $user->active
62
            && $ministry->id === $testimony->ministry_id;
63
    }
64
65
    /**
66
     * Determine whether the ministry can delete the testimony.
67
     *
68
     * @param  \App\Models\Ministry  $ministry
69
     * @param  \App\Testimony  $testimony
70
     * @return mixed
71
     */
72
    public function delete(Ministry $ministry, Testimony $testimony)
73
    {
74
        return $ministry->id === $testimony->ministry_id || auth('web')->user()->id === $testimony->user_id;
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...
75
    }
76
}
77