Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

AdminBundle/Helper/FormWidgets/FormWidget.php (1 issue)

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
namespace Kunstmaan\AdminBundle\Helper\FormWidgets;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Helper\FormHelper;
7
use Symfony\Component\Form\AbstractType;
8
use Symfony\Component\Form\FormBuilderInterface;
9
use Symfony\Component\Form\FormView;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * The default tab implementation
14
 */
15
class FormWidget implements FormWidgetInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $identifier;
21
22
    /**
23
     * @var AbstractType[]
24
     */
25
    protected $types;
26
27
    /**
28
     * @var array
29
     */
30
    protected $data;
31
32
    /**
33
     * @var array
34
     */
35
    protected $options;
36
37
    /**
38
     * @var FormHelper
39
     */
40
    private $formHelper = null;
41
42
    /**
43
     * @var string
44
     */
45
    protected $template;
46
47
    /**
48
     * @param array $types The types
49
     * @param array $data  The data attached to the types
50
     */
51 3
    public function __construct(array $types = [], array $data = [], array $options = [])
52
    {
53 3
        $this->types = $types;
54 3
        $this->data = $data;
55 3
        $this->options = $options;
56
57 3
        $this->setTemplate('@KunstmaanAdmin/FormWidgets/FormWidget/widget.html.twig');
58 3
    }
59
60
    /**
61
     * @param FormBuilderInterface $builder The form builder
62
     */
63 1
    public function buildForm(FormBuilderInterface $builder)
64
    {
65 1
        $data = $builder->getData();
66
67 1
        foreach ($this->types as $name => $type) {
68 1
            $builder->add($name, $type, $this->options[$name]);
0 ignored issues
show
$type is of type object<Symfony\Component\Form\AbstractType>, but the function expects a string|null.

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...
69 1
            $data[$name] = $this->data[$name];
70
        }
71
72 1
        $builder->setData($data);
73 1
    }
74
75 1
    public function bindRequest(Request $request)
76
    {
77 1
    }
78
79 1
    public function persist(EntityManager $em)
80
    {
81 1
        foreach ($this->data as $item) {
82 1
            $em->persist($item);
83
        }
84 1
    }
85
86
    /**
87
     * @return array
88
     */
89 2
    public function getFormErrors(FormView $formView)
90
    {
91 2
        $formViews = [];
92 2
        foreach ($this->types as $name => $type) {
93 1
            $formViews[] = $formView[$name];
94
        }
95
96 2
        $formHelper = $this->getFormHelper();
97
98 2
        return $formHelper->getRecursiveErrorMessages($formViews);
99
    }
100
101
    /**
102
     * @return FormHelper
103
     */
104 2
    protected function getFormHelper()
105
    {
106 2
        if (\is_null($this->formHelper)) {
107 2
            $this->formHelper = new FormHelper();
108
        }
109
110 2
        return $this->formHelper;
111
    }
112
113
    /**
114
     * @param string $template
115
     */
116 3
    public function setTemplate($template)
117
    {
118 3
        $this->template = $template;
119 3
    }
120
121
    /**
122
     * @return string
123
     */
124 1
    public function getTemplate()
125
    {
126 1
        return $this->template;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 1
    public function setIdentifier($identifier)
133
    {
134 1
        $this->identifier = $identifier;
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * @return string
141
     */
142 1
    public function getIdentifier()
143
    {
144 1
        return $this->identifier;
145
    }
146
147
    /**
148
     * @param string $name
149
     * @param string $type
150
     * @param null   $data
151
     * @param array  $options
152
     *
153
     * @return FormWidget
154
     */
155 2
    public function addType($name, $type, $data = null, $options = [])
156
    {
157 2
        $this->types[$name] = $type;
158 2
        $this->data[$name] = $data;
159 2
        $this->options[$name] = $options;
160
161 2
        return $this;
162
    }
163
164
    /**
165
     * @return AbstractType[]
166
     */
167 1
    public function getTypes()
168
    {
169 1
        return $this->types;
170
    }
171
172
    /**
173
     * @return array
174
     */
175 1
    public function getData()
176
    {
177 1
        return $this->data;
178
    }
179
180
    /**
181
     * @return array
182
     */
183 1
    public function getExtraParams(Request $request)
184
    {
185 1
        return [];
186
    }
187
188
    /**
189
     * @return array
190
     */
191 1
    public function getOptions()
192
    {
193 1
        return $this->options;
194
    }
195
}
196