Conditions | 7 |
Paths | 1 |
Total Lines | 94 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 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 |
||
22 | public function buildForm(FormBuilderInterface $builder, array $options) |
||
23 | { |
||
24 | $builder |
||
25 | ->add('name', 'text', array('label' => 'game.name')) |
||
26 | ->add('port', 'integer', array('label' => 'game.port')) |
||
27 | ->add('mode', 'choice', array( |
||
28 | 'choices' => SteamServer::getModeList(), |
||
29 | 'empty_value' => 'steam.chooseGameMode', |
||
30 | 'label' => 'steam.gameMode', |
||
31 | 'required' => false, |
||
32 | )) |
||
33 | ->add('maxplayers', 'integer', array('label' => 'game.maxplayers')) |
||
34 | ->add('rconPassword', 'text', array( |
||
35 | 'label' => 'game.rcon.password', |
||
36 | // 'required' => empty($options['data']), |
||
37 | )) |
||
38 | ->add('svPassword', 'text', array('label' => 'steam.svPassword', 'required' => false)) |
||
39 | ->add('alreadyInstalled', 'choice', array( |
||
40 | 'choices' => array(1 => 'game.yes', 0 => 'game.no'), |
||
41 | 'label' => 'game.isAlreadyInstalled', |
||
42 | 'expanded' => true, |
||
43 | )) |
||
44 | ; |
||
45 | |||
46 | $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { |
||
47 | /** @var Symfony\Component\Form\FormInterface $form */ |
||
48 | $form = $event->getForm(); |
||
49 | /** @var DP\GameServer\SteamServerBundle\Entity\SteamServer $steam */ |
||
50 | $steam = $event->getData(); |
||
51 | |||
52 | $isUpdateForm = ($steam->getId() != null); |
||
53 | |||
54 | $form |
||
55 | ->add('machine', 'dedipanel_machine_entity', array( |
||
56 | 'disabled' => $isUpdateForm, |
||
57 | )) |
||
58 | ->add('game', 'entity', array( |
||
59 | 'label' => 'game.selectGame', |
||
60 | 'class' => 'DPGameBundle:Game', |
||
61 | 'query_builder' => function($repo) { |
||
62 | return $repo->getQBAvailableSteamGames(); |
||
63 | }, |
||
64 | 'disabled' => $isUpdateForm, |
||
65 | )) |
||
66 | ->add('dir', 'text', array( |
||
67 | 'label' => 'game.dir', |
||
68 | 'disabled' => $isUpdateForm, |
||
69 | )) |
||
70 | ; |
||
71 | |||
72 | if ($steam->getId() != null) { |
||
73 | $form->remove('alreadyInstalled'); |
||
74 | |||
75 | if ($steam->getInstallationStatus() > 100) { |
||
76 | $form->add('rebootAt', 'time', array( |
||
77 | 'label' => 'steam.rebootAt', |
||
78 | 'required' => false |
||
79 | )); |
||
80 | } |
||
81 | if ($steam->getMachine()->getNbCore() != null) { |
||
82 | $choices = array_combine( |
||
83 | range(0, $steam->getMachine()->getNbCore()-1), |
||
84 | range(1, $steam->getMachine()->getNbCore()) |
||
85 | ); |
||
86 | |||
87 | $form->add('core', 'choice', array( |
||
88 | 'label' => 'game.core', |
||
89 | 'choices' => $choices, |
||
90 | 'multiple' => true, |
||
91 | 'required' => false, |
||
92 | 'expanded' => true, |
||
93 | )); |
||
94 | }; |
||
95 | } |
||
96 | }); |
||
97 | |||
98 | $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) { |
||
99 | /** @var Symfony\Component\Form\FormInterface $form */ |
||
100 | $form = $event->getForm(); |
||
101 | /** @var DP\GameServer\SteamServerBundle\Entity\SteamServer $steam */ |
||
102 | $steam = $event->getData(); |
||
103 | |||
104 | if ($steam->getGame() !== null && $steam->getGame()->getAppId() == 740) { // == csgo |
||
105 | $form->add('mode', 'choice', array( |
||
106 | 'choices' => SteamServer::getModeList(), |
||
107 | 'empty_value' => 'steam.chooseGameMode', |
||
108 | 'label' => 'steam.gameMode', |
||
109 | )); |
||
110 | } |
||
111 | elseif ($steam->getGame() !== null) { |
||
112 | $form->remove('mode'); |
||
113 | } |
||
114 | }); |
||
115 | } |
||
116 | |||
122 |