Conditions | 11 |
Total Lines | 69 |
Code Lines | 40 |
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 db_sync_tool.utility.parser.get_database_configuration() 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 | #!/usr/bin/env python3 |
||
25 | def get_database_configuration(client): |
||
26 | """ |
||
27 | Getting database configuration of given client and defined sync base (framework type) |
||
28 | :param client: String |
||
29 | :return: |
||
30 | """ |
||
31 | system.config['db'] = {} |
||
32 | |||
33 | # check framework type |
||
34 | _base = '' |
||
35 | if 'type' in system.config['host']: |
||
36 | _type = system.config['host']['type'].lower() |
||
37 | if _type == 'typo3': |
||
38 | # TYPO3 sync base |
||
39 | _base = Framework.TYPO3 |
||
40 | elif _type == 'symfony': |
||
41 | # Symfony sync base |
||
42 | _base = Framework.SYMFONY |
||
43 | elif _type == 'drupal': |
||
44 | # Symfony sync base |
||
45 | _base = Framework.DRUPAL |
||
46 | elif _type == 'wordpress': |
||
47 | # Symfony sync base |
||
48 | _base = Framework.WORDPRESS |
||
49 | else: |
||
50 | sys.exit( |
||
51 | output.message( |
||
52 | output.Subject.ERROR, |
||
53 | f'Framework type not supported: {_type}', |
||
54 | False |
||
55 | ) |
||
56 | ) |
||
57 | else: |
||
58 | # Default is TYPO3 sync base |
||
59 | _base = Framework.TYPO3 |
||
60 | |||
61 | sys.path.append('../extension') |
||
62 | if _base == Framework.TYPO3: |
||
63 | # Import TYPO3 parser |
||
64 | from ..extension import typo3 |
||
65 | _parser = typo3 |
||
66 | |||
67 | elif _base == Framework.SYMFONY: |
||
68 | # Import Symfony parser |
||
69 | from ..extension import symfony |
||
70 | _parser = symfony |
||
71 | |||
72 | elif _base == Framework.DRUPAL: |
||
73 | # Import Symfony parser |
||
74 | from ..extension import drupal |
||
75 | _parser = drupal |
||
76 | |||
77 | elif _base == Framework.WORDPRESS: |
||
78 | # Import Symfony parser |
||
79 | from ..extension import wordpress |
||
80 | _parser = wordpress |
||
81 | |||
82 | if client == mode.Client.ORIGIN: |
||
83 | output.message( |
||
84 | output.Subject.INFO, |
||
85 | 'Sync base: ' + _base, |
||
86 | True |
||
87 | ) |
||
88 | |||
89 | load_parser_origin(_parser) |
||
90 | else: |
||
91 | load_parser_target(_parser) |
||
92 | |||
93 | validate_database_credentials(client) |
||
94 | |||
166 |