Conditions | 16 |
Paths | 1 |
Total Lines | 80 |
Code Lines | 55 |
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 |
||
151 | public function buildExpression() |
||
152 | { |
||
153 | /** @var EditableFormField $formFieldWatch */ |
||
154 | $formFieldWatch = $this->ConditionField(); |
||
155 | //Encapsulated the action to the object |
||
156 | $action = $formFieldWatch->getJsEventHandler(); |
||
157 | |||
158 | // is this field a special option field |
||
159 | 2 | $checkboxField = $formFieldWatch->isCheckBoxField(); |
|
160 | $radioField = $formFieldWatch->isRadioField(); |
||
161 | $target = sprintf('$("%s")', $formFieldWatch->getSelectorFieldOnly()); |
||
162 | 2 | $fieldValue = Convert::raw2js($this->FieldValue); |
|
163 | |||
164 | 2 | $conditionOptions = [ |
|
165 | 'ValueLessThan' => '<', |
||
166 | 'ValueLessThanEqual' => '<=', |
||
167 | 2 | 'ValueGreaterThan' => '>', |
|
168 | 2 | 'ValueGreaterThanEqual' => '>=' |
|
169 | 2 | ]; |
|
170 | 2 | ||
171 | // and what should we evaluate |
||
172 | switch ($this->ConditionOption) { |
||
173 | 2 | case 'IsNotBlank': |
|
174 | 2 | case 'IsBlank': |
|
175 | 2 | $expression = ($checkboxField || $radioField) ? "!{$target}.is(\":checked\")" : "{$target}.val() == ''"; |
|
176 | if ((string) $this->ConditionOption === 'IsNotBlank') { |
||
177 | 2 | //Negate |
|
178 | $expression = "!({$expression})"; |
||
179 | 2 | } |
|
180 | 2 | break; |
|
181 | 2 | case 'HasValue': |
|
182 | case 'ValueNot': |
||
183 | if ($checkboxField) { |
||
184 | if ($formFieldWatch->isCheckBoxGroupField()) { |
||
185 | $expression = sprintf( |
||
186 | "$.inArray('%s', %s.filter(':checked').map(function(){ return $(this).val();}).get()) > -1", |
||
187 | $fieldValue, |
||
188 | 2 | $target |
|
189 | 2 | ); |
|
190 | 2 | } else { |
|
191 | $expression = "{$target}.prop('checked')"; |
||
192 | } |
||
193 | } elseif ($radioField) { |
||
194 | // We cannot simply get the value of the radio group, we need to find the checked option first. |
||
195 | $expression = sprintf( |
||
196 | '%s.closest(".field, .control-group").find("input:checked").val() == "%s"', |
||
197 | $target, |
||
198 | $fieldValue |
||
199 | ); |
||
200 | 2 | } else { |
|
201 | $expression = sprintf('%s.val() == "%s"', $target, $fieldValue); |
||
202 | } |
||
203 | |||
204 | if ((string) $this->ConditionOption === 'ValueNot') { |
||
205 | //Negate |
||
206 | $expression = "!({$expression})"; |
||
207 | } |
||
208 | 2 | break; |
|
209 | case 'ValueLessThan': |
||
210 | case 'ValueLessThanEqual': |
||
211 | 2 | case 'ValueGreaterThan': |
|
212 | case 'ValueGreaterThanEqual': |
||
213 | $expression = sprintf( |
||
214 | '%s.val() %s parseFloat("%s")', |
||
215 | 2 | $target, |
|
216 | 1 | $conditionOptions[$this->ConditionOption], |
|
217 | 1 | $fieldValue |
|
218 | 1 | ); |
|
219 | 1 | break; |
|
220 | 1 | default: |
|
221 | 1 | throw new LogicException("Unhandled rule {$this->ConditionOption}"); |
|
222 | 1 | break; |
|
223 | 1 | } |
|
224 | |||
225 | 1 | $result = [ |
|
226 | 1 | 'operation' => $expression, |
|
227 | 'event' => $action, |
||
228 | ]; |
||
229 | |||
230 | 2 | return $result; |
|
231 | } |
||
267 |