Completed
Push — develop ( 0bf16e...bc1225 )
by Abdelrahman
01:37
created

Attribute::getRouteKeyName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Attributes\Models;
6
7
use Rinvex\Tenants\Traits\Tenantable;
8
use Illuminate\Database\Eloquent\Model;
9
use Spatie\Activitylog\Traits\LogsActivity;
10
use Rinvex\Attributes\Models\Attribute as BaseAttribute;
11
12
/**
13
 * Cortex\Attributes\Models\Attribute.
14
 *
15
 * @property int                                                                               $id
16
 * @property string                                                                            $slug
17
 * @property array                                                                             $name
18
 * @property array                                                                             $description
19
 * @property int                                                                               $sort_order
20
 * @property string                                                                            $group
21
 * @property string                                                                            $type
22
 * @property bool                                                                              $is_required
23
 * @property bool                                                                              $is_collection
24
 * @property string                                                                            $default
25
 * @property \Carbon\Carbon|null                                                               $created_at
26
 * @property \Carbon\Carbon|null                                                               $updated_at
27
 * @property-read \Illuminate\Database\Eloquent\Collection|\Cortex\Foundation\Models\Log[]     $activity
28
 * @property array                                                                             $entities
29
 * @property-read \Rinvex\Attributes\Support\ValueCollection|\Rinvex\Attributes\Models\Value[] $values
30
 *
31
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute ordered($direction = 'asc')
32
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereCreatedAt($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDefault($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDescription($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereGroup($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereId($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsCollection($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsRequired($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereName($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereSlug($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereSortOrder($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereType($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereUpdatedAt($value)
44
 * @mixin \Eloquent
45
 */
46
class Attribute extends BaseAttribute
47
{
48
    use Tenantable;
49
    use LogsActivity;
50
51
    /**
52
     * Indicates whether to log only dirty attributes or all.
53
     *
54
     * @var bool
55
     */
56
    protected static $logOnlyDirty = true;
57
58
    /**
59
     * The attributes that are logged on change.
60
     *
61
     * @var array
62
     */
63
    protected static $logAttributes = [
64
        'name',
65
        'slug',
66
        'description',
67
        'sort_order',
68
        'group',
69
        'type',
70
        'entities',
71
        'is_required',
72
        'is_collection',
73
        'default',
74
    ];
75
76
    /**
77
     * The attributes that are ignored on change.
78
     *
79
     * @var array
80
     */
81
    protected static $ignoreChangedAttributes = [
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $ignoreChangedAttributes exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
82
        'created_at',
83
        'updated_at',
84
        'deleted_at',
85
    ];
86
87
    /**
88
     * Get the route key for the model.
89
     *
90
     * @return string
91
     */
92
    public function getRouteKeyName()
93
    {
94
        return 'slug';
95
    }
96
97
    /**
98
     * Get the route key for the model.
99
     *
100
     * @param \Illuminate\Database\Eloquent\Model $entity
101
     * @param string                              $accessArea
102
     *
103
     * @return string
104
     */
105
    public function render(Model $entity, string $accessArea)
106
    {
107
        return view("cortex/attributes::$accessArea.types.".$this->type, ['attribute' => $this, 'entity' => $entity])->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\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...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
108
    }
109
}
110