Completed
Push — master ( 0975d4...e23347 )
by Song
02:20
created

Layout::removeReservedFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) 2019. Mallto.Co.Ltd.<mall-to.com> All rights reserved.
4
 */
5
6
namespace Encore\Admin\Form\Layout;
7
8
use Encore\Admin\Form;
9
use Illuminate\Support\Collection;
10
11
class Layout
12
{
13
    /**
14
     * @var Collection
15
     */
16
    protected $columns;
17
18
    /**
19
     * @var Column
20
     */
21
    protected $current;
22
23
    /**
24
     * @var Form
25
     */
26
    protected $parent;
27
28
    /**
29
     * Layout constructor.
30
     *
31
     * @param Form $form
32
     */
33
    public function __construct(Form $form)
34
    {
35
        $this->parent = $form;
36
37
        $this->current = new Column();
38
39
        $this->columns = new Collection();
40
    }
41
42
    /**
43
     * Add a filter to layout column.
44
     *
45
     * @param Form\Field $field
46
     */
47
    public function addField(Form\Field $field)
48
    {
49
        $this->current->add($field);
50
    }
51
52
    /**
53
     * Add a new column in layout.
54
     *
55
     * @param int      $width
56
     * @param \Closure $closure
57
     */
58 View Code Duplication
    public function column($width, \Closure $closure)
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...
59
    {
60
        if ($this->columns->isEmpty()) {
61
            $column = $this->current;
62
63
            $column->setWidth($width);
64
        } else {
65
            $column = new Column($width);
66
67
            $this->current = $column;
68
        }
69
70
        $this->columns->push($column);
71
72
        $closure($this->parent);
73
    }
74
75
    /**
76
     * Get all columns in filter layout.
77
     *
78
     * @return Collection
79
     */
80
    public function columns()
81
    {
82
        if ($this->columns->isEmpty()) {
83
            $this->columns->push($this->current);
84
        }
85
86
        return $this->columns;
87
    }
88
89
    /**
90
     * Remove reserved fields from form layout.
91
     *
92
     * @param array $fields
93
     */
94
    public function removeReservedFields(array $fields)
95
    {
96
        if (empty($fields)) {
97
            return;
98
        }
99
100
        foreach ($this->columns() as &$column) {
0 ignored issues
show
Bug introduced by
The expression $this->columns() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
101
            $column->removeFields($fields);
102
        }
103
    }
104
}
105