Conditions | 16 |
Paths | 25 |
Total Lines | 116 |
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 |
||
43 | public function with(string $name, array $options = []): self |
||
44 | { |
||
45 | if (!$this->shouldApply()) { |
||
46 | return $this; |
||
47 | } |
||
48 | |||
49 | /* |
||
50 | * The current implementation should work with the following workflow: |
||
51 | * |
||
52 | * $formMapper |
||
53 | * ->with('group1') |
||
54 | * ->add('username') |
||
55 | * ->add('password') |
||
56 | * ->end() |
||
57 | * ->with('tab1', ['tab' => true]) |
||
58 | * ->with('group1') |
||
59 | * ->add('username') |
||
60 | * ->add('password') |
||
61 | * ->end() |
||
62 | * ->with('group2', ['collapsed' => true]) |
||
63 | * ->add('enabled') |
||
64 | * ->add('createdAt') |
||
65 | * ->end() |
||
66 | * ->end(); |
||
67 | * |
||
68 | */ |
||
69 | $defaultOptions = [ |
||
70 | 'collapsed' => false, |
||
71 | 'class' => false, |
||
72 | 'description' => false, |
||
73 | 'label' => $this->admin->getLabelTranslatorStrategy()->getLabel($name, $this->getName(), 'group'), |
||
74 | 'translation_domain' => null, |
||
75 | 'name' => $name, |
||
76 | 'box_class' => 'box box-primary', |
||
77 | 'empty_message' => 'message_form_group_empty', |
||
78 | 'empty_message_translation_domain' => 'SonataAdminBundle', |
||
79 | ]; |
||
80 | |||
81 | $code = $name; |
||
82 | |||
83 | // Open |
||
84 | if (\array_key_exists('tab', $options) && $options['tab']) { |
||
85 | $tabs = $this->getTabs(); |
||
86 | |||
87 | if ($this->currentTab) { |
||
|
|||
88 | if (isset($tabs[$this->currentTab]['auto_created']) && true === $tabs[$this->currentTab]['auto_created']) { |
||
89 | throw new \LogicException('New tab was added automatically when you have added field or group. You should close current tab before adding new one OR add tabs before adding groups and fields.'); |
||
90 | } |
||
91 | |||
92 | throw new \LogicException(sprintf( |
||
93 | 'You should close previous tab "%s" with end() before adding new tab "%s".', |
||
94 | $this->currentTab, |
||
95 | $name |
||
96 | )); |
||
97 | } |
||
98 | |||
99 | if ($this->currentGroup) { |
||
100 | throw new \LogicException(sprintf('You should open tab before adding new group "%s".', $name)); |
||
101 | } |
||
102 | |||
103 | if (!isset($tabs[$name])) { |
||
104 | $tabs[$name] = []; |
||
105 | } |
||
106 | |||
107 | $tabs[$code] = array_merge($defaultOptions, [ |
||
108 | 'auto_created' => false, |
||
109 | 'groups' => [], |
||
110 | ], $tabs[$code], $options); |
||
111 | |||
112 | $this->currentTab = $code; |
||
113 | } else { |
||
114 | if ($this->currentGroup) { |
||
115 | throw new \LogicException(sprintf( |
||
116 | 'You should close previous group "%s" with end() before adding new tab "%s".', |
||
117 | $this->currentGroup, |
||
118 | $name |
||
119 | )); |
||
120 | } |
||
121 | |||
122 | if (!$this->currentTab) { |
||
123 | // no tab define |
||
124 | $this->with('default', [ |
||
125 | 'tab' => true, |
||
126 | 'auto_created' => true, |
||
127 | 'translation_domain' => $options['translation_domain'] ?? null, |
||
128 | ]); // add new tab automatically |
||
129 | } |
||
130 | |||
131 | // if no tab is selected, we go the the main one named '_' .. |
||
132 | if ('default' !== $this->currentTab) { |
||
133 | // groups with the same name can be on different tabs, so we prefix them in order to make unique group name |
||
134 | $code = sprintf('%s.%s', $this->currentTab, $name); |
||
135 | } |
||
136 | |||
137 | $groups = $this->getGroups(); |
||
138 | if (!isset($groups[$code])) { |
||
139 | $groups[$code] = []; |
||
140 | } |
||
141 | |||
142 | $groups[$code] = array_merge($defaultOptions, [ |
||
143 | 'fields' => [], |
||
144 | ], $groups[$code], $options); |
||
145 | |||
146 | $this->currentGroup = $code; |
||
147 | $this->setGroups($groups); |
||
148 | $tabs = $this->getTabs(); |
||
149 | } |
||
150 | |||
151 | if ($this->currentGroup && isset($tabs[$this->currentTab]) && !\in_array($this->currentGroup, $tabs[$this->currentTab]['groups'], true)) { |
||
152 | $tabs[$this->currentTab]['groups'][] = $this->currentGroup; |
||
153 | } |
||
154 | |||
155 | $this->setTabs($tabs); |
||
156 | |||
157 | return $this; |
||
158 | } |
||
159 | |||
281 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: