Conditions | 2 |
Paths | 2 |
Total Lines | 60 |
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 |
||
163 | protected function addCascadeScript() |
||
164 | { |
||
165 | if (empty($this->conditions)) { |
||
166 | return; |
||
167 | } |
||
168 | |||
169 | $cascadeGroups = collect($this->conditions)->map(function ($condition) { |
||
170 | return [ |
||
171 | 'class' => $this->getCascadeClass($condition['value']), |
||
172 | 'operator' => $condition['operator'], |
||
173 | 'value' => $condition['value'], |
||
174 | ]; |
||
175 | })->toJson(); |
||
176 | |||
177 | $script = <<<SCRIPT |
||
178 | ;(function () { |
||
179 | var operator_table = { |
||
180 | '=': function(a, b) { |
||
181 | if ($.isArray(a) && $.isArray(b)) { |
||
182 | return $(a).not(b).length === 0 && $(b).not(a).length === 0; |
||
183 | } |
||
184 | |||
185 | return a == b; |
||
186 | }, |
||
187 | '>': function(a, b) { return a > b; }, |
||
188 | '<': function(a, b) { return a < b; }, |
||
189 | '>=': function(a, b) { return a >= b; }, |
||
190 | '<=': function(a, b) { return a <= b; }, |
||
191 | '!=': function(a, b) { |
||
192 | if ($.isArray(a) && $.isArray(b)) { |
||
193 | return !($(a).not(b).length === 0 && $(b).not(a).length === 0); |
||
194 | } |
||
195 | |||
196 | return a != b; |
||
197 | }, |
||
198 | 'in': function(a, b) { return $.inArray(a, b) != -1; }, |
||
199 | 'notIn': function(a, b) { return $.inArray(a, b) == -1; }, |
||
200 | 'has': function(a, b) { return $.inArray(b, a) != -1; }, |
||
201 | 'oneIn': function(a, b) { return a.filter(v => b.includes(v)).length >= 1; }, |
||
202 | 'oneNotIn': function(a, b) { return a.filter(v => b.includes(v)).length == 0; }, |
||
203 | }; |
||
204 | var cascade_groups = {$cascadeGroups}; |
||
205 | $('{$this->getElementClassSelector()}').on('{$this->cascadeEvent}', function (e) { |
||
206 | |||
207 | {$this->getFormFrontValue()} |
||
208 | |||
209 | cascade_groups.forEach(function (event) { |
||
210 | var group = $('div.cascade-group.'+event.class); |
||
211 | if( operator_table[event.operator](checked, event.value) ) { |
||
212 | group.removeClass('hide'); |
||
213 | } else { |
||
214 | group.addClass('hide'); |
||
215 | } |
||
216 | }); |
||
217 | }) |
||
218 | })(); |
||
219 | SCRIPT; |
||
220 | |||
221 | Admin::script($script); |
||
222 | } |
||
223 | |||
251 |
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.