Conditions | 11 |
Paths | 9 |
Total Lines | 50 |
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 |
||
21 | public function create() |
||
22 | { |
||
23 | parent::create(); |
||
24 | |||
25 | $output = '<' . $this->inputType; |
||
26 | |||
27 | foreach ($this->definition['attributes'] as $attr => $value) { |
||
28 | |||
29 | if ($attr === 'type') { |
||
30 | continue; |
||
31 | } |
||
32 | |||
33 | $output .= ' ' . $attr . '="' . $value . '"'; |
||
34 | |||
35 | } |
||
36 | |||
37 | $output .= '>'; |
||
38 | |||
39 | $definition = $this->definition; |
||
40 | $isSelected = function($val) use (&$definition) { |
||
41 | |||
42 | if ($this->isPost() && empty($val)) { |
||
43 | unset($definition['selected']); |
||
44 | } |
||
45 | |||
46 | if (!empty($this->getValue()) && $val === $this->getValue()) { |
||
47 | return true; |
||
48 | } elseif (!empty($definition['selected']) && $val === $definition['selected']) { |
||
49 | return true; |
||
50 | } |
||
51 | |||
52 | return false; |
||
53 | |||
54 | }; |
||
55 | |||
56 | foreach ($definition['options'] as $val => $text) { |
||
57 | |||
58 | $selected = $isSelected($val) === true ? ' selected="selected"' : ''; |
||
59 | $option = '<option value="' . $val .'"%s>' . $text . '</option>'; |
||
60 | |||
61 | $output .= sprintf($option, $selected); |
||
62 | |||
63 | } |
||
64 | |||
65 | $output .= '</select>'; |
||
66 | |||
67 | $this->element = $output; |
||
68 | |||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | } |