| Conditions | 23 |
| Total Lines | 104 |
| Code Lines | 52 |
| 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 gvm.protocols.gmpv8.Gmp.modify_credential() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | # -*- coding: utf-8 -*- |
||
| 260 | def modify_credential(self, credential_id, name=None, comment=None, |
||
| 261 | allow_insecure=None, certificate=None, |
||
| 262 | key_phrase=None, private_key=None, login=None, |
||
| 263 | password=None, auth_algorithm=None, community=None, |
||
| 264 | privacy_algorithm=None, privacy_password=None, |
||
| 265 | credential_type=None, public_key=None): |
||
| 266 | """Modifies an existing credential. |
||
| 267 | |||
| 268 | Arguments: |
||
| 269 | credential_id (str): UUID of the credential |
||
| 270 | name (str, optional): Name of the credential |
||
| 271 | comment (str, optional): Comment for the credential |
||
| 272 | allow_insecure (boolean, optional): Whether to allow insecure use of |
||
| 273 | the credential |
||
| 274 | certificate (str, optional): Certificate for the credential |
||
| 275 | key_phrase (str, optional): Key passphrase for the private key |
||
| 276 | private_key (str, optional): Private key to use for login |
||
| 277 | login (str, optional): Username for the credential |
||
| 278 | password (str, optional): Password for the credential |
||
| 279 | auth_algorithm (str, optional): The auth_algorithm, |
||
| 280 | either md5 or sha1. |
||
| 281 | community (str, optional): The SNMP community |
||
| 282 | privacy_algorithm (str, optional): The SNMP privacy algorithm, |
||
| 283 | either aes or des. |
||
| 284 | privacy_password (str, optional): The SNMP privacy password |
||
| 285 | credential_type (str, optional): The credential type. One of 'cc', |
||
| 286 | 'snmp', 'up', 'usk', 'smime', 'pgp' |
||
| 287 | public_key: (str, optional): PGP public key in *armor* plain text |
||
| 288 | format |
||
| 289 | |||
| 290 | Returns: |
||
| 291 | The response. See :py:meth:`send_command` for details. |
||
| 292 | """ |
||
| 293 | if not credential_id: |
||
| 294 | raise RequiredArgument('modify_credential requires ' |
||
| 295 | 'a credential_id attribute') |
||
| 296 | |||
| 297 | cmd = XmlCommand('modify_credential') |
||
| 298 | cmd.set_attribute('credential_id', credential_id) |
||
| 299 | |||
| 300 | if credential_type: |
||
| 301 | if credential_type not in CREDENTIAL_TYPES: |
||
| 302 | raise InvalidArgument( |
||
| 303 | 'modify_credential requires type to be either cc, snmp, up ' |
||
| 304 | 'smime, pgp or usk' |
||
| 305 | ) |
||
| 306 | cmd.add_element('type', credential_type) |
||
| 307 | |||
| 308 | if comment: |
||
| 309 | cmd.add_element('comment', comment) |
||
| 310 | |||
| 311 | if name: |
||
| 312 | cmd.add_element('name', name) |
||
| 313 | |||
| 314 | if allow_insecure is not None: |
||
| 315 | cmd.add_element('allow_insecure', _to_bool(allow_insecure)) |
||
| 316 | |||
| 317 | if certificate: |
||
| 318 | cmd.add_element('certificate', certificate) |
||
| 319 | |||
| 320 | if key_phrase or private_key: |
||
| 321 | if not key_phrase or not private_key: |
||
| 322 | raise RequiredArgument('modify_credential requires ' |
||
| 323 | 'a key_phrase and private_key arguments') |
||
| 324 | _xmlkey = cmd.add_element('key') |
||
| 325 | _xmlkey.add_element('phrase', key_phrase) |
||
| 326 | _xmlkey.add_element('private', private_key) |
||
| 327 | |||
| 328 | if login: |
||
| 329 | cmd.add_element('login', login) |
||
| 330 | |||
| 331 | if password: |
||
| 332 | cmd.add_element('password', password) |
||
| 333 | |||
| 334 | if auth_algorithm: |
||
| 335 | if auth_algorithm not in ('md5', 'sha1'): |
||
| 336 | raise InvalidArgument('modify_credential requires ' |
||
| 337 | 'auth_algorithm to be either ' |
||
| 338 | 'md5 or sha1') |
||
| 339 | cmd.add_element('auth_algorithm', auth_algorithm) |
||
| 340 | |||
| 341 | if community: |
||
| 342 | cmd.add_element('community', community) |
||
| 343 | |||
| 344 | if privacy_algorithm is not None or privacy_password is not None: |
||
| 345 | _xmlprivacy = cmd.add_element('privacy') |
||
| 346 | |||
| 347 | if privacy_algorithm is not None: |
||
| 348 | if privacy_algorithm not in ('aes', 'des'): |
||
| 349 | raise InvalidArgument( |
||
| 350 | 'modify_credential requires privacy_algorithm to be ' |
||
| 351 | 'either aes or des' |
||
| 352 | ) |
||
| 353 | |||
| 354 | _xmlprivacy.add_element('algorithm', privacy_algorithm) |
||
| 355 | |||
| 356 | if privacy_password is not None: |
||
| 357 | _xmlprivacy.add_element('password', privacy_password) |
||
| 358 | |||
| 359 | if public_key: |
||
| 360 | _xmlkey = cmd.add_element('key') |
||
| 361 | _xmlkey.add_element('public', public_key) |
||
| 362 | |||
| 363 | return self._send_xml_command(cmd) |
||
| 364 |