| Conditions | 31 |
| Total Lines | 101 |
| 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 Runner.process_results() 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 -*- |
||
| 154 | def process_results(self, all_results): |
||
| 155 | """Group all results by file path.""" |
||
| 156 | all_changed_paths = [] |
||
| 157 | for tool_name, data in all_results.items(): |
||
| 158 | if data: |
||
| 159 | files, results = data['files'], data['results'] |
||
| 160 | all_changed_paths += [result['path'] for result in results] |
||
| 161 | |||
| 162 | all_changed_paths = list(sorted(set(all_changed_paths))) |
||
| 163 | |||
| 164 | if self.test_results: |
||
| 165 | test_files = self.test_results.get('files') |
||
| 166 | test_coverage = self.test_results.get('coverage') |
||
| 167 | else: |
||
| 168 | test_files = [] |
||
| 169 | test_coverage = [] |
||
| 170 | |||
| 171 | for path in all_changed_paths: |
||
| 172 | short_path = path.replace(self.cmd_root, '...') |
||
| 173 | print('') |
||
| 174 | print(short_path) |
||
| 175 | print('-' * len(short_path)) |
||
| 176 | for tool_name, data in all_results.items(): |
||
| 177 | if data: |
||
| 178 | files, results = data['files'], data['results'] |
||
| 179 | lines = [[-1], range(100000)] |
||
| 180 | |||
| 181 | if isinstance(files, dict): |
||
| 182 | added_lines = files.get(path, lines)[-1] |
||
| 183 | else: |
||
| 184 | added_lines = lines[-1] |
||
| 185 | |||
| 186 | messages = [] |
||
| 187 | for result in results: |
||
| 188 | res_path = result['path'] |
||
| 189 | if path == res_path: |
||
| 190 | # LINTERS |
||
| 191 | line = int(result.get('line', -1)) |
||
| 192 | created = result.get('created') |
||
| 193 | added_copy = result.get('added-copy') |
||
| 194 | added_header = result.get('added-header') |
||
| 195 | diff = result.get('diff') |
||
| 196 | if line and line in list(added_lines): |
||
| 197 | spaces = (8 - len(str(line))) * ' ' |
||
| 198 | args = result.copy() |
||
| 199 | args['spaces'] = spaces |
||
| 200 | msg = (' {line}:{spaces}' |
||
| 201 | '{type}: {message}').format(**args) |
||
| 202 | messages.append(msg) |
||
| 203 | |||
| 204 | # FORMATERS |
||
| 205 | if created: |
||
| 206 | msg = ' __init__ file created.' |
||
| 207 | messages.append(msg) |
||
| 208 | if added_copy: |
||
| 209 | msg = ' added copyright.' |
||
| 210 | messages.append(msg) |
||
| 211 | if added_header: |
||
| 212 | msg = ' added header.' |
||
| 213 | messages.append(msg) |
||
| 214 | if diff: |
||
| 215 | msg = self.format_diff(diff) |
||
| 216 | messages.append(msg) |
||
| 217 | |||
| 218 | # TESTERS / COVERAGE |
||
| 219 | |||
| 220 | test = [r['path'] for r in results if path == r['path']] |
||
| 221 | if test and messages: |
||
| 222 | print('\n ' + tool_name) |
||
| 223 | print(' ' + '-' * len(tool_name)) |
||
| 224 | self.failed_checks.add(tool_name) |
||
| 225 | for message in messages: |
||
| 226 | print(message) |
||
| 227 | |||
| 228 | if isinstance(test_files, dict) and test_files: |
||
| 229 | # Asked for lines changed |
||
| 230 | if test_coverage: |
||
| 231 | lines_changed_not_covered = [] |
||
| 232 | lines = test_files.get(path) |
||
| 233 | lines_added = lines[-1] if lines else [] |
||
| 234 | lines_covered = test_coverage.get(path) |
||
| 235 | for line in lines_added: |
||
| 236 | if line not in lines_covered: |
||
| 237 | lines_changed_not_covered.append(str(line)) |
||
| 238 | |||
| 239 | if lines_changed_not_covered: |
||
| 240 | uncov_perc = ((1.0 * len(lines_changed_not_covered)) / |
||
| 241 | (1.0 * len(lines_added))) |
||
| 242 | cov_perc = (1 - uncov_perc) * 100 |
||
| 243 | tool_name = 'coverage' |
||
| 244 | print('\n ' + tool_name) |
||
| 245 | print(' ' + '-' * len(tool_name)) |
||
| 246 | print(' The following lines changed and are not ' |
||
| 247 | 'covered by tests ({0}%):'.format(cov_perc)) |
||
| 248 | print(' ' + ', '.join(lines_changed_not_covered)) |
||
| 249 | |||
| 250 | print('') |
||
| 251 | pytest_tool = self.all_tools.get('pytest') |
||
| 252 | if pytest_tool: |
||
| 253 | if pytest_tool.coverage_fail: |
||
| 254 | self.failed_checks.add('coverage') |
||
| 255 | |||
| 396 |