Conditions | 11 |
Paths | 48 |
Total Lines | 89 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
81 | protected function configureFormFields(FormMapper $formMapper) |
||
82 | { |
||
83 | $subject = $this->getSubject(); |
||
84 | $isNew = ($this->id($subject) === null) ? true : false; |
||
85 | $request = $this->getRequest(); |
||
86 | |||
87 | $idItem = null; |
||
88 | $idMenu = null; |
||
89 | if($request->query->has('uniqid')) { |
||
90 | $formId = $request->query->get('uniqid'); |
||
91 | if ($request->request->has($formId)) { |
||
92 | $formData = $request->request->get($formId); |
||
93 | $idMenu = $formData['create-menu']; |
||
94 | $idItem = $formData['create-item']; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | if ($idMenu === null && $idItem === null) { |
||
99 | if ($isNew === true) { |
||
100 | $idMenu = $this->getRequest()->query->get('create-menu'); |
||
101 | $idItem = $this->getRequest()->query->get('create-item'); |
||
102 | } else { |
||
103 | $idMenu = $subject->getMenu()->getId(); |
||
104 | $idItem = ($subject->getParent() !== null) ? $subject->getParent()->getId() : null; |
||
105 | } |
||
106 | } |
||
107 | |||
108 | $formMapper |
||
109 | ->add('menu', null, [ |
||
110 | 'label' => 'Menu', |
||
111 | 'required' => true, |
||
112 | 'property' => 'name', |
||
113 | 'query_builder' => function (EntityRepository $entityRepository) use ($idMenu) { |
||
114 | $query = $entityRepository->createQuerybuilder('m'); |
||
115 | if ($idMenu === null) { |
||
116 | return $query; |
||
117 | } |
||
118 | |||
119 | return $query |
||
120 | ->where('m.id = :id') |
||
121 | ->setParameter('id', $idMenu); |
||
122 | }, |
||
123 | ]); |
||
124 | |||
125 | if ($idItem != null) { |
||
126 | $pool = $this->getConfigurationPool(); |
||
127 | $doctrine = $pool->getContainer()->get('doctrine.orm.default_entity_manager'); |
||
128 | $parentItem = $doctrine->getRepository('AlpixelMenuBundle:Item')->find($idItem); |
||
129 | |||
130 | $formMapper->add('parent', null, [ |
||
131 | 'label' => 'Item parent', |
||
132 | 'required' => true, |
||
133 | 'property' => 'name', |
||
134 | 'data' => $parentItem, |
||
135 | 'query_builder' => function (EntityRepository $repository) use ($parentItem) { |
||
136 | $qb = $repository->createQueryBuilder('i'); |
||
137 | |||
138 | if ($parentItem->getParent() === null) { |
||
139 | return $qb->where('i.parent IS NULL') |
||
140 | ->andWhere('i.menu = :id') |
||
141 | ->setParameter('id', $parentItem->getMenu()); |
||
142 | } |
||
143 | |||
144 | return $qb->where('i.id IN (:ids)') |
||
145 | ->setParameter('ids', $parentItem->getParent()->getChildren()); |
||
146 | }, |
||
147 | ]); |
||
148 | } |
||
149 | |||
150 | $formMapper |
||
151 | ->add('name', null, [ |
||
152 | 'label' => 'Nom du menu à afficher', |
||
153 | 'required' => true, |
||
154 | ]) |
||
155 | ->add('uri', 'text', [ |
||
156 | 'label' => 'URI', |
||
157 | 'required' => true, |
||
158 | ]) |
||
159 | ->add('create-menu', 'hidden', [ |
||
160 | 'required' => false, |
||
161 | 'data' => $idMenu, |
||
162 | 'mapped' => false, |
||
163 | ]) |
||
164 | ->add('create-item', 'hidden', [ |
||
165 | 'required' => false, |
||
166 | 'data' => $idItem, |
||
167 | 'mapped' => false, |
||
168 | ]); |
||
169 | } |
||
170 | |||
251 |