Conditions | 12 |
Total Lines | 83 |
Code Lines | 49 |
Lines | 31 |
Ratio | 37.35 % |
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.create_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 -*- |
||
81 | def create_permission( |
||
82 | self, |
||
83 | name: str, |
||
84 | subject_id: str, |
||
85 | subject_type: PermissionSubjectType, |
||
86 | *, |
||
87 | resource_id: Optional[str] = None, |
||
88 | resource_type: Optional[EntityType] = None, |
||
89 | comment: Optional[str] = None, |
||
90 | ) -> Any: |
||
91 | """Create a new permission |
||
92 | |||
93 | Arguments: |
||
94 | name: Name of the new permission |
||
95 | subject_id: UUID of subject to whom the permission is granted |
||
96 | subject_type: Type of the subject user, group or role |
||
97 | comment: Comment for the permission |
||
98 | resource_id: UUID of entity to which the permission applies |
||
99 | resource_type: Type of the resource. For Super permissions user, |
||
100 | group or role |
||
101 | |||
102 | Returns: |
||
103 | The response. See :py:meth:`send_command` for details. |
||
104 | """ |
||
105 | if not name: |
||
106 | raise RequiredArgument( |
||
107 | function=self.create_permission.__name__, argument='name' |
||
108 | ) |
||
109 | |||
110 | if not subject_id: |
||
111 | raise RequiredArgument( |
||
112 | function=self.create_permission.__name__, argument='subject_id' |
||
113 | ) |
||
114 | |||
115 | if not isinstance(subject_type, PermissionSubjectType): |
||
116 | raise InvalidArgumentType( |
||
117 | function=self.create_permission.__name__, |
||
118 | argument='subject_type', |
||
119 | arg_type=PermissionSubjectType.__name__, |
||
120 | ) |
||
121 | |||
122 | cmd = XmlCommand("create_permission") |
||
123 | cmd.add_element("name", name) |
||
124 | |||
125 | _xmlsubject = cmd.add_element("subject", attrs={"id": subject_id}) |
||
126 | _xmlsubject.add_element("type", subject_type.value) |
||
127 | |||
128 | if comment: |
||
129 | cmd.add_element("comment", comment) |
||
130 | |||
131 | View Code Duplication | if resource_id or resource_type: |
|
|
|||
132 | if not resource_id: |
||
133 | raise RequiredArgument( |
||
134 | function=self.create_permission.__name__, |
||
135 | argument='resource_id', |
||
136 | ) |
||
137 | |||
138 | if not resource_type: |
||
139 | raise RequiredArgument( |
||
140 | function=self.create_permission.__name__, |
||
141 | argument='resource_type', |
||
142 | ) |
||
143 | |||
144 | if not isinstance(resource_type, EntityType): |
||
145 | raise InvalidArgumentType( |
||
146 | function=self.create_permission.__name__, |
||
147 | argument='resource_type', |
||
148 | arg_type=EntityType.__name__, |
||
149 | ) |
||
150 | |||
151 | _xmlresource = cmd.add_element( |
||
152 | "resource", attrs={"id": resource_id} |
||
153 | ) |
||
154 | |||
155 | _actual_resource_type = resource_type |
||
156 | if resource_type.value == EntityType.AUDIT.value: |
||
157 | _actual_resource_type = EntityType.TASK |
||
158 | elif resource_type.value == EntityType.POLICY.value: |
||
159 | _actual_resource_type = EntityType.SCAN_CONFIG |
||
160 | |||
161 | _xmlresource.add_element("type", _actual_resource_type.value) |
||
162 | |||
163 | return self._send_xml_command(cmd) |
||
164 | |||
320 |