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 |