Conditions | 19 |
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.gmpv208.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 -*- |
||
90 | def create_target( |
||
91 | self, |
||
92 | name: str, |
||
93 | *, |
||
94 | asset_hosts_filter: Optional[str] = None, |
||
95 | hosts: Optional[List[str]] = None, |
||
96 | comment: Optional[str] = None, |
||
97 | exclude_hosts: Optional[List[str]] = None, |
||
98 | ssh_credential_id: Optional[str] = None, |
||
99 | ssh_credential_port: Optional[int] = None, |
||
100 | smb_credential_id: Optional[str] = None, |
||
101 | esxi_credential_id: Optional[str] = None, |
||
102 | snmp_credential_id: Optional[str] = None, |
||
103 | alive_test: Optional[AliveTest] = None, |
||
104 | reverse_lookup_only: Optional[bool] = None, |
||
105 | reverse_lookup_unify: Optional[bool] = None, |
||
106 | port_range: Optional[str] = None, |
||
107 | port_list_id: Optional[str] = None, |
||
108 | ) -> Any: |
||
109 | """Create a new target |
||
110 | |||
111 | Arguments: |
||
112 | name: Name of the target |
||
113 | asset_hosts_filter: Filter to select target host from assets hosts |
||
114 | hosts: List of hosts addresses to scan |
||
115 | exclude_hosts: List of hosts addresses to exclude from scan |
||
116 | comment: Comment for the target |
||
117 | ssh_credential_id: UUID of a ssh credential to use on target |
||
118 | ssh_credential_port: The port to use for ssh credential |
||
119 | smb_credential_id: UUID of a smb credential to use on target |
||
120 | snmp_credential_id: UUID of a snmp credential to use on target |
||
121 | esxi_credential_id: UUID of a esxi credential to use on target |
||
122 | alive_test: Which alive test to use |
||
123 | reverse_lookup_only: Whether to scan only hosts that have names |
||
124 | reverse_lookup_unify: Whether to scan only one IP when multiple IPs |
||
125 | have the same name. |
||
126 | port_range: Port range for the target |
||
127 | port_list_id: UUID of the port list to use on target |
||
128 | |||
129 | Returns: |
||
130 | The response. See :py:meth:`send_command` for details. |
||
131 | """ |
||
132 | |||
133 | cmd = XmlCommand("create_target") |
||
134 | _xmlname = cmd.add_element("name", name) |
||
135 | |||
136 | if not name: |
||
137 | raise RequiredArgument( |
||
138 | function=self.create_target.__name__, argument='name' |
||
139 | ) |
||
140 | |||
141 | if asset_hosts_filter: |
||
142 | cmd.add_element( |
||
143 | "asset_hosts", attrs={"filter": str(asset_hosts_filter)} |
||
144 | ) |
||
145 | elif hosts: |
||
146 | cmd.add_element("hosts", to_comma_list(hosts)) |
||
147 | else: |
||
148 | raise RequiredArgument( |
||
149 | function=self.create_target.__name__, |
||
150 | argument='hosts or asset_hosts_filter', |
||
151 | ) |
||
152 | |||
153 | if comment: |
||
154 | cmd.add_element("comment", comment) |
||
155 | |||
156 | if exclude_hosts: |
||
157 | cmd.add_element("exclude_hosts", to_comma_list(exclude_hosts)) |
||
158 | |||
159 | if ssh_credential_id: |
||
160 | _xmlssh = cmd.add_element( |
||
161 | "ssh_credential", attrs={"id": ssh_credential_id} |
||
162 | ) |
||
163 | if ssh_credential_port: |
||
164 | _xmlssh.add_element("port", str(ssh_credential_port)) |
||
165 | |||
166 | if smb_credential_id: |
||
167 | cmd.add_element("smb_credential", attrs={"id": smb_credential_id}) |
||
168 | |||
169 | if esxi_credential_id: |
||
170 | cmd.add_element("esxi_credential", attrs={"id": esxi_credential_id}) |
||
171 | |||
172 | if snmp_credential_id: |
||
173 | cmd.add_element("snmp_credential", attrs={"id": snmp_credential_id}) |
||
174 | |||
175 | if alive_test: |
||
176 | if not isinstance(alive_test, AliveTest): |
||
177 | raise InvalidArgumentType( |
||
178 | function=self.create_target.__name__, |
||
179 | argument='alive_test', |
||
180 | arg_type=AliveTest.__name__, |
||
181 | ) |
||
182 | |||
183 | cmd.add_element("alive_tests", alive_test.value) |
||
184 | |||
185 | if reverse_lookup_only is not None: |
||
186 | cmd.add_element("reverse_lookup_only", to_bool(reverse_lookup_only)) |
||
187 | |||
188 | if reverse_lookup_unify is not None: |
||
189 | cmd.add_element( |
||
190 | "reverse_lookup_unify", to_bool(reverse_lookup_unify) |
||
191 | ) |
||
192 | |||
193 | # since 20.08 one of port_range or port_list_id is required! |
||
194 | if not port_range and not port_list_id: |
||
195 | raise RequiredArgument( |
||
196 | function=self.create_target.__name__, |
||
197 | argument='port_range or port_list_id', |
||
198 | ) |
||
199 | |||
200 | if port_range: |
||
201 | cmd.add_element("port_range", port_range) |
||
202 | |||
203 | if port_list_id: |
||
204 | cmd.add_element("port_list", attrs={"id": port_list_id}) |
||
205 | |||
206 | return self._send_xml_command(cmd) |
||
207 | |||
385 |