Testimony::getFileName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Faithgen\Testimonies\Models;
4
5
use FaithGen\SDK\Models\User;
6
use FaithGen\SDK\Models\UuidModel;
7
use FaithGen\SDK\Traits\Relationships\Belongs\BelongsToMinistryTrait;
8
use FaithGen\SDK\Traits\Relationships\Belongs\BelongsToUserTrait;
9
use FaithGen\SDK\Traits\Relationships\Morphs\CommentableTrait;
10
use FaithGen\SDK\Traits\Relationships\Morphs\ImageableTrait;
11
use FaithGen\SDK\Traits\StorageTrait;
12
use FaithGen\SDK\Traits\TitleTrait;
13
14
final class Testimony extends UuidModel
15
{
16
    use BelongsToMinistryTrait;
17
    use BelongsToUserTrait;
18
    use TitleTrait;
19
    use CommentableTrait;
20
    use ImageableTrait;
21
    use StorageTrait;
22
23
    protected $table = 'fg_testimonies';
24
25
    public function filesDir()
26
    {
27
        return 'testimonies';
28
    }
29
30
    public function getFileName(): array
31
    {
32
        return $this->images()
33
            ->pluck('name')
34
            ->toArray();
35
    }
36
37
    public function scopeApproved($query, User $user = null)
38
    {
39
        $authedUser = 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...
40
41
        if ($user === null || $authedUser === null) {
42
            $isOwner = false;
43
        }
44
45
        if ($authedUser && $authedUser->id === $user->id) {
46
            $isOwner = true;
47
        }
48
49
        if (config('faithgen-sdk.source') || $isOwner) {
0 ignored issues
show
Bug introduced by
The variable $isOwner does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
50
            return $query;
51
        }
52
53
        return $query->whereApproved(true);
54
    }
55
}
56