Module::getUsedAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Faithgen\AppBuild\Models;
4
5
use Faithgen\AppBuild\Traits\ManyMinistryModules;
6
use FaithGen\SDK\Models\UuidModel;
7
use FaithGen\SDK\Traits\ActiveTrait;
8
use FaithGen\SDK\Traits\Relationships\Morphs\CommentableTrait;
9
use FaithGen\SDK\Traits\Relationships\Morphs\ImageableTrait;
10
use FaithGen\SDK\Traits\StorageTrait;
11
use Illuminate\Support\Str;
12
13
final class Module extends UuidModel
14
{
15
    use ManyMinistryModules;
16
    use ActiveTrait;
17
    use ImageableTrait;
18
    use CommentableTrait;
19
    use StorageTrait;
20
21
    protected $guarded = ['id'];
22
    protected $table = 'fg_modules';
23
24
    protected $hidden = [
25
        'created_at',
26
        'updated_at',
27
    ];
28
29
    public function getNameAttribute($val)
30
    {
31
        return Str::ucfirst($val);
32
    }
33
34
    /**
35
     * Checks is if the authenticated user is using this module.
36
     *
37
     * @return bool
38
     */
39
    public function getUsedAttribute(): bool
40
    {
41
        if (! auth()->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...
42
            return false;
43
        }
44
45
        return (bool) $this->ministryModules()
46
                           ->where('ministry_id', auth()->user()->id)
47
                           ->active()
48
                           ->count();
49
    }
50
51
    /**
52
     * Formats the description.
53
     *
54
     * @param $val
55
     *
56
     * @return string
57
     */
58
    public function getDescriptionAttribute($val)
59
    {
60
        return Str::ucfirst($val);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function filesDir()
67
    {
68
        return 'modules';
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getFileName()
75
    {
76
        return $this->images()
77
                    ->pluck('name')
78
                    ->toArray();
79
    }
80
81
    public function getImageDimensions()
82
    {
83
        return [0];
84
    }
85
}
86