Conditions | 3 |
Paths | 3 |
Total Lines | 62 |
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 |
||
171 | protected function addCascadeScript() |
||
172 | { |
||
173 | if (empty($this->conditions)) { |
||
174 | return; |
||
175 | } |
||
176 | |||
177 | $group = []; |
||
178 | |||
179 | foreach ($this->conditions as $item) { |
||
180 | $group[] = [ |
||
181 | 'class' => $this->getCascadeClass($item['value']), |
||
182 | 'operator' => $item['operator'], |
||
183 | 'value' => $item['value'] |
||
184 | ]; |
||
185 | } |
||
186 | |||
187 | $cascadeGroups = json_encode($group); |
||
188 | |||
189 | $script = <<<SCRIPT |
||
190 | (function () { |
||
191 | var operator_table = { |
||
192 | '=': function(a, b) { |
||
193 | if ($.isArray(a) && $.isArray(b)) { |
||
194 | return $(a).not(b).length === 0 && $(b).not(a).length === 0; |
||
195 | } |
||
196 | |||
197 | return a == b; |
||
198 | }, |
||
199 | '>': function(a, b) { return a > b; }, |
||
200 | '<': function(a, b) { return a < b; }, |
||
201 | '>=': function(a, b) { return a >= b; }, |
||
202 | '<=': function(a, b) { return a <= b; }, |
||
203 | '!=': function(a, b) { |
||
204 | if ($.isArray(a) && $.isArray(b)) { |
||
205 | return !($(a).not(b).length === 0 && $(b).not(a).length === 0); |
||
206 | } |
||
207 | |||
208 | return a != b; |
||
209 | }, |
||
210 | 'in': function(a, b) { return $.inArray(a, b) != -1; }, |
||
211 | 'notIn': function(a, b) { return $.inArray(a, b) == -1; }, |
||
212 | 'has': function(a, b) { return $.inArray(b, a) != -1; }, |
||
213 | }; |
||
214 | var cascade_groups = {$cascadeGroups}; |
||
215 | $('{$this->getElementClassSelector()}').on('{$this->cascadeEvent}', function (e) { |
||
216 | |||
217 | {$this->getFormFrontValue()} |
||
218 | |||
219 | cascade_groups.forEach(function (event) { |
||
220 | var group = $('div.form-group.'+event.class); |
||
221 | if( operator_table[event.operator](checked, event.value) ) { |
||
222 | group.removeClass('hide'); |
||
223 | } else { |
||
224 | group.addClass('hide'); |
||
225 | } |
||
226 | }); |
||
227 | }) |
||
228 | })(); |
||
229 | SCRIPT; |
||
230 | |||
231 | Admin::script($script); |
||
232 | } |
||
233 | |||
261 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.