Attribute::getRouteKeyName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
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 Cortex\Foundation\Traits\Auditable;
9
use Illuminate\Database\Eloquent\Model;
10
use Rinvex\Support\Traits\HashidsTrait;
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                                                                            $slug
19
 * @property string                                                                            $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 whereSlug($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereSortOrder($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereName($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 HashidsTrait;
53
    use LogsActivity;
54
55
    /**
56
     * Indicates whether to log only dirty attributes or all.
57
     *
58
     * @var bool
59
     */
60
    protected static $logOnlyDirty = true;
61
62
    /**
63
     * The attributes that are logged on change.
64
     *
65
     * @var array
66
     */
67
    protected static $logFillable = true;
68
69
    /**
70
     * The attributes that are ignored on change.
71
     *
72
     * @var array
73
     */
74
    protected static $ignoreChangedAttributes = [
75
        'created_at',
76
        'updated_at',
77
        'deleted_at',
78
    ];
79
80
    /**
81
     * Get the route key for the model.
82
     *
83
     * @param \Illuminate\Database\Eloquent\Model $entity
84
     * @param string                              $accessArea
85
     *
86
     * @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...
87
     */
88
    public function render(Model $entity, string $accessArea): string
89
    {
90
        $default = '';
91
        $selected = '';
92
93
        switch ($this->type) {
94
            case 'select':
95
96
                $default = collect(array_map('trans', array_map('trim', explode("\n", $this->default))))->map(function ($item) use (&$selected) {
97
                    if (mb_strpos($item, '=')) {
98
                        $key = mb_strstr($item, '=', true);
99
                        $value = str_replace_first('=', '', mb_strstr($item, '='));
0 ignored issues
show
Deprecated Code introduced by
The function str_replace_first() has been deprecated with message: Str::replaceFirst() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
100
101
                        // Check for SELECTED itmes (marked by asterisk)
102
                        ! str_contains($value, '*') || $selected = $key;
0 ignored issues
show
Deprecated Code introduced by
The function str_contains() has been deprecated with message: Str::contains() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
103
                        ! str_contains($value, '*') || $value = str_replace_last('*', '', $value);
0 ignored issues
show
Deprecated Code introduced by
The function str_contains() has been deprecated with message: Str::contains() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function str_replace_last() has been deprecated with message: Str::replaceLast() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
104
                    } else {
105
                        $key = $value = $item;
106
107
                        // Check for SELECTED itmes (marked by asterisk)
108
                        ! str_contains($value, '*') || $key = $value = $selected = str_replace_last('*', '', $value);
0 ignored issues
show
Deprecated Code introduced by
The function str_contains() has been deprecated with message: Str::contains() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function str_replace_last() has been deprecated with message: Str::replaceLast() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
109
                    }
110
111
                    return [$key => $value];
112
                })->collapse();
113
114
                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...
115
116
            case 'check':
117
118
                $default = collect(array_map('trans', array_map('trim', explode("\n", $this->default))))->map(function ($item) use ($entity) {
119
                    $details = [
120
                        'label' => '',
121
                        'status' => false,
122
                    ];
123
124
                    if (mb_strpos($item, '=')) {
125
                        $details['label'] = mb_strstr($item, '=', true);
126
                        $item = str_replace_first('=', '', mb_strstr($item, '='));
0 ignored issues
show
Deprecated Code introduced by
The function str_replace_first() has been deprecated with message: Str::replaceFirst() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
127
128
                        // Check for SELECTED itmes (marked by asterisk)
129
                        ! str_contains($item, '*') || $details['status'] = true;
0 ignored issues
show
Deprecated Code introduced by
The function str_contains() has been deprecated with message: Str::contains() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
130
                        ! str_contains($item, '*') || $item = str_replace_last('*', '', $item);
0 ignored issues
show
Deprecated Code introduced by
The function str_contains() has been deprecated with message: Str::contains() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function str_replace_last() has been deprecated with message: Str::replaceLast() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
131
132
                        ! $entity->exists || $details['status'] = $entity->{$this->slug}->search($item) !== false;
133
                    } else {
134
                        $details['label'] = $item;
135
136
                        // Check for SELECTED itmes (marked by asterisk)
137
                        ! str_contains($item, '*') || $details['status'] = true;
0 ignored issues
show
Deprecated Code introduced by
The function str_contains() has been deprecated with message: Str::contains() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
138
                        ! str_contains($item, '*') || $details['label'] = $item = str_replace_last('*', '', $item);
0 ignored issues
show
Deprecated Code introduced by
The function str_contains() has been deprecated with message: Str::contains() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function str_replace_last() has been deprecated with message: Str::replaceLast() should be used directly instead. Will be removed in Laravel 6.0.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
139
140
                        ! $entity->exists || $details['status'] = $entity->{$this->slug}->search($item) !== false;
141
                    }
142
143
                    return [$item => $details];
144
                })->collapse();
145
146
                return view("cortex/attributes::{$accessArea}.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default])->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...
147
148
            default:
149
                return view("cortex/attributes::{$accessArea}.types.".$this->type, ['attribute' => $this, 'entity' => $entity, 'default' => $default])->render();
150
        }
151
    }
152
153
    /**
154
     * Get the route key for the model.
155
     *
156
     * @return string
157
     */
158
    public function getRouteKeyName()
159
    {
160
        return 'slug';
161
    }
162
}
163