Conditions | 10 |
Paths | 18 |
Total Lines | 65 |
Code Lines | 33 |
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 |
||
29 | public function buildForm(FormBuilderInterface $builder, array $options) |
||
30 | { |
||
31 | if ($options['businessEntityId'] !== null) { |
||
32 | if ($options['namespace'] === null) { |
||
33 | throw new \Exception('The namespace is mandatory if the business_entity_id is given.'); |
||
34 | } |
||
35 | if ($options['mode'] === null) { |
||
36 | throw new \Exception('The mode is mandatory if the business_entity_id is given.'); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | if ($options['mode'] === Widget::MODE_ENTITY) { |
||
41 | $this->addEntityFields($builder, $options); |
||
42 | } |
||
43 | |||
44 | if ($options['mode'] === Widget::MODE_QUERY) { |
||
45 | $this->addQueryFields($builder, $options); |
||
46 | } |
||
47 | |||
48 | if ($options['mode'] === Widget::MODE_BUSINESS_ENTITY) { |
||
49 | $this->addBusinessEntityFields($builder, $options); |
||
50 | } |
||
51 | |||
52 | //add the mode to the form |
||
53 | $builder->add('mode', HiddenType::class, [ |
||
54 | 'data' => $options['mode'], |
||
55 | ]); |
||
56 | $builder->add('asynchronous', null, [ |
||
57 | 'label' => 'victoire.widget.type.asynchronous.label', |
||
58 | 'required' => false, |
||
59 | ]); |
||
60 | $builder->add('theme', HiddenType::class); |
||
61 | $builder->add('quantum', HiddenType::class); |
||
62 | |||
63 | //add the slot to the form |
||
64 | $builder->add('slot', HiddenType::class, []); |
||
65 | |||
66 | $this->addCriteriasFields($builder, $options); |
||
67 | //we use the PRE_SUBMIT event to set the mode option |
||
68 | $builder->addEventListener( |
||
69 | FormEvents::PRE_SUBMIT, |
||
70 | function (FormEvent $event) use ($options) { |
||
71 | //we get the raw data for the widget form |
||
72 | $rawData = $event->getData(); |
||
73 | |||
74 | //get the posted mode |
||
75 | $mode = $rawData['mode']; |
||
76 | |||
77 | //get the form to add more fields |
||
78 | $form = $event->getForm(); |
||
79 | |||
80 | //the controller does not use the mode to construct the form, so we update it automatically |
||
81 | if ($mode === Widget::MODE_ENTITY) { |
||
82 | $this->addEntityFields($form, $options); |
||
83 | } |
||
84 | |||
85 | if ($mode === Widget::MODE_QUERY) { |
||
86 | $this->addQueryFields($form, $options); |
||
87 | } |
||
88 | if ($mode === Widget::MODE_BUSINESS_ENTITY) { |
||
89 | $this->addBusinessEntityFields($form, $options); |
||
90 | } |
||
91 | } |
||
92 | ); |
||
93 | } |
||
94 | |||
213 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.