Conditions | 10 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 110 |
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 ActionManager.queue() 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 | from plugin.core.helpers.thread import module |
||
27 | @classmethod |
||
28 | def queue(cls, event, request, session=None, account=None): |
||
29 | if event is None: |
||
30 | return None |
||
31 | |||
32 | obj = None |
||
33 | |||
34 | if request is not None: |
||
35 | request = json.dumps(request) |
||
36 | |||
37 | # Retrieve `account_id` for action |
||
38 | account_id = None |
||
39 | |||
40 | if session: |
||
41 | try: |
||
42 | account_id = session.account_id |
||
43 | except KeyError: |
||
44 | account_id = None |
||
45 | |||
46 | if account_id is None and account: |
||
47 | account_id = account.id |
||
48 | |||
49 | if account_id is None: |
||
50 | log.debug('Unable to find valid account for event %r, session %r', event, session) |
||
51 | return None |
||
52 | |||
53 | if not Preferences.get('scrobble.enabled', account_id): |
||
54 | log.debug('Scrobbler not enabled for account %r', account_id) |
||
55 | return None |
||
56 | |||
57 | # Try queue the event |
||
58 | try: |
||
59 | obj = ActionQueue.create( |
||
60 | account=account_id, |
||
61 | session=session, |
||
62 | |||
63 | progress=session.progress, |
||
64 | rating_key=session.rating_key, |
||
65 | |||
66 | event=event, |
||
67 | request=request, |
||
68 | |||
69 | queued_at=datetime.utcnow() |
||
70 | ) |
||
71 | log.debug('Queued %r event for %r', event, session) |
||
72 | except (apsw.ConstraintError, peewee.IntegrityError), ex: |
||
73 | log.warn('Unable to queue event %r for %r: %s', event, session, ex, exc_info=True) |
||
74 | |||
75 | # Ensure process thread is started |
||
76 | cls.start() |
||
77 | |||
78 | return obj |
||
79 | |||
220 |