Conditions | 15 |
Total Lines | 89 |
Code Lines | 55 |
Lines | 29 |
Ratio | 32.58 % |
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.permissions.PermissionsMixin.modify_permission() 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 -*- |
||
231 | def modify_permission( |
||
232 | self, |
||
233 | permission_id: str, |
||
234 | *, |
||
235 | comment: Optional[str] = None, |
||
236 | name: Optional[str] = None, |
||
237 | resource_id: Optional[str] = None, |
||
238 | resource_type: Optional[EntityType] = None, |
||
239 | subject_id: Optional[str] = None, |
||
240 | subject_type: Optional[PermissionSubjectType] = None, |
||
241 | ) -> Any: |
||
242 | """Modifies an existing permission. |
||
243 | |||
244 | Arguments: |
||
245 | permission_id: UUID of permission to be modified. |
||
246 | comment: The comment on the permission. |
||
247 | name: Permission name, currently the name of a command. |
||
248 | subject_id: UUID of subject to whom the permission is granted |
||
249 | subject_type: Type of the subject user, group or role |
||
250 | resource_id: UUID of entity to which the permission applies |
||
251 | resource_type: Type of the resource. For Super permissions user, |
||
252 | group or role |
||
253 | |||
254 | Returns: |
||
255 | The response. See :py:meth:`send_command` for details. |
||
256 | """ |
||
257 | if not permission_id: |
||
258 | raise RequiredArgument( |
||
259 | function=self.modify_permission.__name__, |
||
260 | argument='permission_id', |
||
261 | ) |
||
262 | |||
263 | cmd = XmlCommand("modify_permission") |
||
264 | cmd.set_attribute("permission_id", permission_id) |
||
265 | |||
266 | if comment: |
||
267 | cmd.add_element("comment", comment) |
||
268 | |||
269 | if name: |
||
270 | cmd.add_element("name", name) |
||
271 | |||
272 | View Code Duplication | if resource_id or resource_type: |
|
273 | if not resource_id: |
||
274 | raise RequiredArgument( |
||
275 | function=self.modify_permission.__name__, |
||
276 | argument='resource_id', |
||
277 | ) |
||
278 | |||
279 | if not resource_type: |
||
280 | raise RequiredArgument( |
||
281 | function=self.modify_permission.__name__, |
||
282 | argument='resource_type', |
||
283 | ) |
||
284 | |||
285 | if not isinstance(resource_type, EntityType): |
||
286 | raise InvalidArgumentType( |
||
287 | function=self.modify_permission.__name__, |
||
288 | argument='resource_type', |
||
289 | arg_type=EntityType.__name__, |
||
290 | ) |
||
291 | |||
292 | _xmlresource = cmd.add_element( |
||
293 | "resource", attrs={"id": resource_id} |
||
294 | ) |
||
295 | _actual_resource_type = resource_type |
||
296 | if resource_type.value == EntityType.AUDIT.value: |
||
297 | _actual_resource_type = EntityType.TASK |
||
298 | elif resource_type.value == EntityType.POLICY.value: |
||
299 | _actual_resource_type = EntityType.SCAN_CONFIG |
||
300 | _xmlresource.add_element("type", _actual_resource_type.value) |
||
301 | |||
302 | if subject_id or subject_type: |
||
303 | if not subject_id: |
||
304 | raise RequiredArgument( |
||
305 | function=self.modify_permission.__name__, |
||
306 | argument='subject_id', |
||
307 | ) |
||
308 | |||
309 | if not isinstance(subject_type, PermissionSubjectType): |
||
310 | raise InvalidArgumentType( |
||
311 | function=self.modify_permission.__name__, |
||
312 | argument='subject_type', |
||
313 | arg_type=PermissionSubjectType.__name__, |
||
314 | ) |
||
315 | |||
316 | _xmlsubject = cmd.add_element("subject", attrs={"id": subject_id}) |
||
317 | _xmlsubject.add_element("type", subject_type.value) |
||
318 | |||
319 | return self._send_xml_command(cmd) |
||
320 |