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

src/Form/Layout/Layout.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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)
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
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