Conditions | 2 |
Paths | 2 |
Total Lines | 168 |
Code Lines | 159 |
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 |
||
135 | function generate_javascript($uprefs) { |
||
136 | global $job_keywords; |
||
137 | echo "<script> |
||
138 | var ids = new Array; |
||
139 | var names = new Array; |
||
140 | var levels = new Array; |
||
141 | var parent = new Array; |
||
142 | var radio_value = new Array; |
||
143 | "; |
||
144 | foreach ($job_keywords as $id=>$k) { |
||
145 | $val = $uprefs[$id]; |
||
146 | echo " |
||
147 | names[$id] = '$k->name'; |
||
148 | levels[$id] = $k->level; |
||
149 | parent[$id] = $k->parent; |
||
150 | radio_value[$id] = $val; |
||
151 | ids.push($id); |
||
152 | "; |
||
153 | } |
||
154 | echo sprintf("var nkws = %d;\n", count($job_keywords)); |
||
155 | echo <<<EOT |
||
156 | var rows = new Array; |
||
157 | var texts = new Array; |
||
158 | var expanded = new Array; |
||
159 | var terminal = new Array; |
||
160 | var nprefs = new Array; |
||
161 | |||
162 | // initialize stuff |
||
163 | |||
164 | for (i=0; i<nkws; i++) { |
||
165 | terminal[ids[i]] = true; |
||
166 | nprefs[ids[i]] = 0; |
||
167 | } |
||
168 | for (i=0; i<nkws; i++) { |
||
169 | var j = ids[i]; |
||
170 | var rowid = "row"+j; |
||
171 | var textid = "text"+j; |
||
172 | rows[j] = document.getElementById(rowid); |
||
173 | texts[j] = document.getElementById(textid); |
||
174 | if (parent[j]) { |
||
175 | terminal[parent[j]] = false; |
||
176 | } |
||
177 | expanded[j] = false; |
||
178 | |||
179 | if (radio_value[j]) { |
||
180 | k = j; |
||
181 | while (1) { |
||
182 | k = parent[k]; |
||
183 | if (!k) break; |
||
184 | nprefs[k]++; |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | |||
189 | // Firefox doesn't set radio buttons correctly. |
||
190 | // |
||
191 | for (i=0; i<nkws; i++) { |
||
192 | var j = ids[i]; |
||
193 | if (!terminal[j]) continue; |
||
194 | for (k=0; k<3; k++) { |
||
195 | id = "radio"+j+"_"+k; |
||
196 | r = document.getElementById(id); |
||
197 | //console.log(id, r); |
||
198 | r.checked= (k == 1-radio_value[j]); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | var font_size = [120, 108, 92, 80]; |
||
203 | var indent = [0, 1.3, 2.6, 3.9]; |
||
204 | var button_indent = 1.0; |
||
205 | |||
206 | // -1: show "contract" button |
||
207 | // 0: no button |
||
208 | // 1: show "expand" button |
||
209 | // |
||
210 | function set_expand(k, val) { |
||
211 | //console.log('set_expand ', k, val); |
||
212 | var level = levels[k]; |
||
213 | var x = '<span style="font-size:'+font_size[level]+'%">'; |
||
214 | x += '<span style="padding-left:'+indent[level]+'em"/>'; |
||
215 | if (val < 0) { |
||
216 | x += '<a onclick="expand_contract('+k+')" id="t'+k+'" href=#/>⊟</a> '; |
||
217 | } else if (val == 0) { |
||
218 | x += '<span style="padding-left:'+button_indent+'em"/>'; |
||
219 | } else { |
||
220 | x += '<a onclick="expand_contract('+k+')" id="t'+k+'" href=#/>⊞</a> '; |
||
221 | } |
||
222 | x += names[k]; |
||
223 | texts[k].innerHTML = x; |
||
224 | } |
||
225 | |||
226 | function radio(k, val) { |
||
227 | //console.log('radio ', k, val, parent[k]); |
||
228 | old_val = radio_value[k]; |
||
229 | radio_value[k] = val; |
||
230 | inc = 0; |
||
231 | if (val && !old_val) inc = 1; |
||
232 | if (!val && old_val) inc = -1; |
||
233 | if (inc) { |
||
234 | while (1) { |
||
235 | k = parent[k]; |
||
236 | if (!k) break; |
||
237 | nprefs[k] += inc; |
||
238 | } |
||
239 | } |
||
240 | render(); |
||
241 | } |
||
242 | |||
243 | // click on expand/contract link |
||
244 | // |
||
245 | function expand_contract(k) { |
||
246 | expanded[k] = !expanded[k]; |
||
247 | set_expand(k, expanded[k]?-1:1); |
||
248 | var h = expanded[k]?false:true; |
||
249 | //console.log('expand_contract ', k, h); |
||
250 | render(); |
||
251 | return false; |
||
252 | } |
||
253 | |||
254 | // return true if all ancestrors of i are expanded or nprefs>0 |
||
255 | // |
||
256 | function ancestors_expanded(i) { |
||
257 | while (1) { |
||
258 | i = parent[i]; |
||
259 | if (!i) break; |
||
260 | if (!nprefs[i] && !expanded[i]) return false; |
||
261 | } |
||
262 | return true; |
||
263 | } |
||
264 | |||
265 | function render() { |
||
266 | for (i=0; i<nkws; i++) { |
||
267 | j = ids[i]; |
||
268 | if (terminal[j]) { |
||
269 | set_expand(j, 0); |
||
270 | if (nprefs[parent[j]]>0 || ancestors_expanded(j)) { |
||
271 | rows[j].hidden = false; |
||
272 | } else { |
||
273 | rows[j].hidden = true; |
||
274 | } |
||
275 | } else { |
||
276 | //console.log("nprefs ", j, nprefs[j]); |
||
277 | if (nprefs[j]) { |
||
278 | expanded[j] = true; |
||
279 | rows[j].hidden = false; |
||
280 | set_expand(j, 0); |
||
281 | } else { |
||
282 | p = parent[j]; |
||
283 | if (p) { |
||
284 | if (nprefs[parent[j]]>0 || ancestors_expanded(j)) { |
||
285 | rows[j].hidden = false; |
||
286 | set_expand(j, expanded[j]?-1:1); |
||
287 | } else { |
||
288 | rows[j].hidden = true; |
||
289 | } |
||
290 | } else { |
||
291 | rows[j].hidden = false; |
||
292 | set_expand(j, expanded[j]?-1:1); |
||
293 | } |
||
294 | } |
||
295 | } |
||
296 | } |
||
297 | } |
||
298 | |||
299 | render(); |
||
300 | |||
301 | EOT; |
||
302 | echo "</script>\n"; |
||
303 | } |
||
388 |