GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 54c72b...e7e058 )
by Aden
03:11
created

BaseAttribute::renderClone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\Attributes;
4
5
class BaseAttribute
6
{
7
    /**
8
     * Attribute Type Constant.
9
     */
10
    const ATTRIBUTE_TYPE = '';
11
12
    /**
13
     * View Path for this Attribute Type
14
     *     Defaults to flare::admin.attributes which outputs
15
     *     a warning callout notifying the user that the field
16
     *     view does not yet exist.
17
     *     
18
     * @var string
19
     */
20
    protected $viewpath = 'flare::admin.attributes';
21
22
    /**
23
     * Attribute.
24
     * 
25
     * @var string
26
     */
27
    protected $attribute;
28
29
    /**
30
     * Field.
31
     * 
32
     * @var mixed
33
     */
34
    protected $field;
35
36
    /**
37
     * Eloquent Model.
38
     * 
39
     * @var \Illuminate\Database\Eloquent\Model
40
     */
41
    protected $model;
42
43
    /**
44
     * Model Manager.
45
     * 
46
     * @var \LaravelFlare\Flare\Admin\Models\ModelAdmin
47
     */
48
    protected $modelManager;
49
50
    /**
51
     * __construct.
52
     * 
53
     * @param string $attribute
54
     * @param string $field
55
     * @param string $model
56
     * @param string $modelManager
57
     */
58
    public function __construct($attribute, $field, $model = null, $modelManager = null)
59
    {
60
        $this->attribute = $attribute;
61
        $this->field = $field;
62
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model can also be of type string. However, the property $model is declared as type object<Illuminate\Database\Eloquent\Model>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
63
        $this->modelManager = $modelManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $modelManager can also be of type string. However, the property $modelManager is declared as type object<LaravelFlare\Flar...dmin\Models\ModelAdmin>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
64
    }
65
66
    /**
67
     * Renders the Add (Create) Field View.
68
     * 
69
     * @return \Illuminate\View\View
70
     */
71
    public function renderAdd()
72
    {
73
        return view($this->viewpath.'.add', $this->viewData());
0 ignored issues
show
Bug Compatibility introduced by
The expression view($this->viewpath . '...d', $this->viewData()); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 73 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...aseAttribute::renderAdd of type Illuminate\View\View.
Loading history...
74
    }
75
76
    /**
77
     * Renders the Edit (Update) Field View.
78
     * 
79
     * @return \Illuminate\View\View
80
     */
81
    public function renderEdit()
82
    {
83
        return view($this->viewpath.'.edit', $this->viewData());
0 ignored issues
show
Bug Compatibility introduced by
The expression view($this->viewpath . '...t', $this->viewData()); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 83 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...seAttribute::renderEdit of type Illuminate\View\View.
Loading history...
84
    }
85
86
    /**
87
     * Renders the Clone (Update) Field View.
88
     * 
89
     * @return \Illuminate\View\View
90
     */
91
    public function renderClone()
92
    {
93
        if (view()->exists($this->viewpath.'.clone')) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

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...
94
            view($this->viewpath.'.clone', $this->viewData());
95
        }
96
97
        return view($this->viewpath.'.edit', $this->viewData());
0 ignored issues
show
Bug Compatibility introduced by
The expression view($this->viewpath . '...t', $this->viewData()); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 97 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...eAttribute::renderClone of type Illuminate\View\View.
Loading history...
98
    }
99
100
    /**
101
     * Renders the Viewable Field View.
102
     * 
103
     * @return \Illuminate\View\View
104
     */
105
    public function renderView()
106
    {
107
        return view($this->viewpath.'.view', $this->viewData());
0 ignored issues
show
Bug Compatibility introduced by
The expression view($this->viewpath . '...w', $this->viewData()); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 107 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...seAttribute::renderView of type Illuminate\View\View.
Loading history...
108
    }
109
110
    /**
111
     * Getter for Attribute.
112
     * 
113
     * @return string
114
     */
115
    public function getAttribute()
116
    {
117
        return $this->attribute;
118
    }
119
120
    /**
121
     * Getter for Field.
122
     * 
123
     * @return mixed
124
     */
125
    public function getField()
126
    {
127
        $this->getFieldOptions();
128
129
        return $this->field;
130
    }
131
132
    /**
133
     * Gets Field Options if they are defined.
134
     */
135
    public function getFieldOptions()
136
    {
137
        if (method_exists($this->getModelManager(), $method = camel_case('get_'.$this->getAttribute().'_options'))) {
138
            // First check for a method of options based on getAttributeNameOptions()
139
            $this->field['options'] = $this->getModelManager()->$method();
140
        } elseif (isset($this->field['options']) && is_string($this->field['options']) && method_exists($this->getModelManager(), $method = camel_case('get_'.$this->field['options'].'_options'))) {
141
            // Check if Options is a string and if so, check for a method
142
            // of options based on getDefinedOptions()
143
            $this->field['options'] = $this->getModelManager()->$method();
144
        } elseif (isset($this->field['options']) && is_string($this->field['options'])) {
145
            // Otherwise, if the options have been provided as a string
146
            // we will assume that the available options are comma
147
            // delimited and explode and return that array.
148
            $this->field['options'] = explode(',', $this->field['options']);
149
        }
150
    }
151
152
    /**
153
     * Accessor for Model.
154
     * 
155
     * @var \Illuminate\Database\Eloquent\Model
156
     */
157
    public function getModel()
158
    {
159
        return $this->model;
160
    }
161
162
    /**
163
     * Accessor for Model.
164
     * 
165
     * @var \LaravelFlare\Flare\Admin\Models\ModelAdmin
166
     */
167
    public function getModelManager()
168
    {
169
        return $this->modelManager;
170
    }
171
172
    /**
173
     * Acessor for Attribute Type converted to Title Case.
174
     * 
175
     * @return string
176
     */
177
    public function getAttributeType()
178
    {
179
        return title_case(isset($this->getField()['type']) ? $this->getField()['type'] : self::ATTRIBUTE_TYPE);
180
    }
181
182
    /**
183
     * Acessor for Attribute Title converted to Title Case with Spaces.
184
     * 
185
     * @return string
186
     */
187
    public function getAttributeTitle()
188
    {
189
        return str_replace('_', ' ', title_case($this->getAttribute()));
190
    }
191
192
    /**
193
     * Returns all of the accessible data for the Attirbute View
194
     *
195
     * @return array
196
     */
197
    protected function viewData()
198
    {
199
        return [
200
                'field' => $this->getField(),
201
                'model' => $this->getModel(),
202
                'attribute' => $this->getAttribute(),
203
                'modelManager' => $this->getModelManager(),
204
                'attributeType' => $this->getAttributeType(),
205
                'attributeTitle' => $this->getAttributeTitle(),
206
            ];
207
    }
208
}
209