Completed
Pull Request — master (#1116)
by Lasse
01:39
created

bears.general.SpaceConsistencyBear.run()   F

Complexity

Conditions 11

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 11
dl 0
loc 66
rs 3.8028

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

Complex classes like bears.general.SpaceConsistencyBear.run() 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 coalib.bearlib.spacing.SpacingHelper import SpacingHelper
2
from coalib.results.Diff import Diff
3
from coalib.bears.LocalBear import LocalBear
4
from coalib.results.Result import Result
5
6
7
class SpaceConsistencyBear(LocalBear):
8
    def run(self,
9
            filename,
10
            file,
11
            use_spaces: bool,
12
            allow_trailing_whitespace: bool=False,
13
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH,
14
            enforce_newline_at_EOF: bool=True):
15
        '''
16
        Checks the space consistency for each line.
17
18
        :param use_spaces:                True if spaces are to be used instead
19
                                          of tabs.
20
        :param allow_trailing_whitespace: Whether to allow trailing whitespace
21
                                          or not.
22
        :param tab_width:                 Number of spaces representing one
23
                                          tab.
24
        :param enforce_newline_at_EOF:    Whether to enforce a newline at the
25
                                          End Of File.
26
        '''
27
        spacing_helper = SpacingHelper(tab_width)
28
        result_texts = []
29
30
        for line_number, line in enumerate(file, start=1):
31
            replacement = line
32
33
            if enforce_newline_at_EOF:
34
                # Since every line contains at the end at least one \n, only
35
                # the last line could potentially not have one. So we don't
36
                # need to check whether the current line_number is the last
37
                # one.
38
                if replacement[-1] != "\n":
39
                    replacement += "\n"
40
                    result_texts.append("No newline at EOF.")
41
42
            if not allow_trailing_whitespace:
43
                pre_replacement = line
44
                replacement = replacement.rstrip(" \t\n") + "\n"
45
                if replacement != pre_replacement:
46
                    result_texts.append("Trailing whitespaces.")
47
48
            if use_spaces:
49
                pre_replacement = replacement
50
                replacement = spacing_helper.replace_tabs_with_spaces(
51
                    replacement)
52
                if replacement != pre_replacement:
53
                    result_texts.append("Tabs used instead of spaces.")
54
            else:
55
                pre_replacement = replacement
56
                replacement = spacing_helper.replace_spaces_with_tabs(
57
                    replacement)
58
                if replacement != pre_replacement:
59
                    result_texts.append("Spaces used instead of tabs.")
60
61
            if len(result_texts) > 0:
62
                diff = Diff(file)
63
                diff.change_line(line_number, line, replacement)
64
                inconsistencies = "".join("\n- " + string
65
                                          for string in result_texts)
66
                yield Result.from_values(
67
                    self,
68
                    "Line contains following spacing inconsistencies:"
69
                    + inconsistencies,
70
                    diffs={filename: diff},
71
                    file=filename,
72
                    line=line_number)
73
                result_texts = []
74