Conditions | 7 |
Paths | 12 |
Total Lines | 52 |
Code Lines | 32 |
Lines | 52 |
Ratio | 100 % |
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 |
||
99 | View Code Duplication | public function buildForm(FormBuilderInterface $builder, array $options) |
|
100 | { |
||
101 | |||
102 | //getAll categories |
||
103 | $categoryQb = $this->getEntityManager()->getRepository('VictoireBlogBundle:Category')->getAll(); |
||
104 | //getAll published articles |
||
105 | $articleQb = $this->getEntityManager()->getRepository('VictoireBlogBundle:Article')->getAll(true); |
||
106 | |||
107 | //get Listing |
||
108 | $listing = $options['widget']->getListing(); |
||
109 | |||
110 | $mode = $listing->getMode(); |
||
111 | switch ($mode) { |
||
112 | case 'query': |
||
113 | //filter with listingQuery |
||
114 | $articleQb->filterWithListingQuery($listing->getQuery()); |
||
115 | break; |
||
116 | } |
||
117 | $categoryQb->filterByArticles($articleQb->getInstance('article')); |
||
118 | $categories = $categoryQb->getInstance('c_category')->getQuery()->getResult(); |
||
119 | |||
120 | //the blank value |
||
121 | $categoriesChoices = []; |
||
122 | |||
123 | foreach ($categories as $category) { |
||
124 | $categoriesChoices[$category->getId()] = $category->getTitle(); |
||
125 | } |
||
126 | |||
127 | $data = null; |
||
128 | if ($this->getRequest()->query->has('filter') && array_key_exists('category_filter', $this->getRequest()->query->get('filter'))) { |
||
129 | if ($options['multiple']) { |
||
130 | $data = []; |
||
131 | foreach ($this->getRequest()->query->get('filter')['category_filter']['category'] as $id => $selectedCategory) { |
||
132 | $data[$id] = $selectedCategory; |
||
133 | } |
||
134 | } else { |
||
135 | $data = $this->getRequest()->query->get('filter')['category_filter']['category']; |
||
136 | } |
||
137 | } |
||
138 | $builder |
||
139 | ->add( |
||
140 | 'category', ChoiceType::class, [ |
||
141 | 'label' => false, |
||
142 | 'choices' => $categoriesChoices, |
||
143 | 'required' => false, |
||
144 | 'expanded' => true, |
||
145 | 'multiple' => $options['multiple'], |
||
146 | 'empty_value' => $this->translator->trans('blog.category_filter.empty_value.label'), |
||
147 | 'data' => $data, |
||
148 | ] |
||
149 | ); |
||
150 | } |
||
151 | |||
174 |