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.

CrudFields   B
last analyzed

Complexity

Total Complexity 47

Size/Duplication

Total Lines 364
Duplicated Lines 9.34 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 0
Metric Value
dl 34
loc 364
rs 8.439
c 0
b 0
f 0
wmc 47
lcom 3
cbo 4

21 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 4 1
A setEntry() 0 6 1
A getEntry() 0 4 1
A add() 0 13 4
A set() 0 14 2
A count() 0 4 1
A has() 0 4 1
A get() 0 14 3
A columns() 0 10 3
A loop() 0 7 2
A keys() 0 4 1
A getFieldsScripts() 0 11 2
A getFieldsCss() 0 11 2
A validationRules() 0 21 4
A contextualValidationRules() 0 9 2
A hydrateFormData() 0 17 4
A getErrors() 0 9 2
A getOldInput() 0 9 2
A getMultipart() 0 12 3
A hydrateErrorsFromSession() 17 17 3
A hydrateFieldsFromSession() 17 17 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like CrudFields often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CrudFields, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Akibatech\Crud\Services;
4
5
use Akibatech\Crud\Exceptions\DuplicateFieldIdentifierException;
6
use Akibatech\Crud\Exceptions\NoFieldsException;
7
use Akibatech\Crud\Fields\Field;
8
use Illuminate\Support\MessageBag;
9
use Illuminate\Validation\Validator;
10
11
/**
12
 * Class CrudFields
13
 *
14
 * @package Akibatech\Crud\Services
15
 */
16
class CrudFields
17
{
18
    /**
19
     * @var Field[]
20
     */
21
    protected $fields = [];
22
23
    /**
24
     * @var CrudEntry
25
     */
26
    protected $entry;
27
28
    /**
29
     * @var MessageBag
30
     */
31
    protected $errors;
32
33
    /**
34
     * @var MessageBag
35
     */
36
    protected $old_input;
37
38
    /**
39
     * @var bool
40
     */
41
    protected $multipart = false;
42
43
    /**
44
     * Make staticly a new instance.
45
     *
46
     * @param   array $fields
47
     * @return  CrudFields
48
     */
49
    public static function make(array $fields)
50
    {
51
        return (new static)->add($fields);
52
    }
53
54
    /**
55
     * @param   CrudEntry $entry
56
     * @return  self
57
     */
58
    public function setEntry(CrudEntry $entry)
59
    {
60
        $this->entry = $entry;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param   void
67
     * @return  CrudEntry
68
     */
69
    public function getEntry()
70
    {
71
        return $this->entry;
72
    }
73
74
    /**
75
     * @param   Field[] $fields
76
     * @return  self
77
     * @throws  DuplicateFieldIdentifierException
78
     */
79
    public function add(array $fields)
80
    {
81
        if (is_array($fields) && count($fields) > 0)
82
        {
83
            /** @var Field $field */
84
            foreach ($fields as $field)
85
            {
86
                $this->set($field);
87
            }
88
        }
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param   Field $field
95
     * @return  self
96
     * @throws  DuplicateFieldIdentifierException
97
     */
98
    public function set(Field $field)
99
    {
100
        $identifier = $field->getIdentifier();
101
102
        if ($this->has($identifier))
103
        {
104
            throw new DuplicateFieldIdentifierException("$identifier was already set.");
105
        }
106
107
        $field->setFields($this);
108
        $this->fields[$identifier] = $field;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @param   void
115
     * @return  int
116
     */
117
    public function count()
118
    {
119
        return count($this->fields);
120
    }
121
122
    /**
123
     * @param   string $identifier
124
     * @return  bool
125
     */
126
    public function has($identifier)
127
    {
128
        return array_key_exists($identifier, $this->fields) === true;
129
    }
130
131
    /**
132
     * @param   null|string $identifier
133
     * @return  Field[]|Field|null
134
     */
135
    public function get($identifier = null)
136
    {
137
        if (is_null($identifier))
138
        {
139
            return $this->fields;
140
        }
141
142
        if ($this->has($identifier))
143
        {
144
            return $this->fields[$identifier];
145
        }
146
147
        return null;
148
    }
149
150
    /**
151
     * @param   void
152
     * @return  \Generator
153
     */
154
    public function columns()
155
    {
156
        foreach ($this->fields as $field)
157
        {
158
            if ($field->isDisplayedInColumns())
159
            {
160
                yield $field->getLabel();
161
            }
162
        }
163
    }
164
165
    /**
166
     * @param   void
167
     * @return  \Generator
168
     */
169
    public function loop()
170
    {
171
        foreach ($this->fields as $field)
172
        {
173
            yield $field;
174
        }
175
    }
176
177
    /**
178
     * Returns all fields keys.
179
     *
180
     * @param   void
181
     * @return  integer[]
182
     */
183
    public function keys()
184
    {
185
        return array_keys($this->fields);
186
    }
187
188
    /**
189
     * @param   void
190
     * @return  array
191
     */
192
    public function getFieldsScripts()
193
    {
194
        $scripts = [];
195
196
        foreach ($this->fields as $field)
197
        {
198
            $scripts = array_merge($scripts, $field->getScripts());
199
        }
200
201
        return $scripts;
202
    }
203
204
    /**
205
     * @param   void
206
     * @return  array
207
     */
208
    public function getFieldsCss()
209
    {
210
        $css = [];
211
212
        foreach ($this->fields as $field)
213
        {
214
            $css = array_merge($css, $field->getCss());
215
        }
216
217
        return $css;
218
    }
219
220
    /**
221
     * Gets fields validation rules.
222
     *
223
     * @param   void
224
     * @return  array
225
     * @throws  NoFieldsException
226
     */
227
    public function validationRules()
228
    {
229
        $rules = [];
230
231
        if ($this->count() === 0)
232
        {
233
            throw new NoFieldsException();
234
        }
235
236
        foreach ($this->fields as $key => $field)
237
        {
238
            $field_rules = $field->getRules();
239
240
            if (!empty($field_rules))
241
            {
242
                $rules[$key] = $field_rules;
243
            }
244
        }
245
246
        return $rules;
247
    }
248
249
    /**
250
     * @param   Validator $validator
251
     * @return  Validator
252
     */
253
    public function contextualValidationRules(Validator $validator)
254
    {
255
        foreach ($this->fields as &$field)
256
        {
257
            $validator = $field->beforeValidation($validator);
258
        }
259
260
        return $validator;
261
    }
262
263
    /**
264
     * Hydrate fields with new data.
265
     *
266
     * @param   array $data
267
     * @return  self
268
     */
269
    public function hydrateFormData(array $data)
270
    {
271
        if (count($data) === 0)
272
        {
273
            return $this;
274
        }
275
276
        foreach ($this->fields as $field)
277
        {
278
            if (array_key_exists($field->getIdentifier(), $data))
279
            {
280
                $field->newValue($data[$field->getIdentifier()]);
281
            }
282
        }
283
284
        return $this;
285
    }
286
287
    /**
288
     * @param   void
289
     * @return  MessageBag
290
     */
291
    public function getErrors()
292
    {
293
        if (is_null($this->errors))
294
        {
295
            $this->errors = new MessageBag();
296
        }
297
298
        return $this->errors;
299
    }
300
301
    /**
302
     * @param   void
303
     * @return  MessageBag
304
     */
305
    public function getOldInput()
306
    {
307
        if (is_null($this->old_input))
308
        {
309
            $this->old_input = new MessageBag();
310
        }
311
312
        return $this->old_input;
313
    }
314
315
    /**
316
     * @param   void
317
     * @return  bool
318
     */
319
    public function getMultipart()
320
    {
321
        foreach ($this->fields as $field)
322
        {
323
            if ($field::MULTIPART === true)
324
            {
325
                return true;
326
            }
327
        }
328
329
        return false;
330
    }
331
332
    /**
333
     * Hydrates the instance with previous validation errors.
334
     *
335
     * @param   MessageBag $errors
336
     * @return  self
337
     */
338 View Code Duplication
    public function hydrateErrorsFromSession(MessageBag $errors = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
    {
340
        if (!is_null($errors))
341
        {
342
            $this->errors = $errors;
343
            return $this;
344
        }
345
346
        $request = app()->make('request');
347
348
        if ($request->session()->has('errors'))
349
        {
350
            $this->errors = $request->session()->get('errors', MessageBag::class);
351
        }
352
353
        return $this;
354
    }
355
356
    /**
357
     * Hydrates the instance with previous input.
358
     *
359
     * @param   MessageBag $old_input
360
     * @return  self
361
     */
362 View Code Duplication
    public function hydrateFieldsFromSession(MessageBag $old_input = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
363
    {
364
        if (!is_null($old_input))
365
        {
366
            $this->old_input = $old_input;
367
            return $this;
368
        }
369
370
        $request = app()->make('request');
371
372
        if ($request->session()->has('_old_input'))
373
        {
374
            $this->old_input = new MessageBag($request->old());
375
        }
376
377
        return $this;
378
    }
379
}
380