Conditions | 18 |
Total Lines | 117 |
Code Lines | 67 |
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.gmpv214.entities.targets.TargetsMixin.create_target() 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 -*- |
||
34 | def create_target( |
||
35 | self, |
||
36 | name: str, |
||
37 | *, |
||
38 | asset_hosts_filter: Optional[str] = None, |
||
39 | hosts: Optional[List[str]] = None, |
||
40 | comment: Optional[str] = None, |
||
41 | exclude_hosts: Optional[List[str]] = None, |
||
42 | ssh_credential_id: Optional[str] = None, |
||
43 | ssh_credential_port: Optional[int] = None, |
||
44 | smb_credential_id: Optional[str] = None, |
||
45 | esxi_credential_id: Optional[str] = None, |
||
46 | snmp_credential_id: Optional[str] = None, |
||
47 | alive_test: Optional[AliveTest] = None, |
||
48 | allow_simultaneous_ips: Optional[bool] = None, |
||
49 | reverse_lookup_only: Optional[bool] = None, |
||
50 | reverse_lookup_unify: Optional[bool] = None, |
||
51 | port_range: Optional[str] = None, |
||
52 | port_list_id: Optional[str] = None, |
||
53 | ) -> Any: |
||
54 | """Create a new target |
||
55 | |||
56 | Arguments: |
||
57 | name: Name of the target |
||
58 | asset_hosts_filter: Filter to select target host from assets hosts |
||
59 | hosts: List of hosts addresses to scan |
||
60 | exclude_hosts: List of hosts addresses to exclude from scan |
||
61 | comment: Comment for the target |
||
62 | ssh_credential_id: UUID of a ssh credential to use on target |
||
63 | ssh_credential_port: The port to use for ssh credential |
||
64 | smb_credential_id: UUID of a smb credential to use on target |
||
65 | snmp_credential_id: UUID of a snmp credential to use on target |
||
66 | esxi_credential_id: UUID of a esxi credential to use on target |
||
67 | alive_test: Which alive test to use |
||
68 | allow_simultaneous_ips: Whether to scan multiple IPs of the |
||
69 | same host simultaneously |
||
70 | reverse_lookup_only: Whether to scan only hosts that have names |
||
71 | reverse_lookup_unify: Whether to scan only one IP when multiple IPs |
||
72 | have the same name. |
||
73 | port_range: Port range for the target |
||
74 | port_list_id: UUID of the port list to use on target |
||
75 | |||
76 | Returns: |
||
77 | The response. See :py:meth:`send_command` for details. |
||
78 | """ |
||
79 | if not name: |
||
80 | raise RequiredArgument( |
||
81 | function=self.create_target.__name__, argument='name' |
||
82 | ) |
||
83 | |||
84 | cmd = XmlCommand("create_target") |
||
85 | _xmlname = cmd.add_element("name", name) |
||
86 | |||
87 | if asset_hosts_filter: |
||
88 | cmd.add_element( |
||
89 | "asset_hosts", attrs={"filter": str(asset_hosts_filter)} |
||
90 | ) |
||
91 | elif hosts: |
||
92 | cmd.add_element("hosts", to_comma_list(hosts)) |
||
93 | else: |
||
94 | raise RequiredArgument( |
||
95 | function=self.create_target.__name__, |
||
96 | argument='hosts or asset_hosts_filter', |
||
97 | ) |
||
98 | |||
99 | if comment: |
||
100 | cmd.add_element("comment", comment) |
||
101 | |||
102 | if exclude_hosts: |
||
103 | cmd.add_element("exclude_hosts", to_comma_list(exclude_hosts)) |
||
104 | |||
105 | if ssh_credential_id: |
||
106 | _xmlssh = cmd.add_element( |
||
107 | "ssh_credential", attrs={"id": ssh_credential_id} |
||
108 | ) |
||
109 | if ssh_credential_port: |
||
110 | _xmlssh.add_element("port", str(ssh_credential_port)) |
||
111 | |||
112 | if smb_credential_id: |
||
113 | cmd.add_element("smb_credential", attrs={"id": smb_credential_id}) |
||
114 | |||
115 | if esxi_credential_id: |
||
116 | cmd.add_element("esxi_credential", attrs={"id": esxi_credential_id}) |
||
117 | |||
118 | if snmp_credential_id: |
||
119 | cmd.add_element("snmp_credential", attrs={"id": snmp_credential_id}) |
||
120 | |||
121 | if alive_test: |
||
122 | if not isinstance(alive_test, AliveTest): |
||
123 | raise InvalidArgumentType( |
||
124 | function=self.create_target.__name__, |
||
125 | argument='alive_test', |
||
126 | arg_type=AliveTest.__name__, |
||
127 | ) |
||
128 | |||
129 | cmd.add_element("alive_tests", alive_test.value) |
||
130 | |||
131 | if allow_simultaneous_ips is not None: |
||
132 | cmd.add_element( |
||
133 | "allow_simultaneous_ips", to_bool(allow_simultaneous_ips) |
||
134 | ) |
||
135 | |||
136 | if reverse_lookup_only is not None: |
||
137 | cmd.add_element("reverse_lookup_only", to_bool(reverse_lookup_only)) |
||
138 | |||
139 | if reverse_lookup_unify is not None: |
||
140 | cmd.add_element( |
||
141 | "reverse_lookup_unify", to_bool(reverse_lookup_unify) |
||
142 | ) |
||
143 | |||
144 | if port_range: |
||
145 | cmd.add_element("port_range", port_range) |
||
146 | |||
147 | if port_list_id: |
||
148 | cmd.add_element("port_list", attrs={"id": port_list_id}) |
||
149 | |||
150 | return self._send_xml_command(cmd) |
||
151 | |||
260 |