| Conditions | 14 | 
| Total Lines | 99 | 
| Code Lines | 40 | 
| 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:
Complex classes like ospd.protocol.OspRequest.process_target_element() 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 | # Copyright (C) 2014-2020 Greenbone Networks GmbH  | 
            ||
| 175 | @classmethod  | 
            ||
| 176 | def process_target_element(cls, scanner_target: Element) -> Dict:  | 
            ||
| 177 | """Receive an XML object with the target, ports and credentials to run  | 
            ||
| 178 | a scan against.  | 
            ||
| 179 | |||
| 180 | Arguments:  | 
            ||
| 181 | Single XML target element. The target has <hosts> and <ports>  | 
            ||
| 182 | subelements. Hosts can be a single host, a host range, a  | 
            ||
| 183 | comma-separated host list or a network address.  | 
            ||
| 184 | <ports> and <credentials> are optional. Therefore each  | 
            ||
| 185 | ospd-scanner should check for a valid ones if needed.  | 
            ||
| 186 | |||
| 187 | Example form:  | 
            ||
| 188 | |||
| 189 | <target>  | 
            ||
| 190 | <hosts>192.168.0.0/24</hosts>  | 
            ||
| 191 | <ports>22</ports>  | 
            ||
| 192 | <credentials>  | 
            ||
| 193 | <credential type="up" service="ssh" port="22">  | 
            ||
| 194 | <username>scanuser</username>  | 
            ||
| 195 | <password>mypass</password>  | 
            ||
| 196 | </credential>  | 
            ||
| 197 | <credential type="up" service="smb">  | 
            ||
| 198 | <username>smbuser</username>  | 
            ||
| 199 | <password>mypass</password>  | 
            ||
| 200 | </credential>  | 
            ||
| 201 | </credentials>  | 
            ||
| 202 | <alive_test></alive_test>  | 
            ||
| 203 | <alive_test_ports></alive_test_ports>  | 
            ||
| 204 | <reverse_lookup_only>1</reverse_lookup_only>  | 
            ||
| 205 | <reverse_lookup_unify>0</reverse_lookup_unify>  | 
            ||
| 206 | </target>  | 
            ||
| 207 | |||
| 208 | Return:  | 
            ||
| 209 |             A Dict  hosts, port, {credentials}, exclude_hosts, options]. | 
            ||
| 210 | |||
| 211 | Example form:  | 
            ||
| 212 | |||
| 213 |             { | 
            ||
| 214 | 'hosts': '192.168.0.0/24',  | 
            ||
| 215 | 'port': '22',  | 
            ||
| 216 |                 'credentials': {'smb': {'type': type, | 
            ||
| 217 | 'port': port,  | 
            ||
| 218 | 'username': username,  | 
            ||
| 219 | 'password': pass,  | 
            ||
| 220 | }  | 
            ||
| 221 | },  | 
            ||
| 222 | |||
| 223 | 'exclude_hosts': '',  | 
            ||
| 224 | 'finished_hosts': '',  | 
            ||
| 225 |                 'options': {'alive_test': 'ALIVE_TEST_CONSIDER_ALIVE', | 
            ||
| 226 | 'alive_test_ports: '22,80,123',  | 
            ||
| 227 | 'reverse_lookup_only': '1',  | 
            ||
| 228 | 'reverse_lookup_unify': '0',  | 
            ||
| 229 | },  | 
            ||
| 230 | }  | 
            ||
| 231 | """  | 
            ||
| 232 | if scanner_target:  | 
            ||
| 233 | exclude_hosts = ''  | 
            ||
| 234 | finished_hosts = ''  | 
            ||
| 235 | ports = ''  | 
            ||
| 236 | hosts = None  | 
            ||
| 237 |             credentials = {}  # type: Dict | 
            ||
| 238 |             options = {} | 
            ||
| 239 | |||
| 240 | for child in scanner_target:  | 
            ||
| 241 | if child.tag == 'hosts':  | 
            ||
| 242 | hosts = child.text  | 
            ||
| 243 | if child.tag == 'exclude_hosts':  | 
            ||
| 244 | exclude_hosts = child.text  | 
            ||
| 245 | if child.tag == 'finished_hosts':  | 
            ||
| 246 | finished_hosts = child.text  | 
            ||
| 247 | if child.tag == 'ports':  | 
            ||
| 248 | ports = child.text  | 
            ||
| 249 | if child.tag == 'credentials':  | 
            ||
| 250 | credentials = cls.process_credentials_elements(child)  | 
            ||
| 251 | if child.tag == 'alive_test_methods':  | 
            ||
| 252 | options['alive_test_methods'] = '1'  | 
            ||
| 253 | cls.process_alive_test_methods(child, options)  | 
            ||
| 254 | if child.tag == 'alive_test':  | 
            ||
| 255 | options['alive_test'] = child.text  | 
            ||
| 256 | if child.tag == 'alive_test_ports':  | 
            ||
| 257 | options['alive_test_ports'] = child.text  | 
            ||
| 258 | if child.tag == 'reverse_lookup_unify':  | 
            ||
| 259 | options['reverse_lookup_unify'] = child.text  | 
            ||
| 260 | if child.tag == 'reverse_lookup_only':  | 
            ||
| 261 | options['reverse_lookup_only'] = child.text  | 
            ||
| 262 | |||
| 263 | if hosts:  | 
            ||
| 264 |                 return { | 
            ||
| 265 | 'hosts': hosts,  | 
            ||
| 266 | 'ports': ports,  | 
            ||
| 267 | 'credentials': credentials,  | 
            ||
| 268 | 'exclude_hosts': exclude_hosts,  | 
            ||
| 269 | 'finished_hosts': finished_hosts,  | 
            ||
| 270 | 'options': options,  | 
            ||
| 271 | }  | 
            ||
| 272 | else:  | 
            ||
| 273 |                 raise OspdError('No target to scan') | 
            ||
| 274 | |||
| 298 |