Conditions | 10 |
Total Lines | 60 |
Code Lines | 34 |
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.tickets.TicketsMixin.modify_ticket() 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.
1 | # -*- coding: utf-8 -*- |
||
190 | def modify_ticket( |
||
191 | self, |
||
192 | ticket_id: str, |
||
193 | *, |
||
194 | status: Optional[TicketStatus] = None, |
||
195 | note: Optional[str] = None, |
||
196 | assigned_to_user_id: Optional[str] = None, |
||
197 | comment: Optional[str] = None, |
||
198 | ) -> Any: |
||
199 | """Modify a single ticket |
||
200 | |||
201 | Arguments: |
||
202 | ticket_id: UUID of an existing ticket |
||
203 | status: New status for the ticket |
||
204 | note: Note for the status change. Required if status is set. |
||
205 | assigned_to_user_id: UUID of the user the ticket should be assigned |
||
206 | to |
||
207 | comment: Comment for the ticket |
||
208 | |||
209 | Returns: |
||
210 | The response. See :py:meth:`send_command` for details. |
||
211 | """ |
||
212 | if not ticket_id: |
||
213 | raise RequiredArgument( |
||
214 | function=self.modify_ticket.__name__, argument='ticket_id' |
||
215 | ) |
||
216 | |||
217 | if status and not note: |
||
218 | raise RequiredArgument( |
||
219 | function=self.modify_ticket.__name__, argument='note' |
||
220 | ) |
||
221 | |||
222 | if note and not status: |
||
223 | raise RequiredArgument( |
||
224 | function=self.modify_ticket.__name__, argument='status' |
||
225 | ) |
||
226 | |||
227 | cmd = XmlCommand("modify_ticket") |
||
228 | cmd.set_attribute("ticket_id", ticket_id) |
||
229 | |||
230 | if assigned_to_user_id: |
||
231 | _assigned = cmd.add_element("assigned_to") |
||
232 | _user = _assigned.add_element("user") |
||
233 | _user.set_attribute("id", assigned_to_user_id) |
||
234 | |||
235 | if status: |
||
236 | if not isinstance(status, TicketStatus): |
||
237 | raise InvalidArgumentType( |
||
238 | function=self.modify_ticket.__name__, |
||
239 | argument='status', |
||
240 | arg_type=TicketStatus.__name__, |
||
241 | ) |
||
242 | |||
243 | cmd.add_element('status', status.value) |
||
244 | cmd.add_element('{}_note'.format(status.name.lower()), note) |
||
245 | |||
246 | if comment: |
||
247 | cmd.add_element("comment", comment) |
||
248 | |||
249 | return self._send_xml_command(cmd) |
||
250 |