|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2017 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.python.checks; |
| 21 | + |
| 22 | +import com.sonar.sslr.api.AstNode; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Set; |
| 25 | +import org.sonar.check.Priority; |
| 26 | +import org.sonar.check.Rule; |
| 27 | +import org.sonar.check.RuleProperty; |
| 28 | +import org.sonar.python.PythonCheck; |
| 29 | +import org.sonar.python.api.PythonGrammar; |
| 30 | +import org.sonar.python.api.PythonKeyword; |
| 31 | +import org.sonar.python.api.PythonPunctuator; |
| 32 | +import org.sonar.squidbridge.annotations.ActivatedByDefault; |
| 33 | +import org.sonar.squidbridge.annotations.SqaleLinearWithOffsetRemediation; |
| 34 | + |
| 35 | +@Rule( |
| 36 | + key = CognitiveComplexityFunctionCheck.CHECK_KEY, |
| 37 | + name = "Cognitive Complexity of functions should not be too high", |
| 38 | + priority = Priority.CRITICAL, |
| 39 | + tags = Tags.BRAIN_OVERLOAD |
| 40 | +) |
| 41 | +@ActivatedByDefault |
| 42 | +@SqaleLinearWithOffsetRemediation( |
| 43 | + coeff = "1min", |
| 44 | + offset = "5min", |
| 45 | + effortToFixDescription = "per complexity point above the threshold") |
| 46 | +public class CognitiveComplexityFunctionCheck extends PythonCheck { |
| 47 | + |
| 48 | + private static final String MESSAGE = "Refactor this method to reduce its Cognitive Complexity from %s to the %s allowed."; |
| 49 | + public static final String CHECK_KEY = "S3776"; |
| 50 | + private static final int DEFAULT_THRESHOLD = 15; |
| 51 | + |
| 52 | + private AstNode currentFunction = null; |
| 53 | + private int complexity; |
| 54 | + private int nestingLevel; |
| 55 | + private Set<IssueLocation> secondaryLocations = new HashSet<>(); |
| 56 | + |
| 57 | + @RuleProperty( |
| 58 | + key = "threshold", |
| 59 | + description = "The maximum authorized complexity.", |
| 60 | + defaultValue = "" + DEFAULT_THRESHOLD) |
| 61 | + private int threshold = DEFAULT_THRESHOLD; |
| 62 | + |
| 63 | + public void setThreshold(int threshold) { |
| 64 | + this.threshold = threshold; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void init() { |
| 69 | + subscribeTo( |
| 70 | + PythonGrammar.IF_STMT, |
| 71 | + PythonKeyword.ELIF, |
| 72 | + PythonKeyword.ELSE, |
| 73 | + |
| 74 | + PythonGrammar.WHILE_STMT, |
| 75 | + PythonGrammar.FOR_STMT, |
| 76 | + PythonGrammar.EXCEPT_CLAUSE, |
| 77 | + |
| 78 | + PythonGrammar.AND_TEST, |
| 79 | + PythonGrammar.OR_TEST, |
| 80 | + |
| 81 | + PythonGrammar.TEST, |
| 82 | + |
| 83 | + PythonGrammar.FUNCDEF, |
| 84 | + PythonGrammar.SUITE); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void visitNode(AstNode astNode) { |
| 89 | + if (astNode.is(PythonGrammar.FUNCDEF) && currentFunction == null) { |
| 90 | + currentFunction = astNode; |
| 91 | + complexity = 0; |
| 92 | + nestingLevel = 0; |
| 93 | + secondaryLocations.clear(); |
| 94 | + } |
| 95 | + |
| 96 | + if (currentFunction != null) { |
| 97 | + if (astNode.is(PythonGrammar.SUITE) && incrementsNestingLevel(astNode)) { |
| 98 | + nestingLevel++; |
| 99 | + } |
| 100 | + |
| 101 | + checkComplexity(astNode); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void checkComplexity(AstNode astNode) { |
| 106 | + if (astNode.is(PythonGrammar.IF_STMT, PythonGrammar.WHILE_STMT, PythonGrammar.FOR_STMT, PythonGrammar.EXCEPT_CLAUSE)) { |
| 107 | + incrementWithNesting(astNode.getFirstChild()); |
| 108 | + } |
| 109 | + |
| 110 | + if (astNode.is(PythonKeyword.ELIF) || (astNode.is(PythonKeyword.ELSE) && astNode.getNextSibling().is(PythonPunctuator.COLON))) { |
| 111 | + incrementWithoutNesting(astNode); |
| 112 | + } |
| 113 | + |
| 114 | + if (astNode.is(PythonGrammar.AND_TEST, PythonGrammar.OR_TEST)) { |
| 115 | + incrementWithoutNesting(astNode.getFirstChild(PythonKeyword.AND, PythonKeyword.OR)); |
| 116 | + } |
| 117 | + |
| 118 | + // conditional expression |
| 119 | + if (astNode.is(PythonGrammar.TEST) && astNode.hasDirectChildren(PythonKeyword.IF)) { |
| 120 | + incrementWithNesting(astNode.getFirstChild(PythonKeyword.IF)); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void leaveNode(AstNode astNode) { |
| 126 | + if (currentFunction == null) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + if (currentFunction.equals(astNode)) { |
| 131 | + if (complexity > threshold){ |
| 132 | + raiseIssue(); |
| 133 | + } |
| 134 | + currentFunction = null; |
| 135 | + } |
| 136 | + |
| 137 | + if (astNode.is(PythonGrammar.SUITE) && incrementsNestingLevel(astNode)) { |
| 138 | + nestingLevel--; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void raiseIssue() { |
| 143 | + String message = String.format(MESSAGE, complexity, threshold); |
| 144 | + PreciseIssue issue = addIssue(currentFunction.getFirstChild(PythonGrammar.FUNCNAME), message) |
| 145 | + .withCost(complexity - threshold); |
| 146 | + secondaryLocations.forEach(issue::secondary); |
| 147 | + } |
| 148 | + |
| 149 | + private boolean incrementsNestingLevel(AstNode astNode) { |
| 150 | + AstNode previousSibling = astNode.getPreviousSibling().getPreviousSibling(); |
| 151 | + if (previousSibling.is(PythonKeyword.TRY, PythonKeyword.FINALLY)) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + AstNode parent = astNode.getParent(); |
| 156 | + |
| 157 | + return !parent.is(PythonGrammar.WITH_STMT, PythonGrammar.CLASSDEF) |
| 158 | + && (!parent.is(PythonGrammar.FUNCDEF) || !parent.equals(currentFunction)); |
| 159 | + } |
| 160 | + |
| 161 | + private void incrementWithNesting(AstNode secondaryLocationNode) { |
| 162 | + int currentNodeComplexity = nestingLevel + 1; |
| 163 | + incrementComplexity(secondaryLocationNode, currentNodeComplexity); |
| 164 | + } |
| 165 | + |
| 166 | + private void incrementWithoutNesting(AstNode secondaryLocationNode) { |
| 167 | + incrementComplexity(secondaryLocationNode, 1); |
| 168 | + } |
| 169 | + |
| 170 | + private void incrementComplexity(AstNode secondaryLocationNode, int currentNodeComplexity) { |
| 171 | + secondaryLocations.add(new IssueLocation(secondaryLocationNode, secondaryMessage(currentNodeComplexity))); |
| 172 | + complexity += currentNodeComplexity; |
| 173 | + } |
| 174 | + |
| 175 | + private static String secondaryMessage(int complexity) { |
| 176 | + if (complexity == 1) { |
| 177 | + return "+1"; |
| 178 | + |
| 179 | + } else{ |
| 180 | + return String.format("+%s (incl %s for nesting)", complexity, complexity - 1); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments