Completed
Push — develop ( 3e608e...ecb37c )
by Abdelrahman
02:06
created

Attribute   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 4
dl 0
loc 94
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteKey() 0 4 1
A resolveRouteBinding() 0 6 1
B render() 0 31 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Attributes\Models;
6
7
use Vinkla\Hashids\Facades\Hashids;
8
use Rinvex\Tenants\Traits\Tenantable;
9
use Cortex\Foundation\Traits\Auditable;
10
use Illuminate\Database\Eloquent\Model;
11
use Spatie\Activitylog\Traits\LogsActivity;
12
use Rinvex\Attributes\Models\Attribute as BaseAttribute;
13
14
/**
15
 * Cortex\Attributes\Models\Attribute.
16
 *
17
 * @property int                                                                               $id
18
 * @property string                                                                            $name
19
 * @property array                                                                             $name
20
 * @property array                                                                             $description
21
 * @property int                                                                               $sort_order
22
 * @property string                                                                            $group
23
 * @property string                                                                            $type
24
 * @property bool                                                                              $is_required
25
 * @property bool                                                                              $is_collection
26
 * @property string                                                                            $default
27
 * @property \Carbon\Carbon|null                                                               $created_at
28
 * @property \Carbon\Carbon|null                                                               $updated_at
29
 * @property-read \Illuminate\Database\Eloquent\Collection|\Cortex\Foundation\Models\Log[]     $activity
30
 * @property array                                                                             $entities
31
 * @property-read \Rinvex\Attributes\Support\ValueCollection|\Rinvex\Attributes\Models\Value[] $values
32
 *
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute ordered($direction = 'asc')
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereCreatedAt($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDefault($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDescription($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereGroup($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereId($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsCollection($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsRequired($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereName($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereName($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereSortOrder($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereType($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereUpdatedAt($value)
46
 * @mixin \Eloquent
47
 */
48
class Attribute extends BaseAttribute
49
{
50
    use Auditable;
51
    use Tenantable;
52
    use LogsActivity;
53
54
    /**
55
     * Indicates whether to log only dirty attributes or all.
56
     *
57
     * @var bool
58
     */
59
    protected static $logOnlyDirty = true;
60
61
    /**
62
     * The attributes that are logged on change.
63
     *
64
     * @var array
65
     */
66
    protected static $logFillable = true;
67
68
    /**
69
     * The attributes that are ignored on change.
70
     *
71
     * @var array
72
     */
73
    protected static $ignoreChangedAttributes = [
74
        'created_at',
75
        'updated_at',
76
        'deleted_at',
77
    ];
78
79
    /**
80
     * Get the value of the model's route key.
81
     *
82
     * @return mixed
83
     */
84
    public function getRouteKey()
85
    {
86
        return Hashids::encode($this->getAttribute($this->getRouteKeyName()));
87
    }
88
89
    /**
90
     * Retrieve the model for a bound value.
91
     *
92
     * @param  mixed  $value
93
     * @return \Illuminate\Database\Eloquent\Model|null
94
     */
95
    public function resolveRouteBinding($value)
96
    {
97
        $value = Hashids::decode($value)[0];
98
99
        return $this->where($this->getRouteKeyName(), $value)->first();
100
    }
101
102
    /**
103
     * Get the route key for the model.
104
     *
105
     * @param \Illuminate\Database\Eloquent\Model $entity
106
     * @param string                              $accessArea
107
     *
108
     * @return \Illuminate\View\View
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
109
     */
110
    public function render(Model $entity, string $accessArea): string
111
    {
112
        $default = '';
113
        $selected = '';
114
115
        switch ($this->type) {
116
            case 'select':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
117
118
                $default = collect(array_map('trans', array_map('trim', explode("\n", $this->default))))->map(function ($item) use (&$selected) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 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...
119
                    if (mb_strpos($item, '=')) {
120
                        $key = mb_strstr($item, '=', true);
121
                        $value = str_replace_first('=', '', mb_strstr($item, '='));
122
123
                        // Check for SELECTED itmes (marked by asterisk)
124
                        ! str_contains($value, '*') || $selected = $key;
125
                        ! str_contains($value, '*') || $value = str_replace_first('*', '', $value);
126
                    } else {
127
                        $key = $value = $item;
128
129
                        // Check for SELECTED itmes (marked by asterisk)
130
                        ! str_contains($value, '*') || $key = $value = $selected = str_replace_first('*', '', $value);
131
                    }
132
133
                    return [$key => $value];
134
                })->collapse();
135
136
                break;
137
        }
138
139
        return view("cortex/attributes::$accessArea.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default, 'selected' => $selected])->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 176 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...
140
    }
141
}
142