Failed Conditions
Pull Request — master (#1382)
by Abdeali
01:46
created

bears.general.SpaceConsistencyBear.run()   F

Complexity

Conditions 11

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 11
dl 0
loc 65
rs 3.8571

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
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 5
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (86/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
6
7
class SpaceConsistencyBear(LocalBear):
8
9
    def run(self,
10
            filename,
11
            file,
12
            use_spaces: bool,
13
            allow_trailing_whitespace: bool=False,
14
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH,
15
            enforce_newline_at_EOF: bool=True):
16
        '''
17
        Checks the space consistency for each line.
18
19
        :param use_spaces:                True if spaces are to be used instead
20
                                          of tabs.
21
        :param allow_trailing_whitespace: Whether to allow trailing whitespace
22
                                          or not.
23
        :param tab_width:                 Number of spaces representing one
24
                                          tab.
25
        :param enforce_newline_at_EOF:    Whether to enforce a newline at the
26
                                          End Of File.
27
        '''
28
        spacing_helper = SpacingHelper(tab_width)
29
        result_texts = []
30
31
        for line_number, line in enumerate(file, start=1):
32
            replacement = line
33
34
            if enforce_newline_at_EOF:
35
                # Since every line contains at the end at least one \n, only
36
                # the last line could potentially not have one. So we don't
37
                # need to check whether the current line_number is the last
38
                # one.
39
                if replacement[-1] != "\n":
40
                    replacement += "\n"
41
                    result_texts.append("No newline at EOF.")
42
43
            if not allow_trailing_whitespace:
44
                replacement = replacement.rstrip(" \t\n") + "\n"
45
                if replacement != line.rstrip("\n") + "\n":
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