Completed
Push — develop ( d9217f...0008ff )
by Abdelrahman
12:25
created

Attribute::render()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 8.439
cc 6
eloc 18
nc 2
nop 2
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 Spatie\Activitylog\Traits\LogsActivity;
11
use Rinvex\Attributes\Models\Attribute as BaseAttribute;
12
13
/**
14
 * Cortex\Attributes\Models\Attribute.
15
 *
16
 * @property int                                                                               $id
17
 * @property string                                                                            $name
18
 * @property array                                                                             $name
19
 * @property array                                                                             $description
20
 * @property int                                                                               $sort_order
21
 * @property string                                                                            $group
22
 * @property string                                                                            $type
23
 * @property bool                                                                              $is_required
24
 * @property bool                                                                              $is_collection
25
 * @property string                                                                            $default
26
 * @property \Carbon\Carbon|null                                                               $created_at
27
 * @property \Carbon\Carbon|null                                                               $updated_at
28
 * @property-read \Illuminate\Database\Eloquent\Collection|\Cortex\Foundation\Models\Log[]     $activity
29
 * @property array                                                                             $entities
30
 * @property-read \Rinvex\Attributes\Support\ValueCollection|\Rinvex\Attributes\Models\Value[] $values
31
 *
32
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute ordered($direction = 'asc')
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereCreatedAt($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDefault($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereDescription($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereGroup($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereId($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsCollection($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereIsRequired($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereName($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 whereSortOrder($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereType($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Attributes\Models\Attribute whereUpdatedAt($value)
45
 * @mixin \Eloquent
46
 */
47
class Attribute extends BaseAttribute
48
{
49
    use Auditable;
50
    use Tenantable;
51
    use LogsActivity;
52
53
    /**
54
     * Indicates whether to log only dirty attributes or all.
55
     *
56
     * @var bool
57
     */
58
    protected static $logOnlyDirty = true;
59
60
    /**
61
     * The attributes that are logged on change.
62
     *
63
     * @var array
64
     */
65
    protected static $logFillable = true;
66
67
    /**
68
     * The attributes that are ignored on change.
69
     *
70
     * @var array
71
     */
72
    protected static $ignoreChangedAttributes = [
73
        'created_at',
74
        'updated_at',
75
        'deleted_at',
76
    ];
77
78
    /**
79
     * Get the route key for the model.
80
     *
81
     * @return string
82
     */
83
    public function getRouteKeyName(): string
84
    {
85
        return 'name';
86
    }
87
88
    /**
89
     * Get the route key for the model.
90
     *
91
     * @param \Illuminate\Database\Eloquent\Model $entity
92
     * @param string                              $accessArea
93
     *
94
     * @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...
95
     */
96
    public function render(Model $entity, string $accessArea): string
97
    {
98
        $default = '';
99
        $selected = '';
100
101
        switch ($this->type) {
102
            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...
103
104
                $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...
105
                    if (strpos($item, '=')) {
106
                        $key = strstr($item, '=', true);
107
                        $value = str_replace_first('=', '', strstr($item, '='));
108
109
                        // Check for SELECTED itmes (marked by asterisk)
110
                        ! str_contains($value, '*') || $selected = $key;
111
                        ! str_contains($value, '*') || $value = str_replace_first('*', '', $value);
112
                    } else {
113
                        $key = $value = $item;
114
115
                        // Check for SELECTED itmes (marked by asterisk)
116
                        ! str_contains($value, '*') || $key = $value = $selected = str_replace_first('*', '', $value);
117
                    }
118
119
                    return [$key => $value];
120
                })->collapse();
121
122
                break;
123
        }
124
125
        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...
126
    }
127
}
128