3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.issue.commonrule;
22 import java.util.Optional;
23 import org.sonar.api.measures.CoreMetrics;
24 import org.sonar.ce.task.projectanalysis.component.Component;
25 import org.sonar.ce.task.projectanalysis.measure.Measure;
26 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
27 import org.sonar.ce.task.projectanalysis.metric.Metric;
28 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
29 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
30 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolder;
31 import org.sonar.server.rule.CommonRuleKeys;
33 import static java.lang.String.format;
35 public class CommentDensityRule extends CommonRule {
37 private final MeasureRepository measureRepository;
38 private final Metric commentDensityMetric;
39 private final Metric commentLinesMetric;
40 private final Metric nclocMetric;
42 public CommentDensityRule(ActiveRulesHolder activeRulesHolder, MeasureRepository measureRepository, MetricRepository metricRepository) {
43 super(activeRulesHolder, CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY);
44 this.measureRepository = measureRepository;
45 this.commentDensityMetric = metricRepository.getByKey(CoreMetrics.COMMENT_LINES_DENSITY_KEY);
46 this.commentLinesMetric = metricRepository.getByKey(CoreMetrics.COMMENT_LINES_KEY);
47 this.nclocMetric = metricRepository.getByKey(CoreMetrics.NCLOC_KEY);
51 protected CommonRuleIssue doProcessFile(Component file, ActiveRule activeRule) {
52 Optional<Measure> commentDensityMeasure = measureRepository.getRawMeasure(file, commentDensityMetric);
53 Optional<Measure> commentLinesMeasure = measureRepository.getRawMeasure(file, commentLinesMetric);
54 Optional<Measure> nclocMeasure = measureRepository.getRawMeasure(file, nclocMetric);
56 if (!file.getFileAttributes().isUnitTest() && commentDensityMeasure.isPresent() && nclocMeasure.isPresent() && nclocMeasure.get().getIntValue() > 0) {
57 // this is a small optimization to not load the minimum value when the measures are not present
58 double minCommentDensity = getMinDensity(activeRule);
59 if (commentDensityMeasure.get().getDoubleValue() < minCommentDensity) {
60 return generateIssue(commentDensityMeasure.get(), commentLinesMeasure, nclocMeasure.get(), minCommentDensity);
66 private static double getMinDensity(ActiveRule activeRule) {
67 double min = getMinDensityParam(activeRule, CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY);
69 throw new IllegalStateException("Minimum density of rule [" + activeRule.getRuleKey() + "] is incorrect. Got [100] but must be strictly less than 100.");
74 private static CommonRuleIssue generateIssue(Measure commentDensityMeasure, Optional<Measure> commentLinesMeasure,
75 Measure nclocMeasure, double minCommentDensity) {
76 int commentLines = commentLinesMeasure.map(Measure::getIntValue).orElse(0);
77 int ncloc = nclocMeasure.getIntValue();
78 int minExpectedCommentLines = (int) Math.ceil(minCommentDensity * ncloc / (100 - minCommentDensity));
79 int missingCommentLines = minExpectedCommentLines - commentLines;
80 if (missingCommentLines <= 0) {
81 throw new IllegalStateException(format("Bug in measures of comment lines - density=%s, comment_lines= %d, ncloc=%d, threshold=%s%%",
82 commentDensityMeasure.getDoubleValue(), commentLines, nclocMeasure.getIntValue(), minCommentDensity));
85 // TODO declare min threshold as int but not float ?
86 String message = format("%d more comment lines need to be written to reach the minimum threshold of %s%% comment density.", missingCommentLines, minCommentDensity);
87 return new CommonRuleIssue(missingCommentLines, message);