Completed
Push — master ( 976295...cf57a5 )
by Kristijan
03:22
created

ParentType::__clone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
crap 2.0625
1
<?php
2
3
namespace Kris\LaravelFormBuilder\Fields;
4
5
use Kris\LaravelFormBuilder\Form;
6
7
abstract class ParentType extends FormField
8
{
9
10
    /**
11
     * @var FormField[]
12
     */
13
    protected $children;
14
15
    /**
16
     * Populate children array.
17
     *
18
     * @return mixed
19
     */
20
    abstract protected function createChildren();
21
22
    /**
23
     * @param       $name
24
     * @param       $type
25
     * @param Form  $parent
26
     * @param array $options
27
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
28
     */
29 40
    public function __construct($name, $type, Form $parent, array $options = [])
30
    {
31 40
        parent::__construct($name, $type, $parent, $options);
32
        // If there is default value provided and  setValue was not triggered
33
        // in the parent call, make sure we generate child elements.
34 35
        if ($this->hasDefault) {
35 12
            $this->createChildren();
36
        }
37 34
        $this->checkIfFileType();
38 34
    }
39
40
    /**
41
     * @param  mixed $val
42
     *
43
     * @return ChildFormType
44
     */
45 32
    public function setValue($val)
46
    {
47 32
        parent::setValue($val);
48 32
        $this->createChildren();
49
50 27
        return $this;
51
    }
52
53
    /**
54
     * {inheritdoc}
55
     */
56 13
    public function render(array $options = [], $showLabel = true, $showField = true, $showError = true)
57
    {
58 13
        $options['children'] = $this->children;
59 13
        return parent::render($options, $showLabel, $showField, $showError);
60
    }
61
62
    /**
63
     * Get all children of the choice field.
64
     *
65
     * @return mixed
66
     */
67 14
    public function getChildren()
68
    {
69 14
        return $this->children;
70
    }
71
72
    /**
73
     * Get a child of the choice field.
74
     *
75
     * @return mixed
76
     */
77 6
    public function getChild($key)
78
    {
79 6
        return array_get($this->children, $key);
80
    }
81
82
    /**
83
     * Remove child.
84
     *
85
     * @return $this
86
     */
87 1
    public function removeChild($key)
88
    {
89 1
        if ($this->getChild($key)) {
90 1
            unset($this->children[$key]);
91
        }
92
93 1
        return $this;
94
    }
95
96
    /**
97
     * @inheritdoc
98
         */
99 40
    public function setOption($name, $value)
100
    {
101 40
        parent::setOption($name, $value);
102
103 40
        foreach ((array) $this->children as $key => $child) {
104 13
            $this->children[$key]->setOption($name, $value);
105
        }
106 40
    }
107
108
    /**
109
     * @inheritdoc
110
         */
111 5
    public function setOptions($options)
112
    {
113 5
        parent::setOptions($options);
114
115 5
        foreach ((array) $this->children as $key => $child) {
116
            $this->children[$key]->setOptions($options);
117
        }
118 5
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123 1
    public function isRendered()
124
    {
125 1
        foreach ((array) $this->children as $key => $child) {
126 1
            if ($child->isRendered()) {
127 1
                return true;
128
            }
129
        }
130
131 1
        return parent::isRendered();
132
    }
133
134
    /**
135
     * Get child dynamically.
136
     *
137
     * @param string $name
138
     * @return FormField
139
     */
140 5
    public function __get($name)
141
    {
142 5
        return $this->getChild($name);
143
    }
144
145
    /**
146
     * Check if field has type property and if it's file add enctype/multipart to form.
147
     *
148
     * @return void
149
     */
150 34
    protected function checkIfFileType()
151
    {
152 34
        if ($this->getOption('type') === 'file') {
153 1
            $this->parent->setFormOption('files', true);
154
        }
155 34
    }
156
157 5
    public function __clone()
158
    {
159 5
        foreach ((array) $this->children as $key => $child) {
160
            $this->children[$key] = clone $child;
161
        }
162 5
    }
163
164
    /**
165
     * @inheritdoc
166
     */
167 1
    public function disable()
168
    {
169 1
        foreach ($this->children as $field) {
170 1
            $field->disable();
171
        }
172 1
    }
173
174
    /**
175
     * @inheritdoc
176
     */
177 1
    public function enable()
178
    {
179 1
        foreach ($this->children as $field) {
180 1
            $field->enable();
181
        }
182 1
    }
183
184
    /**
185
     * @inheritdoc
186
     */
187 2
    public function getValidationRules()
188
    {
189 2
        $rules = parent::getValidationRules();
190 2
        $childrenRules = $this->formHelper->mergeFieldsRules($this->children);
191
192 2
        return $rules->append($childrenRules);
0 ignored issues
show
Documentation introduced by
$childrenRules is of type object<Kris\LaravelFormBuilder\Rules>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
    }
194
}
195