Conditions | 7 |
Total Lines | 77 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Tests | 27 |
CRAP Score | 7.2944 |
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:
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 | """ |
||
34 | 1 | def create_user( |
|
35 | screen_name: Optional[str], |
||
36 | email_address: Optional[str], |
||
37 | password: str, |
||
38 | *, |
||
39 | legacy_id: Optional[str] = None, |
||
40 | first_names: Optional[str] = None, |
||
41 | last_name: Optional[str] = None, |
||
42 | date_of_birth: Optional[date] = None, |
||
43 | country: Optional[str] = None, |
||
44 | zip_code: Optional[str] = None, |
||
45 | city: Optional[str] = None, |
||
46 | street: Optional[str] = None, |
||
47 | phone_number: Optional[str] = None, |
||
48 | internal_comment: Optional[str] = None, |
||
49 | extras: Optional[dict[str, Any]] = None, |
||
50 | creator_id: Optional[UserID] = None, |
||
51 | site_id: Optional[SiteID] = None, |
||
52 | ) -> tuple[User, UserAccountCreated]: |
||
53 | """Create a user account and related records.""" |
||
54 | creator: Optional[User] |
||
55 | 1 | if creator_id is not None: |
|
56 | creator = user_service.get_user(creator_id) |
||
57 | else: |
||
58 | 1 | creator = None |
|
59 | |||
60 | 1 | created_at = datetime.utcnow() |
|
61 | |||
62 | 1 | db_user = build_db_user( |
|
63 | created_at, screen_name, email_address, legacy_id=legacy_id |
||
64 | ) |
||
65 | |||
66 | 1 | db_user.detail.first_names = first_names |
|
67 | 1 | db_user.detail.last_name = last_name |
|
68 | 1 | db_user.detail.date_of_birth = date_of_birth |
|
69 | 1 | db_user.detail.country = country |
|
70 | 1 | db_user.detail.zip_code = zip_code |
|
71 | 1 | db_user.detail.city = city |
|
72 | 1 | db_user.detail.street = street |
|
73 | 1 | db_user.detail.phone_number = phone_number |
|
74 | 1 | db_user.detail.internal_comment = internal_comment |
|
75 | 1 | db_user.detail.extras = extras |
|
76 | |||
77 | 1 | db.session.add(db_user) |
|
78 | |||
79 | 1 | try: |
|
80 | 1 | db.session.commit() |
|
81 | except Exception as e: |
||
82 | current_app.logger.error('User creation failed: %s', e) |
||
83 | db.session.rollback() |
||
84 | raise UserCreationFailed() |
||
85 | |||
86 | 1 | user = user_service._db_entity_to_user(db_user) |
|
87 | |||
88 | # Create event in separate step as user ID is not available earlier. |
||
89 | 1 | event_data = {} |
|
90 | 1 | if creator is not None: |
|
91 | event_data['initiator_id'] = str(creator.id) |
||
92 | 1 | if site_id is not None: |
|
93 | 1 | event_data['site_id'] = site_id |
|
94 | 1 | event_service.create_event( |
|
95 | 'user-created', user.id, event_data, occurred_at=created_at |
||
96 | ) |
||
97 | |||
98 | 1 | event = UserAccountCreated( |
|
99 | occurred_at=db_user.created_at, |
||
100 | initiator_id=creator.id if creator else None, |
||
101 | initiator_screen_name=creator.screen_name if creator else None, |
||
102 | user_id=user.id, |
||
103 | user_screen_name=user.screen_name, |
||
104 | site_id=site_id, |
||
105 | ) |
||
106 | |||
107 | # password |
||
108 | 1 | password_service.create_password_hash(user.id, password) |
|
109 | |||
110 | 1 | return user, event |
|
111 | |||
176 |