Conditions | 18 |
Total Lines | 108 |
Code Lines | 62 |
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.modify_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 -*- |
||
152 | def modify_target( |
||
153 | self, |
||
154 | target_id: str, |
||
155 | *, |
||
156 | name: Optional[str] = None, |
||
157 | comment: Optional[str] = None, |
||
158 | hosts: Optional[List[str]] = None, |
||
159 | exclude_hosts: Optional[List[str]] = None, |
||
160 | ssh_credential_id: Optional[str] = None, |
||
161 | ssh_credential_port: Optional[bool] = None, |
||
162 | smb_credential_id: Optional[str] = None, |
||
163 | esxi_credential_id: Optional[str] = None, |
||
164 | snmp_credential_id: Optional[str] = None, |
||
165 | alive_test: Optional[AliveTest] = None, |
||
166 | allow_simultaneous_ips: Optional[bool] = None, |
||
167 | reverse_lookup_only: Optional[bool] = None, |
||
168 | reverse_lookup_unify: Optional[bool] = None, |
||
169 | port_list_id: Optional[str] = None, |
||
170 | ) -> Any: |
||
171 | """Modifies an existing target. |
||
172 | |||
173 | Arguments: |
||
174 | target_id: ID of target to modify. |
||
175 | comment: Comment on target. |
||
176 | name: Name of target. |
||
177 | hosts: List of target hosts. |
||
178 | exclude_hosts: A list of hosts to exclude. |
||
179 | ssh_credential_id: UUID of SSH credential to use on target. |
||
180 | ssh_credential_port: The port to use for ssh credential |
||
181 | smb_credential_id: UUID of SMB credential to use on target. |
||
182 | esxi_credential_id: UUID of ESXi credential to use on target. |
||
183 | snmp_credential_id: UUID of SNMP credential to use on target. |
||
184 | port_list_id: UUID of port list describing ports to scan. |
||
185 | alive_test: Which alive tests to use. |
||
186 | allow_simultaneous_ips: Whether to scan multiple IPs of the |
||
187 | same host simultaneously |
||
188 | reverse_lookup_only: Whether to scan only hosts that have names. |
||
189 | reverse_lookup_unify: Whether to scan only one IP when multiple IPs |
||
190 | have the same name. |
||
191 | |||
192 | Returns: |
||
193 | The response. See :py:meth:`send_command` for details. |
||
194 | """ |
||
195 | if not target_id: |
||
196 | raise RequiredArgument( |
||
197 | function=self.modify_target.__name__, argument='target_id' |
||
198 | ) |
||
199 | |||
200 | cmd = XmlCommand("modify_target") |
||
201 | cmd.set_attribute("target_id", target_id) |
||
202 | |||
203 | if comment: |
||
204 | cmd.add_element("comment", comment) |
||
205 | |||
206 | if name: |
||
207 | cmd.add_element("name", name) |
||
208 | |||
209 | if hosts: |
||
210 | cmd.add_element("hosts", to_comma_list(hosts)) |
||
211 | if exclude_hosts is None: |
||
212 | exclude_hosts = [''] |
||
213 | |||
214 | if exclude_hosts: |
||
215 | cmd.add_element("exclude_hosts", to_comma_list(exclude_hosts)) |
||
216 | |||
217 | if alive_test: |
||
218 | if not isinstance(alive_test, AliveTest): |
||
219 | raise InvalidArgumentType( |
||
220 | function=self.modify_target.__name__, |
||
221 | argument='alive_test', |
||
222 | arg_type=AliveTest.__name__, |
||
223 | ) |
||
224 | cmd.add_element("alive_tests", alive_test.value) |
||
225 | |||
226 | if ssh_credential_id: |
||
227 | _xmlssh = cmd.add_element( |
||
228 | "ssh_credential", attrs={"id": ssh_credential_id} |
||
229 | ) |
||
230 | |||
231 | if ssh_credential_port: |
||
232 | _xmlssh.add_element("port", str(ssh_credential_port)) |
||
233 | |||
234 | if smb_credential_id: |
||
235 | cmd.add_element("smb_credential", attrs={"id": smb_credential_id}) |
||
236 | |||
237 | if esxi_credential_id: |
||
238 | cmd.add_element("esxi_credential", attrs={"id": esxi_credential_id}) |
||
239 | |||
240 | if snmp_credential_id: |
||
241 | cmd.add_element("snmp_credential", attrs={"id": snmp_credential_id}) |
||
242 | |||
243 | if allow_simultaneous_ips is not None: |
||
244 | cmd.add_element( |
||
245 | "allow_simultaneous_ips", to_bool(allow_simultaneous_ips) |
||
246 | ) |
||
247 | |||
248 | if reverse_lookup_only is not None: |
||
249 | cmd.add_element("reverse_lookup_only", to_bool(reverse_lookup_only)) |
||
250 | |||
251 | if reverse_lookup_unify is not None: |
||
252 | cmd.add_element( |
||
253 | "reverse_lookup_unify", to_bool(reverse_lookup_unify) |
||
254 | ) |
||
255 | |||
256 | if port_list_id: |
||
257 | cmd.add_element("port_list", attrs={"id": port_list_id}) |
||
258 | |||
259 | return self._send_xml_command(cmd) |
||
260 |