Completed
Push — master ( 4434b2...6b8e46 )
by Yaro
02:04
created

AbstractField::prepare()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 1
dl 0
loc 21
ccs 0
cts 13
cp 0
crap 30
rs 9.2728
c 0
b 0
f 0
1
<?php
2
3
namespace Yaro\Jarboe\Table\Fields;
4
5
use Illuminate\Http\Request;
6
use Yaro\Jarboe\Table\CRUD;
7
use Yaro\Jarboe\Table\Fields\Interfaces\FieldPropsInterface;
8
use Yaro\Jarboe\Table\Filters\AbstractFilter;
9
10
abstract class AbstractField implements FieldPropsInterface
11
{
12
    const DEFAULT_TAB_IDENT = '[extra]';
13
14
    protected $model = '';
15
    protected $title = '';
16
    protected $name  = '';
17
    protected $readonly = false;
18
    protected $default = null;
19
    protected $hidden = [
20
        'list'   => false,
21
        'edit'   => false,
22
        'create' => false,
23
    ];
24
    protected $width;
25
    protected $filter = null;
26
    protected $tab = self::DEFAULT_TAB_IDENT;
27
    protected $col = 12;
28
29 481
    public static function make($name = '', $title = '')
30
    {
31 481
        $field = new static;
32
33 481
        if (!$title) {
34 439
            $title = preg_replace('~_~', ' ', ucfirst($name));
35
        }
36 481
        $field->title($title);
37 481
        $field->name($name);
38
39 481
        return $field;
40
    }
41
42 14
    public function setModel($model)
43
    {
44 14
        $this->model = $model;
45 14
    }
46
47 14
    public function getModel()
48
    {
49 14
        return $this->model;
50
    }
51
52 8
    public function value(Request $request)
53
    {
54 8
        $value = $request->get($this->name());
55 8
        if (!$value && $this->isNullable()) {
56 1
            return null;
57
        }
58
59 8
        return $value;
60
    }
61
62 12
    public function shouldSkip(Request $request)
63
    {
64 12
        return false;
65
    }
66
67 14
    public function col(int $col)
68
    {
69 14
        $this->col = $col;
70
71 14
        return $this;
72
    }
73
74 28
    public function getCol()
75
    {
76 28
        return $this->col;
77
    }
78
79 28
    public function filter(AbstractFilter $filter = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
80
    {
81 28
        if (is_null($filter)) {
82 28
            return $this->filter;
83
        }
84
85 14
        $filter->field($this);
86
87 14
        $this->filter = $filter;
88
89 14
        return $this;
90
    }
91
92 14
    public function tab(string $tab)
93
    {
94 14
        $this->tab = $tab;
95
96 14
        return $this;
97
    }
98
99 28
    public function getTab()
100
    {
101 28
        return $this->tab;
102
    }
103
104 481
    public function title(string $title = null)
105
    {
106 481
        if (is_null($title)) {
107 56
            return $this->title;
108
        }
109
110 481
        $this->title = $title;
111
112 481
        return $this;
113
    }
114
115 481
    public function name(string $name = null)
116
    {
117 481
        if (is_null($name)) {
118 52
            return $this->name;
119
        }
120
121 481
        $this->name = $name;
122
123 481
        return $this;
124
    }
125
126 14
    public function readonly(bool $isReadonly = true)
127
    {
128 14
        $this->readonly = $isReadonly;
129
130 14
        return $this;
131
    }
132
133 40
    public function isReadonly()
134
    {
135 40
        return $this->readonly;
136
    }
137
138 11
    public function default($value)
139
    {
140 11
        $this->default = $value;
141
142 11
        return $this;
143
    }
144
145 34
    public function getDefault()
146
    {
147 34
        return $this->default;
148
    }
149
150
    public function old($name = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
151
    {
152
        if (is_null($name)) {
153
            $name = $this->name();
154
        }
155
        
156
        return old($name);
157
    }
158
159
    public function hasOld($name = null)
160
    {
161
        return !is_null($this->old($name));
162
    }
163
164
    public function oldOrDefault($locale = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
165
    {
166
        $name = $this->name();
167
        if ($locale) {
168
            $name .= '.'. $locale;
169
        }
170
171
        if ($this->hasOld($name)) {
172
            return $this->old($name);
173
        }
174
175
        return $this->getDefault();
176
    }
177
178
    public function oldOrAttribute($model, $default = null, $locale = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
179
    {
180
        $name = $this->name();
181
        if ($locale) {
182
            $name .= '.'. $locale;
183
        }
184
185
        if ($this->hasOld($name)) {
186
            return $this->old($name);
187
        }
188
189
        if ($this->isTranslatable()) {
190
            return $model->getTranslation($this->name(), $locale) ?: $default;
191
        }
192
        return $model->{$this->name()} ?: $default;
193
    }
194
195
    public function getAttribute($model, $locale = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
196
    {
197
        if ($locale && $this->isTranslatable()) {
198
            return $model->getTranslation($this->name(), $locale);
199
        }
200
201
        return $model->{$this->name()};
202
    }
203
204 14
    public function width(int $width)
205
    {
206 14
        $this->width = $width;
207
208 14
        return $this;
209
    }
210
211 28
    public function getWidth()
212
    {
213 28
        return $this->width;
214
    }
215
216
    public function hide(bool $edit = false, bool $create = false, bool $list = false)
217
    {
218
        $this->hidden = [
219
            'list'   => $list,
220
            'edit'   => $edit,
221
            'create' => $create,
222
        ];
223
224
        return $this;
225
    }
226
227 13
    public function hideList(bool $hide = false)
228
    {
229 13
        $this->hidden['list'] = $hide;
230
231 13
        return $this;
232
    }
233
234 13
    public function hideEdit(bool $hide = false)
235
    {
236 13
        $this->hidden['edit'] = $hide;
237
238 13
        return $this;
239
    }
240
241 13
    public function hideCreate(bool $hide = false)
242
    {
243 13
        $this->hidden['create'] = $hide;
244
245 13
        return $this;
246
    }
247
248 28
    public function hidden(string $type)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
249
    {
250 28
        if (!array_key_exists($type, $this->hidden)) {
251 14
            throw new \RuntimeException(sprintf("Wrong type [%s] for field's hidden attribute", $type));
252
        }
253
254 14
        return $this->hidden[$type];
255
    }
256
257
    public function afterStore($model, Request $request)
258
    {
259
        // dummy
260
    }
261
262
    public function afterUpdate($model, Request $request)
263
    {
264
        // dummy
265
    }
266
267 10
    public function isInline()
268
    {
269 10
        return false;
270
    }
271
272
    public function isRelationField()
273
    {
274
        return false;
275
    }
276
277
    public function isMultiple()
278
    {
279
        return false;
280
    }
281
282
    public function isEncode()
283
    {
284
        return false;
285
    }
286
287
    public function isMarkupRow()
288
    {
289
        return false;
290
    }
291
292
    public function isOrderable()
293
    {
294
        return false;
295
    }
296
297
    public function isAjax()
298
    {
299
        return false;
300
    }
301
302 3
    public function isNullable()
303
    {
304 3
        return false;
305
    }
306
307
    public function hasTooltip()
308
    {
309
        return false;
310
    }
311
312
    public function hasClipboardButton()
313
    {
314
        return false;
315
    }
316
317
    public function isTranslatable()
318
    {
319
        return false;
320
    }
321
322
    public function isMaskable()
323
    {
324
        return false;
325
    }
326
327
    public function getPlaceholder()
328
    {
329
        return '';
330
    }
331
332
    abstract public function getListValue($model);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
333
334
    abstract public function getEditFormValue($model);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
335
336
    abstract public function getCreateFormValue();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
337
338
    // TODO: interface type-hinting
339
    public function prepare(CRUD $crud)
340
    {
341
        if ($this->filter()) {
342
            $this->filter()->value(
343
                $this->filter()->getValue($crud->tableIdentifier())
344
            );
345
        }
346
347
        $this->setModel($crud->getModel());
348
349
        if (method_exists($this, 'setRelationSearchUrl')) {
350
            $this->setRelationSearchUrl($crud->relationSearchUrl());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method setRelationSearchUrl() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Radio, Yaro\Jarboe\Table\Fields\Select. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
351
        }
352
        if (method_exists($this, 'setInlineUrl')) {
353
            $this->setInlineUrl($crud->inlineUrl());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method setInlineUrl() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Date, Yaro\Jarboe\Table\Fields\Number, Yaro\Jarboe\Table\Fields\Text, Yaro\Jarboe\Table\Fields\Textarea. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
354
        }
355
        if ($this->isTranslatable()) {
356
            $this->locales($crud->getLocales());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method locales() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Text, Yaro\Jarboe\Table\Fields\Textarea, Yaro\Jarboe\Table\Fields\Wysiwyg. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
357
            $this->setCurrentLocale($crud->getCurrentLocale());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Yaro\Jarboe\Table\Fields\AbstractField as the method setCurrentLocale() does only exist in the following sub-classes of Yaro\Jarboe\Table\Fields\AbstractField: Yaro\Jarboe\Table\Fields\Text, Yaro\Jarboe\Table\Fields\Textarea, Yaro\Jarboe\Table\Fields\Wysiwyg. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
358
        }
359
    }
360
}
361