Failed Conditions
Pull Request — master (#1152)
by Lasse
03:36
created

bears.ruby.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.ruby.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
import re
2
3
from coalib.bearlib.abstractions.Lint import Lint
4
from coalib.bears.LocalBear import LocalBear
5
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
6
7
8
class RubyLintBear(LocalBear, Lint):
9
    executable = 'ruby'
10
    arguments = '-wc'
11
    output_regex = re.compile(
12
        r'(?P<file_name>.+?):(?P<line>\d+): (?P<message>'
13
        r'.*?(?P<severity>error|warning)[,:] [^\r\n]+)\r?\n'
14
        r'(?:^[^\r\n]+\r?\n^(?P<col>.*?)\^)?')
15
    use_stderr = True
16
    severity_map = {
17
        "warning": RESULT_SEVERITY.NORMAL,
18
        "error": RESULT_SEVERITY.MAJOR}
19
20
    def run(self, filename, file):
21
        '''
22
        Checks the code with `ruby -wc` on each file separately.
23
        '''
24
        return self.lint(filename)
25