Completed
Push — master ( 9cee5d...f0143d )
by Piotr
13s
created

News::initForm()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 93
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 8.4642
c 0
b 0
f 0
cc 3
eloc 64
nc 1
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace FSi\FixturesBundle\Admin;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use FSi\Bundle\AdminBundle\Doctrine\Admin\CRUDElement;
7
use FSi\Bundle\AdminBundle\Form\TypeSolver;
8
use FSi\Component\DataGrid\DataGridFactoryInterface;
9
use FSi\Component\DataSource\DataSourceFactoryInterface;
10
use FSi\FixturesBundle\Form\TagType;
11
use Symfony\Component\Form\FormFactoryInterface;
12
13
class News extends CRUDElement
14
{
15
    public function getId()
16
    {
17
        return 'news';
18
    }
19
20
    public function getClassName()
21
    {
22
        return 'FSi\FixturesBundle\Entity\News';
23
    }
24
25
    protected function initDataGrid(DataGridFactoryInterface $factory)
26
    {
27
        /* @var $datagrid \FSi\Component\DataGrid\DataGrid */
28
        $datagrid = $factory->createDataGrid('news');
29
        $datagrid->addColumn('title', 'text', array(
30
            'label' => 'admin.news.list.title',
31
            'field_mapping' => array('title', 'subtitle'),
32
            'value_glue' => '<br/>',
33
            'editable' => true
34
        ));
35
        $datagrid->addColumn('date', 'datetime', array(
36
            'label' => 'admin.news.list.date',
37
            'datetime_format' => 'Y-m-d',
38
            'editable' => true,
39
            'form_type' => array(
40
                'date' => TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\DateType', 'date')
41
            ),
42
            'form_options' => array(
43
                'date' => array('widget' => 'single_text')
44
            )
45
        ));
46
        $datagrid->addColumn('created_at', 'datetime', array(
47
            'label' => 'admin.news.list.created_at'
48
        ));
49
        $datagrid->addColumn('visible', 'boolean', array(
50
            'label' => 'admin.news.list.visible'
51
        ));
52
        $datagrid->addColumn('creator_email', 'text', array(
53
            'label' => 'admin.news.list.creator_email'
54
        ));
55
        $datagrid->addColumn('photo', 'fsi_image', array(
56
            'label' => 'admin.news.list.photo',
57
            'width' => 100
58
        ));
59
        $datagrid->addColumn('actions', 'action', array(
60
            'label' => 'admin.news.list.actions',
61
            'field_mapping' => array('id'),
62
            'actions' => array(
63
                'edit' => array(
64
                    'route_name' => "fsi_admin_crud_edit",
65
                    'additional_parameters' => array('element' => $this->getId()),
66
                    'parameters_field_mapping' => array('id' => 'id')
67
                ),
68
                'display' => array(
69
                    'element' => DisplayNews::ID
70
                )
71
            )
72
        ));
73
74
        return $datagrid;
75
    }
76
77
    protected function initDataSource(DataSourceFactoryInterface $factory)
78
    {
79
        /* @var $datasource \FSi\Component\DataSource\DataSource */
80
        $datasource = $factory->createDataSource('doctrine', array('entity' => $this->getClassName()), 'news');
81
        $datasource->addField('title', 'text', 'like', array(
82
            'sortable' => false,
83
            'form_options' => array(
84
                'label' => 'admin.news.list.title',
85
            )
86
        ));
87
        $datasource->addField('created_at', 'date', 'between', array(
88
            'field' => 'createdAt',
89
            'sortable' => true,
90
            'form_from_options' => array(
91
                'widget' => 'single_text',
92
                'label' => 'admin.news.list.created_at_from',
93
            ),
94
            'form_to_options' => array(
95
                'widget' => 'single_text',
96
                'label' => 'admin.news.list.created_at_to',
97
            )
98
        ));
99
        $datasource->addField('visible', 'boolean', 'eq', array(
100
            'sortable' => false,
101
            'form_options' => array(
102
                'label' => 'admin.news.list.visible',
103
            )
104
        ));
105
        $datasource->addField('creator_email', 'text', 'like', array(
106
            'field' => 'creatorEmail',
107
            'sortable' => true,
108
            'form_options' => array(
109
                'label' => 'admin.news.list.creator_email',
110
            )
111
        ));
112
113
        $datasource->setMaxResults(10);
114
115
        return $datasource;
116
    }
117
118
    protected function initForm(FormFactoryInterface $factory, $data = null)
119
    {
120
        $builder = $factory->createNamedBuilder(
121
            'news',
122
            TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\FormType', 'form'),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...ype\\FormType', 'form') targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\F...e::createNamedBuilder() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
123
            $data,
124
            array(
125
                'data_class' => $this->getClassName()
126
            )
127
        );
128
129
        $builder->add(
130
            'title',
131
            TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\TextType', 'text'),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...ype\\TextType', 'text') targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\FormBuilderInterface::add() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
132
            array(
133
                'label' => 'admin.news.list.title',
134
            )
135
        );
136
137
        $builder->add(
138
            'date',
139
            TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\DateType', 'date'),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...ype\\DateType', 'date') targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\FormBuilderInterface::add() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
140
            array(
141
                'label' => 'admin.news.list.date',
142
                'widget' => 'single_text',
143
                'required' => false,
144
            )
145
        );
146
147
        $builder->add(
148
            'created_at',
149
            TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\DateType', 'date'),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...ype\\DateType', 'date') targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\FormBuilderInterface::add() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
150
            array(
151
                'label' => 'admin.news.list.created_at',
152
                'widget' => 'single_text'
153
            )
154
        );
155
156
        $builder->add(
157
            'visible',
158
            TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\CheckboxType', 'checkbox'),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...ckboxType', 'checkbox') targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\FormBuilderInterface::add() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
159
            array(
160
                'label' => 'admin.news.list.visible',
161
                'required' => false,
162
            )
163
        );
164
165
        $builder->add(
166
            'creator_email',
167
            TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\EmailType', 'email'),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...e\\EmailType', 'email') targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\FormBuilderInterface::add() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
168
            array(
169
                'label' => 'admin.news.list.creator_email'
170
            )
171
        );
172
173
        $builder->add(
174
            'photo',
175
            TypeSolver::getFormType('FSi\Bundle\DoctrineExtensionsBundle\Form\Type\FSi\ImageType', 'fsi_image'),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...mageType', 'fsi_image') targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\FormBuilderInterface::add() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
176
            array(
177
                'label' => 'admin.news.list.photo'
178
            )
179
        );
180
181
        $builder->add(
182
            'tags',
183
            TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\CollectionType', 'collection'),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...ionType', 'collection') targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\FormBuilderInterface::add() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
184
            array(
185
                TypeSolver::hasCollectionEntryTypeOption() ? 'entry_type' : 'type' =>
186
                    TypeSolver::getFormType('FSi\FixturesBundle\Form\TagType', new TagType()),
187
                'label' => 'admin.news.list.tags',
188
                'allow_add' => true,
189
                'allow_delete' => true,
190
                'by_reference' => false,
191
            )
192
        );
193
194
        $builder->add(
195
            'nonEditableTags',
196
            TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\CollectionType', 'collection'),
0 ignored issues
show
Bug introduced by
It seems like \FSi\Bundle\AdminBundle\...ionType', 'collection') targeting FSi\Bundle\AdminBundle\F...peSolver::getFormType() can also be of type object<Symfony\Component\Form\FormTypeInterface>; however, Symfony\Component\Form\FormBuilderInterface::add() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
197
            array(
198
                TypeSolver::hasCollectionEntryTypeOption() ? 'entry_type' : 'type' =>
199
                    TypeSolver::getFormType('Symfony\Component\Form\Extension\Core\Type\TextType', 'text'),
200
                'data' => new ArrayCollection(['Tag 1', 'Tag 2', 'Tag 3']),
201
                'label' => 'admin.news.list.non_editable_tags',
202
                'allow_add' => false,
203
                'allow_delete' => false,
204
                'mapped' => false,
205
                'required' => false
206
            )
207
        );
208
209
        return $builder->getForm();
210
    }
211
}
212