| Conditions | 14 |
| Total Lines | 57 |
| Code Lines | 42 |
| Lines | 57 |
| Ratio | 100 % |
| Tests | 14 |
| CRAP Score | 48.8837 |
| 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 sdoc.helper.Html.Html.generate_attribute() 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 | 1 | from typing import Dict, Optional |
|
| 29 | 1 | @staticmethod |
|
| 30 | 1 | def generate_attribute(name: str, value: str) -> str: |
|
| 31 | """ |
||
| 32 | Returns a string proper conversion of special characters to HTML entities of an attribute of a HTML tag. |
||
| 33 | |||
| 34 | :param str name: The name of the attribute. |
||
| 35 | :param str value: The value of the attribute. |
||
| 36 | """ |
||
| 37 | 1 | html = '' |
|
| 38 | |||
| 39 | # Boolean attributes. |
||
| 40 | 1 | if name in ('autofocus', |
|
| 41 | 'checked', |
||
| 42 | 'disabled', |
||
| 43 | 'hidden', |
||
| 44 | 'ismap', |
||
| 45 | 'multiple', |
||
| 46 | 'novalidate', |
||
| 47 | 'readonly', |
||
| 48 | 'required', |
||
| 49 | 'selected', |
||
| 50 | 'spellcheck'): |
||
| 51 | if value: |
||
| 52 | html += ' ' |
||
| 53 | html += name |
||
| 54 | html += '="' |
||
| 55 | html += name |
||
| 56 | html += '"' |
||
| 57 | |||
| 58 | # Annoying boolean attribute exceptions. |
||
| 59 | 1 | elif name in ('draggable', 'contenteditable'): |
|
| 60 | if value is not None: |
||
| 61 | html += ' ' |
||
| 62 | html += name |
||
| 63 | html += '="true"' if value else '="false"' |
||
| 64 | |||
| 65 | 1 | elif name == 'autocomplete': |
|
| 66 | if value is not None: |
||
| 67 | html += ' ' |
||
| 68 | html += name |
||
| 69 | html += '="on"' if value else '="off"' |
||
| 70 | |||
| 71 | 1 | elif name == 'translate': |
|
| 72 | if value is not None: |
||
| 73 | html += ' ' |
||
| 74 | html += name |
||
| 75 | html += '="yes"' if value else '="no"' |
||
| 76 | |||
| 77 | else: |
||
| 78 | 1 | if value is not None and value != '': |
|
| 79 | 1 | html += ' ' |
|
| 80 | 1 | html += Html.escape(name) |
|
| 81 | 1 | html += '="' |
|
| 82 | 1 | html += Html.escape(value) |
|
| 83 | 1 | html += '"' |
|
| 84 | |||
| 85 | 1 | return html |
|
| 86 | |||
| 152 |