Conditions | 10 |
Paths | 32 |
Total Lines | 43 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
94 | public function getAttributesHTML($attributes = null) |
||
95 | { |
||
96 | $exclude = null; |
||
97 | |||
98 | if (is_string($attributes)) { |
||
99 | $exclude = func_get_args(); |
||
100 | } |
||
101 | |||
102 | if (!$attributes || is_string($attributes)) { |
||
103 | $attributes = $this->getAttributes(); |
||
104 | } |
||
105 | |||
106 | $attributes = (array) $attributes; |
||
107 | |||
108 | $attributes = array_filter($attributes, function ($v) { |
||
109 | return ($v || $v === 0 || $v === '0'); |
||
110 | }); |
||
111 | |||
112 | if ($exclude) { |
||
113 | $attributes = array_diff_key( |
||
114 | $attributes, |
||
115 | array_flip($exclude) |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | // Create markup |
||
120 | $parts = []; |
||
121 | |||
122 | foreach ($attributes as $name => $value) { |
||
123 | if ($value === true) { |
||
124 | $value = $name; |
||
125 | } else { |
||
126 | if (is_scalar($value)) { |
||
127 | $value = (string) $value; |
||
128 | } else { |
||
129 | $value = json_encode($value); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | $parts[] = sprintf('%s="%s"', Convert::raw2att($name), Convert::raw2att($value)); |
||
|
|||
134 | } |
||
135 | |||
136 | return implode(' ', $parts); |
||
137 | } |
||
139 |