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 ( cc3baf...54684c )
by Aden
04:25
created

BaseAttribute::getValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\Attributes;
4
5
use Illuminate\Support\HtmlString;
6
use LaravelFlare\Flare\Admin\Models\ModelAdmin;
7
8
class BaseAttribute
9
{
10
    /**
11
     * Attribute Type Constant.
12
     */
13
    const ATTRIBUTE_TYPE = '';
14
15
    /**
16
     * View Path for this Attribute Type
17
     *     Defaults to flare::admin.attributes which outputs
18
     *     a warning callout notifying the user that the field
19
     *     view does not yet exist.
20
     *     
21
     * @var string
22
     */
23
    protected $viewpath = 'flare::admin.attributes';
24
25
    /**
26
     * Attribute.
27
     * 
28
     * @var string
29
     */
30
    protected $attribute;
31
32
    /**
33
     * Field.
34
     * 
35
     * @var mixed
36
     */
37
    protected $field;
38
39
    /**
40
     * Value.
41
     * 
42
     * @var \Illuminate\Database\Eloquent\Model
43
     */
44
    protected $value;
45
46
    /**
47
     * Eloquent Model.
48
     * 
49
     * @var \Illuminate\Database\Eloquent\Model
50
     */
51
    protected $model;
52
53
    /**
54
     * Model Manager.
55
     * 
56
     * @var \LaravelFlare\Flare\Admin\Models\ModelAdmin
57
     */
58
    protected $modelManager;
59
60
    /**
61
     * __construct.
62
     * 
63
     * @param string $attribute
64
     * @param string $field
65
     * @param string $modelManager
66
     */
67
    public function __construct($attribute, $field, $value, $modelManager = null)
68
    {
69
        $this->attribute = $attribute;
70
        $this->field = $field;
71
        $this->value = $value;
72
        $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...
73
74
        if ($modelManager instanceof ModelAdmin) {
75
            $this->model = $modelManager->model();
76
        }
77
    }
78
79
    /**
80
     * Returns the View to Render as an HTMLString
81
     * 
82
     * @param  boolean $view 
83
     * 
84
     * @return /Illuminate/Support/String
0 ignored issues
show
Documentation introduced by
The doc-type /Illuminate/Support/String could not be parsed: Unknown type name "/Illuminate/Support/String" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
85
     */
86
    public function render($view = false)
87
    {
88
        if (method_exists($this, $method = 'render'.ucfirst($view))) {
89
            return new HtmlString(
90
                call_user_func_array([$this, $method], [])
91
            );
92
        }
93
    }
94
95
    /**
96
     * Renders the Add (Create) Field View.
97
     * 
98
     * @return \Illuminate\View\View
99
     */
100
    public function renderAdd()
101
    {
102
        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 102 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...aseAttribute::renderAdd of type Illuminate\View\View.
Loading history...
103
    }
104
105
    /**
106
     * Renders the Edit (Update) Field View.
107
     * 
108
     * @return \Illuminate\View\View
109
     */
110
    public function renderEdit()
111
    {
112
        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 112 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...seAttribute::renderEdit of type Illuminate\View\View.
Loading history...
113
    }
114
115
    /**
116
     * Renders the Clone (Update) Field View.
117
     * 
118
     * @return \Illuminate\View\View
119
     */
120
    public function renderClone()
121
    {
122
        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...
123
            view($this->viewpath.'.clone', $this->viewData());
124
        }
125
126
        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 126 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...eAttribute::renderClone of type Illuminate\View\View.
Loading history...
127
    }
128
129
    /**
130
     * Renders the Viewable Field View.
131
     * 
132
     * @return \Illuminate\View\View
133
     */
134
    public function renderView()
135
    {
136
        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 136 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...seAttribute::renderView of type Illuminate\View\View.
Loading history...
137
    }
138
139
    /**
140
     * Getter for Attribute.
141
     * 
142
     * @return string
143
     */
144
    public function getAttribute()
145
    {
146
        return $this->attribute;
147
    }
148
149
    /**
150
     * Getter for Field.
151
     * 
152
     * @return mixed
153
     */
154
    public function getField()
155
    {
156
        $this->getFieldOptions();
157
158
        return $this->field;
159
    }
160
161
    /**
162
     * Returns the current value
163
     * 
164
     * @return mixed
165
     */
166
    public function getValue()
167
    {
168
        if ($this->modelManager) {
169
            return $this->modelManager->getAttribute($this->attribute);
170
        }
171
172
        if ($this->model) {
173
            return $this->model->getAttribute($this->attribute);
174
        }
175
176
        return $this->value;
177
    }
178
179
    /**
180
     * Returns the old or current value
181
     * 
182
     * @return mixed
183
     */
184
    public function getOldValue()
185
    {
186
        return old($this->attribute, $this->getValue());
187
    }
188
189
    /**
190
     * Gets Field Options if they are defined.
191
     */
192
    public function getFieldOptions()
193
    {
194
        if (method_exists($this->getModelManager(), $method = camel_case('get_'.$this->getAttribute().'_options'))) {
195
            // First check for a method of options based on getAttributeNameOptions()
196
            $this->field['options'] = $this->getModelManager()->$method();
197
        } elseif (isset($this->field['options']) && is_string($this->field['options']) && method_exists($this->getModelManager(), $method = camel_case('get_'.$this->field['options'].'_options'))) {
198
            // Check if Options is a string and if so, check for a method
199
            // of options based on getDefinedOptions()
200
            $this->field['options'] = $this->getModelManager()->$method();
201
        } elseif (isset($this->field['options']) && is_string($this->field['options'])) {
202
            // Otherwise, if the options have been provided as a string
203
            // we will assume that the available options are comma
204
            // delimited and explode and return that array.
205
            $this->field['options'] = explode(',', $this->field['options']);
206
        }
207
    }
208
209
    /**
210
     * Accessor for Model.
211
     * 
212
     * @var \Illuminate\Database\Eloquent\Model
213
     */
214
    public function getModel()
215
    {
216
        return $this->model;
217
    }
218
219
    /**
220
     * Accessor for Model.
221
     * 
222
     * @var \LaravelFlare\Flare\Admin\Models\ModelAdmin
223
     */
224
    public function getModelManager()
225
    {
226
        return $this->modelManager;
227
    }
228
229
    /**
230
     * Acessor for Attribute Type converted to Title Case.
231
     * 
232
     * @return string
233
     */
234
    public function getAttributeType()
235
    {
236
        return title_case(isset($this->getField()['type']) ? $this->getField()['type'] : self::ATTRIBUTE_TYPE);
237
    }
238
239
    /**
240
     * Acessor for Attribute Title converted to Title Case with Spaces.
241
     * 
242
     * @return string
243
     */
244
    public function getAttributeTitle()
245
    {
246
        return str_replace('_', ' ', title_case($this->getAttribute()));
247
    }
248
249
    /**
250
     * Returns all of the accessible data for the Attirbute View
251
     *
252
     * @return array
253
     */
254
    protected function viewData()
255
    {
256
        return [
257
                'field' => $this->getField(),
258
                'model' => $this->getModel(),
259
                'attribute' => $this->getAttribute(),
260
                'value' => $this->getValue(),
261
                'oldValue' => $this->getOldValue(),
262
                'modelManager' => $this->getModelManager(),
263
                'attributeType' => $this->getAttributeType(),
264
                'attributeTitle' => $this->getAttributeTitle(),
265
            ];
266
    }
267
}
268