src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Complexity/ComplexityChecker.py

branch
eric7
changeset 11147
dee6e106b4d3
parent 11145
d328a7b74fd8
child 11150
73d80859079c
equal deleted inserted replaced
11146:59e04f7003e9 11147:dee6e106b4d3
17 """ 17 """
18 Class implementing a checker for code complexity. 18 Class implementing a checker for code complexity.
19 """ 19 """
20 20
21 Codes = [ 21 Codes = [
22 "C101", 22 "C-101",
23 "C111", 23 "C-111",
24 "C112", 24 "C-112",
25 ] 25 ]
26 26
27 def __init__(self, source, filename, tree, select, ignore, args): 27 def __init__(self, source, filename, tree, select, ignore, args):
28 """ 28 """
29 Constructor 29 Constructor
59 59
60 # collection of detected errors 60 # collection of detected errors
61 self.errors = [] 61 self.errors = []
62 62
63 checkersWithCodes = [ 63 checkersWithCodes = [
64 (self.__checkMcCabeComplexity, ("C101",)), 64 (self.__checkMcCabeComplexity, ("C-101",)),
65 (self.__checkLineComplexity, ("C111", "C112")), 65 (self.__checkLineComplexity, ("C-111", "C-112")),
66 ] 66 ]
67 67
68 self.__checkers = [] 68 self.__checkers = []
69 for checker, codes in checkersWithCodes: 69 for checker, codes in checkersWithCodes:
70 if any(not (code and self.__ignoreCode(code)) for code in codes): 70 if any(not (code and self.__ignoreCode(code)) for code in codes):
150 150
151 visitor = PathGraphingAstVisitor() 151 visitor = PathGraphingAstVisitor()
152 visitor.preorder(tree, visitor) 152 visitor.preorder(tree, visitor)
153 for graph in visitor.graphs.values(): 153 for graph in visitor.graphs.values():
154 if graph.complexity() > maxComplexity: 154 if graph.complexity() > maxComplexity:
155 self.__error(graph.lineno, 0, "C101", graph.entity, graph.complexity()) 155 self.__error(graph.lineno, 0, "C-101", graph.entity, graph.complexity())
156 156
157 def __checkLineComplexity(self): 157 def __checkLineComplexity(self):
158 """ 158 """
159 Private method to check the complexity of a single line of code and 159 Private method to check the complexity of a single line of code and
160 the median line complexity of the source code. 160 the median line complexity of the source code.
175 sortedItems = visitor.sortedList() 175 sortedItems = visitor.sortedList()
176 score = visitor.score() 176 score = visitor.score()
177 177
178 for line, complexity in sortedItems: 178 for line, complexity in sortedItems:
179 if complexity > maxLineComplexity: 179 if complexity > maxLineComplexity:
180 self.__error(line, 0, "C111", complexity) 180 self.__error(line, 0, "C-111", complexity)
181 181
182 if score > maxLineComplexityScore: 182 if score > maxLineComplexityScore:
183 self.__error(0, 0, "C112", score) 183 self.__error(0, 0, "C-112", score)
184 184
185 185
186 class LineComplexityVisitor(ast.NodeVisitor): 186 class LineComplexityVisitor(ast.NodeVisitor):
187 """ 187 """
188 Class calculating the number of AST nodes per line of code 188 Class calculating the number of AST nodes per line of code

eric ide

mercurial