Conditions | 14 |
Total Lines | 76 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 210 |
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:
Complex classes like ssg.rule_yaml.sort_section_keys() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """ |
||
226 | def sort_section_keys(file_path, file_contents, sections, sort_func=None): |
||
227 | """ |
||
228 | Sort subkeys in a YAML file's section. |
||
229 | """ |
||
230 | |||
231 | if isinstance(sections, str): |
||
232 | sections = [sections] |
||
233 | |||
234 | new_contents = file_contents[:] |
||
235 | |||
236 | for section in sections: |
||
237 | section_range = get_section_lines(file_path, new_contents, section) |
||
238 | if not section_range: |
||
239 | continue |
||
240 | |||
241 | # Start by parsing the lines as YAML. |
||
242 | parsed_section = parse_from_yaml(new_contents, section_range) |
||
243 | |||
244 | # Ignore the section header. This header is included in the start range, |
||
245 | # so just increment by one. |
||
246 | start_offset = 1 |
||
247 | while not new_contents[section_range.start + start_offset].strip(): |
||
248 | start_offset += 1 |
||
249 | |||
250 | # Ignore any trailing empty lines. |
||
251 | end_offset = 0 |
||
252 | while not new_contents[section_range.end - end_offset].strip(): |
||
253 | end_offset += 1 |
||
254 | |||
255 | # Validate we only have a single section. |
||
256 | assert len(parsed_section.keys()) == 1 |
||
257 | |||
258 | # Sort the parsed subkeys. |
||
259 | parent_key = list(parsed_section.keys())[0] |
||
260 | subkeys = sorted(parsed_section[parent_key].keys(), key=sort_func) |
||
261 | |||
262 | # Don't bother if there are zero or one subkeys. Sorting order thus |
||
263 | # doesn't matter. |
||
264 | if not subkeys or len(subkeys) == 1: |
||
265 | continue |
||
266 | |||
267 | # Now we need to map sorted subkeys onto lines in the new contents, |
||
268 | # so we can re-order them appropriately. We'll assume the section is |
||
269 | # small so we'll do it in O(n^2). |
||
270 | subkey_mapping = dict() |
||
271 | for key in subkeys: |
||
272 | our_line = None |
||
273 | spaced_key = ' ' + key + ':' |
||
274 | tabbed_key = '\t' + key + ':' |
||
275 | range_start = section_range.start + start_offset |
||
276 | range_end = section_range.end - end_offset + 1 |
||
277 | for line_num in range(range_start, range_end): |
||
278 | this_line = new_contents[line_num] |
||
279 | if spaced_key in this_line or tabbed_key in this_line: |
||
280 | if our_line: |
||
281 | # Not supposed to be possible to have multiple keys |
||
282 | # matching the same value in this file. We should've |
||
283 | # already fixed this with fix-rules.py's duplicate_subkeys. |
||
284 | msg = "File {0} has duplicated key {1}: {2} vs {3}" |
||
285 | msg = msg.format(file_path, key, our_line, this_line) |
||
286 | raise ValueError(msg) |
||
287 | our_line = this_line |
||
288 | assert our_line |
||
289 | subkey_mapping[key] = our_line |
||
290 | |||
291 | # Now we'll remove all the section's subkeys and start over. Include |
||
292 | # section header but not any of the keys (or potential blank lines |
||
293 | # in the interior -- but we preserve them on either end of the |
||
294 | # section). |
||
295 | prefix = new_contents[:section_range.start+start_offset] |
||
296 | contents = list(map(lambda key: subkey_mapping[key], subkeys)) |
||
297 | suffix = new_contents[section_range.end+1-end_offset:] |
||
298 | |||
299 | new_contents = prefix + contents + suffix |
||
300 | |||
301 | return new_contents |
||
302 |