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 ( 151d6f...d20332 )
by Aden
03:17
created

BaseAttribute::render()   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 1
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\Attributes;
4
5
use \Illuminate\Support\HtmlString;
6
7
class BaseAttribute
8
{
9
    /**
10
     * Attribute Type Constant.
11
     */
12
    const ATTRIBUTE_TYPE = '';
13
14
    /**
15
     * View Path for this Attribute Type
16
     *     Defaults to flare::admin.attributes which outputs
17
     *     a warning callout notifying the user that the field
18
     *     view does not yet exist.
19
     *     
20
     * @var string
21
     */
22
    protected $viewpath = 'flare::admin.attributes';
23
24
    /**
25
     * Attribute.
26
     * 
27
     * @var string
28
     */
29
    protected $attribute;
30
31
    /**
32
     * Field.
33
     * 
34
     * @var mixed
35
     */
36
    protected $field;
37
38
    /**
39
     * Eloquent Model.
40
     * 
41
     * @var \Illuminate\Database\Eloquent\Model
42
     */
43
    protected $model;
44
45
    /**
46
     * Model Manager.
47
     * 
48
     * @var \LaravelFlare\Flare\Admin\Models\ModelAdmin
49
     */
50
    protected $modelManager;
51
52
    /**
53
     * __construct.
54
     * 
55
     * @param string $attribute
56
     * @param string $field
57
     * @param string $model
58
     * @param string $modelManager
59
     */
60
    public function __construct($attribute, $field, $model = null, $modelManager = null)
61
    {
62
        $this->attribute = $attribute;
63
        $this->field = $field;
64
        $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...
65
        $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...
66
    }
67
68
    /**
69
     * Returns the View to Render as an HTMLString
70
     * 
71
     * @param  boolean $view 
72
     * 
73
     * @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...
74
     */
75
    public function render($view = false)
76
    {
77
        if (method_exists($this, $method = 'render'.ucfirst($view))) {
78
            return new HtmlString(
79
                call_user_func_array([$this, $method], [])
80
            );
81
        }
82
    }
83
84
    /**
85
     * Renders the Add (Create) Field View.
86
     * 
87
     * @return \Illuminate\View\View
88
     */
89
    public function renderAdd()
90
    {
91
        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 91 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...aseAttribute::renderAdd of type Illuminate\View\View.
Loading history...
92
    }
93
94
    /**
95
     * Renders the Edit (Update) Field View.
96
     * 
97
     * @return \Illuminate\View\View
98
     */
99
    public function renderEdit()
100
    {
101
        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 101 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...seAttribute::renderEdit of type Illuminate\View\View.
Loading history...
102
    }
103
104
    /**
105
     * Renders the Clone (Update) Field View.
106
     * 
107
     * @return \Illuminate\View\View
108
     */
109
    public function renderClone()
110
    {
111
        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...
112
            view($this->viewpath.'.clone', $this->viewData());
113
        }
114
115
        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 115 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...eAttribute::renderClone of type Illuminate\View\View.
Loading history...
116
    }
117
118
    /**
119
     * Renders the Viewable Field View.
120
     * 
121
     * @return \Illuminate\View\View
122
     */
123
    public function renderView()
124
    {
125
        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 125 which is incompatible with the return type documented by LaravelFlare\Flare\Admin...seAttribute::renderView of type Illuminate\View\View.
Loading history...
126
    }
127
128
    /**
129
     * Getter for Attribute.
130
     * 
131
     * @return string
132
     */
133
    public function getAttribute()
134
    {
135
        return $this->attribute;
136
    }
137
138
    /**
139
     * Getter for Field.
140
     * 
141
     * @return mixed
142
     */
143
    public function getField()
144
    {
145
        $this->getFieldOptions();
146
147
        return $this->field;
148
    }
149
150
    /**
151
     * Gets Field Options if they are defined.
152
     */
153
    public function getFieldOptions()
154
    {
155
        if (method_exists($this->getModelManager(), $method = camel_case('get_'.$this->getAttribute().'_options'))) {
156
            // First check for a method of options based on getAttributeNameOptions()
157
            $this->field['options'] = $this->getModelManager()->$method();
158
        } elseif (isset($this->field['options']) && is_string($this->field['options']) && method_exists($this->getModelManager(), $method = camel_case('get_'.$this->field['options'].'_options'))) {
159
            // Check if Options is a string and if so, check for a method
160
            // of options based on getDefinedOptions()
161
            $this->field['options'] = $this->getModelManager()->$method();
162
        } elseif (isset($this->field['options']) && is_string($this->field['options'])) {
163
            // Otherwise, if the options have been provided as a string
164
            // we will assume that the available options are comma
165
            // delimited and explode and return that array.
166
            $this->field['options'] = explode(',', $this->field['options']);
167
        }
168
    }
169
170
    /**
171
     * Accessor for Model.
172
     * 
173
     * @var \Illuminate\Database\Eloquent\Model
174
     */
175
    public function getModel()
176
    {
177
        return $this->model;
178
    }
179
180
    /**
181
     * Accessor for Model.
182
     * 
183
     * @var \LaravelFlare\Flare\Admin\Models\ModelAdmin
184
     */
185
    public function getModelManager()
186
    {
187
        return $this->modelManager;
188
    }
189
190
    /**
191
     * Acessor for Attribute Type converted to Title Case.
192
     * 
193
     * @return string
194
     */
195
    public function getAttributeType()
196
    {
197
        return title_case(isset($this->getField()['type']) ? $this->getField()['type'] : self::ATTRIBUTE_TYPE);
198
    }
199
200
    /**
201
     * Acessor for Attribute Title converted to Title Case with Spaces.
202
     * 
203
     * @return string
204
     */
205
    public function getAttributeTitle()
206
    {
207
        return str_replace('_', ' ', title_case($this->getAttribute()));
208
    }
209
210
    /**
211
     * Returns all of the accessible data for the Attirbute View
212
     *
213
     * @return array
214
     */
215
    protected function viewData()
216
    {
217
        return [
218
                'field' => $this->getField(),
219
                'model' => $this->getModel(),
220
                'attribute' => $this->getAttribute(),
221
                'modelManager' => $this->getModelManager(),
222
                'attributeType' => $this->getAttributeType(),
223
                'attributeTitle' => $this->getAttributeTitle(),
224
            ];
225
    }
226
}
227