Conditions | 17 |
Paths | 561 |
Total Lines | 88 |
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 |
||
113 | public function buildPager(): void |
||
114 | { |
||
115 | if ($this->bound) { |
||
116 | return; |
||
117 | } |
||
118 | |||
119 | foreach ($this->getFilters() as $name => $filter) { |
||
120 | list($type, $options) = $filter->getRenderSettings(); |
||
121 | |||
122 | $this->formBuilder->add($filter->getFormName(), $type, $options); |
||
123 | } |
||
124 | |||
125 | $hiddenType = HiddenType::class; |
||
126 | |||
127 | $this->formBuilder->add('_sort_by', $hiddenType); |
||
128 | $this->formBuilder->get('_sort_by')->addViewTransformer(new CallbackTransformer( |
||
129 | static function ($value) { |
||
130 | return $value; |
||
131 | }, |
||
132 | static function ($value) { |
||
133 | return $value instanceof FieldDescriptionInterface ? $value->getName() : $value; |
||
134 | } |
||
135 | )); |
||
136 | |||
137 | $this->formBuilder->add('_sort_order', $hiddenType); |
||
138 | $this->formBuilder->add('_page', $hiddenType); |
||
139 | |||
140 | if (isset($this->values['_per_page']) && \is_array($this->values['_per_page'])) { |
||
141 | $this->formBuilder->add('_per_page', CollectionType::class, [ |
||
142 | 'entry_type' => $hiddenType, |
||
143 | 'allow_add' => true, |
||
144 | ]); |
||
145 | } else { |
||
146 | $this->formBuilder->add('_per_page', $hiddenType); |
||
147 | } |
||
148 | |||
149 | $this->form = $this->formBuilder->getForm(); |
||
150 | $this->form->submit($this->values); |
||
151 | |||
152 | $data = $this->form->getData(); |
||
153 | |||
154 | foreach ($this->getFilters() as $name => $filter) { |
||
155 | $this->values[$name] = isset($this->values[$name]) ? $this->values[$name] : null; |
||
156 | $filterFormName = $filter->getFormName(); |
||
157 | if (isset($this->values[$filterFormName]['value']) && '' !== $this->values[$filterFormName]['value']) { |
||
158 | $filter->apply($this->query, $data[$filterFormName]); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | if (isset($this->values['_sort_by'])) { |
||
163 | if (!$this->values['_sort_by'] instanceof FieldDescriptionInterface) { |
||
164 | throw new UnexpectedTypeException($this->values['_sort_by'], FieldDescriptionInterface::class); |
||
165 | } |
||
166 | |||
167 | if ($this->values['_sort_by']->isSortable()) { |
||
168 | $this->query->setSortBy($this->values['_sort_by']->getSortParentAssociationMapping(), $this->values['_sort_by']->getSortFieldMapping()); |
||
169 | |||
170 | $this->values['_sort_order'] = $this->values['_sort_order'] ?? 'ASC'; |
||
171 | $this->query->setSortOrder($this->values['_sort_order']); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | $maxPerPage = 25; |
||
176 | if (isset($this->values['_per_page'])) { |
||
177 | if (isset($this->values['_per_page']['value'])) { |
||
178 | $maxPerPage = $this->values['_per_page']['value']; |
||
179 | } else { |
||
180 | $maxPerPage = $this->values['_per_page']; |
||
181 | } |
||
182 | } |
||
183 | $this->pager->setMaxPerPage($maxPerPage); |
||
184 | |||
185 | $page = 1; |
||
186 | if (isset($this->values['_page'])) { |
||
187 | if (isset($this->values['_page']['value'])) { |
||
188 | $page = $this->values['_page']['value']; |
||
189 | } else { |
||
190 | $page = $this->values['_page']; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | $this->pager->setPage($page); |
||
195 | |||
196 | $this->pager->setQuery($this->query); |
||
197 | $this->pager->init(); |
||
198 | |||
199 | $this->bound = true; |
||
200 | } |
||
201 | |||
337 |