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