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 ( 4e0f46...61af64 )
by Marceau
03:34
created

Field::getViewBaseVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Akibatech\Crud\Fields;
4
5
use Akibatech\Crud\Services\CrudFields;
6
use Illuminate\View\View;
7
8
/**
9
 * Class Field
10
 *
11
 * @package Akibatech\Crud\Fields
12
 */
13
abstract class Field
14
{
15
    /**
16
     * @var CrudFields
17
     */
18
    protected $fields;
19
20
    /**
21
     * @var string
22
     */
23
    protected $identifier;
24
25
    /**
26
     * @var string
27
     */
28
    protected $label;
29
30
    /**
31
     * @var string
32
     */
33
    protected $placeholder;
34
35
    /**
36
     * @var array
37
     */
38
    protected $rules;
39
40
    /**
41
     * Field constructor.
42
     *
43
     * @param   string       $identifier
44
     * @param   array|string $rules
45
     */
46
    public function __construct($identifier, $rules = null)
47
    {
48
        $this->identifier = $identifier;
49
50
        if (!is_null($rules))
51
        {
52
            $this->withRules($rules);
53
        }
54
    }
55
56
    /**
57
     * Constructs staticly.
58
     *
59
     * @param   string $idenfitier
60
     * @param   null|string|array $rules
61
     * @return  static
62
     */
63
    public static function handle($idenfitier, $rules = null)
64
    {
65
        return (new static($idenfitier, $rules));
66
    }
67
68
69
    /**
70
     * Add validation rules to the field.
71
     *
72
     * @param   string|array $rules
73
     * @return  mixed
74
     */
75
    public function withRules($rules)
76
    {
77
        if (is_array($rules))
78
        {
79
            foreach ($rules as $rule)
80
            {
81
                $this->addRule($rule);
82
            }
83
        }
84
        else
85
        {
86
            if (is_string($rules))
87
            {
88
                if (stripos($rules, '|') !== false)
89
                {
90
                    $rules = explode('|', $rules);
91
92
                    return $this->withRules($rules);
93
                }
94
95
                return $this->withRules([$rules]);
96
            }
97
        }
98
99
        return $this;
100
    }
101
102
    /**
103
     * Add a validation rule.
104
     *
105
     * @param   string $rule
106
     * @return  self
107
     */
108
    public function addRule($rule)
109
    {
110
        $this->rules[] = $rule;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @param   CrudFields $fields
117
     * @return  self
118
     */
119
    public function setFields(CrudFields $fields)
120
    {
121
        $this->fields = $fields;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Get the field identifier.
128
     *
129
     * @param   void
130
     * @return  string
131
     */
132
    public function getIdentifier()
133
    {
134
        return $this->identifier;
135
    }
136
137
    /**
138
     * Set a custom label for the field.
139
     *
140
     * @param   string $name
141
     * @return  self
142
     */
143
    public function withLabel($name)
144
    {
145
        $this->label = $name;
146
147
        return $this;
148
    }
149
150
    /**
151
     * Defines a placeholder for the field.
152
     *
153
     * @param   string $placeholder
154
     * @return  self
155
     */
156
    public function withPlaceholder($placeholder)
157
    {
158
        $this->placeholder = $placeholder;
159
160
        return $this;
161
    }
162
163
    /**
164
     * Appends an help message to the input.
165
     *
166
     * @param   string $help
167
     * @return  self
168
     */
169
    public function withHelp($help)
170
    {
171
        $this->help = $help;
0 ignored issues
show
Bug introduced by
The property help does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
172
173
        return $this;
174
    }
175
176
    /**
177
     * Render the field form.
178
     *
179
     * @param   void
180
     * @return  string
181
     */
182
    public function form()
183
    {
184
        return $this->getForm()->render();
185
    }
186
187
    /**
188
     * Get the form view.
189
     *
190
     * @param   void
191
     * @return  View
192
     */
193
    protected function getForm()
194
    {
195
        return view()->make($this->getViewName())->with($this->getViewBaseVariables());
0 ignored issues
show
Bug introduced by
The method make 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...
196
    }
197
198
    /**
199
     * Get the field view name.
200
     *
201
     * @param   void
202
     * @return  string
203
     */
204
    abstract public function getViewName();
205
206
    /**
207
     * Returns additionnal variables to the views.
208
     *
209
     * @param   void
210
     * @return  array
211
     */
212
    protected function getViewVariables()
213
    {
214
        return [];
215
    }
216
217
    /**
218
     * Returns all base variables for the view.
219
     *
220
     * @param   void
221
     * @return  array
222
     */
223
    protected function getViewBaseVariables()
224
    {
225
        $base_variables = [
226
            'field'       => $this,
227
            'has_error'   => $this->hasError(),
228
            'error'       => $this->getError(),
229
            'placeholder' => $this->getPlaceholder(),
230
            'help'        => $this->getHelp(),
231
            'has_old'     => $this->hasOld(),
232
            'old'         => $this->getOld(),
233
            'label'       => $this->getLabel(),
234
            'name'        => $this->identifier,
235
            'id'          => 'field-' . $this->identifier,
236
            'value'       => $this->getValue()
237
        ];
238
239
        return array_merge($base_variables, $this->getViewVariables());
240
    }
241
242
    /**
243
     * Checks if the field has an error.
244
     *
245
     * @param   void
246
     * @return  bool
247
     */
248
    public function hasError()
249
    {
250
        return $this->fields->getErrors()->has($this->identifier);
251
    }
252
253
    /**
254
     * Returns the error.
255
     *
256
     * @param   void
257
     * @return  null|string
258
     */
259
    public function getError()
260
    {
261
        if ($this->hasError())
262
        {
263
            return $this->fields->getErrors()->first($this->identifier);
264
        }
265
266
        return null;
267
    }
268
269
    /**
270
     * Returns the field's placeholder.
271
     *
272
     * @param   void
273
     * @return  string
274
     */
275
    public function getPlaceholder()
276
    {
277
        if (empty($this->placeholder))
278
        {
279
            return null;
280
        }
281
282
        return $this->placeholder;
283
    }
284
285
    /**
286
     * Returns the field's help.
287
     *
288
     * @param   void
289
     * @return  string
290
     */
291
    public function getHelp()
292
    {
293
        if (empty($this->help))
294
        {
295
            return null;
296
        }
297
298
        return $this->help;
299
    }
300
301
    /**
302
     * Checks if the field has a previous value.
303
     *
304
     * @param   void
305
     * @return  bool
306
     */
307
    public function hasOld()
308
    {
309
        return $this->fields->getOldInput()->has($this->identifier);
310
    }
311
312
    /**
313
     * Returns the old value.
314
     *
315
     * @param   void
316
     * @return  string|null
317
     */
318
    public function getOld()
319
    {
320
        if ($this->hasOld())
321
        {
322
            return $this->fields->getOldInput()->first($this->identifier);
323
        }
324
325
        return null;
326
    }
327
328
    /**
329
     * Returns the field's label.
330
     *
331
     * @param   void
332
     * @return  string
333
     */
334
    public function getLabel()
335
    {
336
        if (empty($this->label))
337
        {
338
            return title_case($this->identifier);
339
        }
340
341
        return $this->label;
342
    }
343
344
    /**
345
     * Get the field value.
346
     *
347
     * @param   void
348
     * @return  mixed
349
     */
350
    public function getValue()
351
    {
352
        return $this->fields->getEntry()->getModel()->getAttributeValue($this->identifier);
0 ignored issues
show
Bug introduced by
The method getAttributeValue does only exist in Illuminate\Database\Eloquent\Model, but not in Akibatech\Crud\Traits\Crudable.

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...
353
    }
354
355
    /**
356
     * Get the value to be displayed on a table.
357
     *
358
     * @param   void
359
     * @return  mixed
360
     */
361
    public function getTableValue()
362
    {
363
        return $this->getValue();
364
    }
365
366
    /**
367
     * Set a new value to the model.
368
     *
369
     * @param   mixed $value
370
     * @return  self
371
     */
372
    public function newValue($value)
373
    {
374
        $this->fields->getEntry()->getModel()->setAttribute($this->identifier, $value);
0 ignored issues
show
Bug introduced by
The method setAttribute does only exist in Illuminate\Database\Eloquent\Model, but not in Akibatech\Crud\Traits\Crudable.

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...
375
376
        return $this;
377
    }
378
379
    /**
380
     * @param   void
381
     * @return  string
382
     */
383
    public function getRules()
384
    {
385
        return $this->rules;
386
    }
387
}
388