Conditions | 13 |
Total Lines | 51 |
Code Lines | 35 |
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 satdigitalinvoice.client_validation.validar_client() 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 | import logging |
||
17 | def validar_client(client): |
||
18 | errors = [] |
||
19 | |||
20 | rfc = client['Rfc'] |
||
21 | |||
22 | def error(msg): |
||
23 | errors.append(f"{rfc}: {msg}") |
||
24 | |||
25 | try: |
||
26 | rfc = RFC(rfc) |
||
27 | |||
28 | for email in client["Email"]: |
||
29 | if not re.fullmatch(EMAIL_REGEX, email): |
||
30 | error(f"Correo '{email}' is invalid") |
||
31 | |||
32 | res = csf.retrieve(rfc, id_cif=client["IdCIF"]) |
||
33 | |||
34 | if rfc.type == RFCType.FISICA: |
||
35 | razon_social = f"{res['Nombre']} {res['Apellido Paterno']} {res['Apellido Materno']}" |
||
36 | elif 'Denominación o Razón Social' in res: |
||
37 | razon_social = res['Denominación o Razón Social'] |
||
38 | else: |
||
39 | error(f"Does not have 'Denominación o Razón Social'") |
||
40 | |||
41 | if client['RazonSocial'] != razon_social: |
||
|
|||
42 | error(f"RazonSocial '{client['RazonSocial']}' is invalid, expected '{razon_social}'") |
||
43 | |||
44 | if client['CodigoPostal'] != res['CP']: |
||
45 | error(f"CodigoPostal '{client['CodigoPostal']}' is invalid, expected '{res['CP']}'") |
||
46 | |||
47 | for r in res['Regimenes']: |
||
48 | if r['RegimenFiscal'].code == None: |
||
49 | error(f"RegimenFiscal '{r['RegimenFiscal']}' is invalid") |
||
50 | |||
51 | if client['RegimenFiscal'] not in (r['RegimenFiscal'] for r in res['Regimenes']): |
||
52 | regimen = ', '.join(r['RegimenFiscal'].code or "" for r in res['Regimenes']) |
||
53 | error( |
||
54 | f"RegimenFiscal '{client['RegimenFiscal']}' is invalid, " |
||
55 | f"expected one of '{regimen}'" |
||
56 | ) |
||
57 | |||
58 | if res['Situación del contribuyente'] not in ('ACTIVO', 'REACTIVADO'): |
||
59 | error(f"Is not ACTIVO '{res['Situación del contribuyente']}'") |
||
60 | |||
61 | taxpayer_status = sat_service.list_69b(rfc) |
||
62 | if taxpayer_status: |
||
63 | error(f"has status '{taxpayer_status}'") |
||
64 | except Exception as ex: |
||
65 | error(ex) |
||
66 | |||
67 | return errors |
||
68 | |||
74 |