Skip to content

Commit

Permalink
Merge pull request #1530 from ivangalkin/lgtm2
Browse files Browse the repository at this point in the history
fix quality flaws (2)
  • Loading branch information
guwirth authored Aug 3, 2018
2 parents 2de102d + fd46324 commit f1ae36d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,10 @@ protected void onPublicApi(AstNode node, String id, List<Token> comments) {
boolean commented = !comments.isEmpty();

if (LOG.isDebugEnabled()) {
LOG.debug("node: {} line: {} id: '{}' documented: {}",
new Object[]{node.getType(), node.getTokenLine(), id, commented});
LOG.debug("node: {} line: {} id: '{}' documented: {}", node.getType(), node.getTokenLine(), id, commented);
}
if (!commented) {
getContext().createLineViolation(this, "Undocumented API: " + id,
node);
getContext().createLineViolation(this, "Undocumented API: " + id, node);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ public String[] getAggregatedMetrics() {
return metricKeys.clone();
}

private void compute(MeasureComputerContext context, String metricKey) {
private static void compute(MeasureComputerContext context, String metricKey) {
final Component component = context.getComponent();
if (component.getType() == Component.Type.FILE) {
LOG.debug("Component {}: FILE doesn't required an aggregation", component.getKey());
return;
}
if (context.getMeasure(metricKey) != null) {
final Measure existingMeasure = context.getMeasure(metricKey);
if (existingMeasure != null) {
LOG.debug("Component {}: measure {} already calculated, value = {}", component.getKey(), metricKey,
context.getMeasure(metricKey).getIntValue());
existingMeasure.getIntValue());
return;
}
Iterable<Measure> childrenMeasures = context.getChildrenMeasures(metricKey);
Expand Down
80 changes: 42 additions & 38 deletions cxx-squid/src/main/java/org/sonar/cxx/DensityMeasureComputer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,48 +50,48 @@ public class DensityMeasureComputer implements MeasureComputer {

private static final Logger LOG = Loggers.get(DensityMeasureComputer.class);

private final String PublicAPIKey;
private final String PublicUndocumentedAPIKey;
private final String PublicDocumentedAPIDensityKey;
private final String publicAPIKey;
private final String publicUndocumentedAPIKey;
private final String publicDocumentedAPIDensityKey;

private final String ComplexFunctionsKey;
private final String ComplexFunctionsPercKey;
private final String ComplexFunctionsLocKey;
private final String ComplexFunctionsLocPercKey;
private final String complexFunctionsKey;
private final String complexFunctionsPercKey;
private final String complexFunctionsLocKey;
private final String complexFunctionsLocPercKey;

private final String BigFunctionsKey;
private final String BigFunctionsPercKey;
private final String BigFunctionsLocKey;
private final String BigFunctionsLocPercKey;
private final String bigFunctionsKey;
private final String bigFunctionsPercKey;
private final String bigFunctionsLocKey;
private final String bigFunctionsLocPercKey;

private final String LocInFunctionsKey;
private final String locInFunctionsKey;

private final String[] inputMetrics;
private final String[] outputMetrics;

public DensityMeasureComputer(String languageKey, String languagePropsKey) {
final Map<CxxMetricsFactory.Key, Metric<?>> metrics = CxxMetricsFactory.generateMap(languageKey, languagePropsKey);

PublicAPIKey = metrics.get(CxxMetricsFactory.Key.PUBLIC_API_KEY).key();
PublicUndocumentedAPIKey = metrics.get(CxxMetricsFactory.Key.PUBLIC_UNDOCUMENTED_API_KEY).key();
PublicDocumentedAPIDensityKey = metrics.get(CxxMetricsFactory.Key.PUBLIC_DOCUMENTED_API_DENSITY_KEY).key();
publicAPIKey = metrics.get(CxxMetricsFactory.Key.PUBLIC_API_KEY).key();
publicUndocumentedAPIKey = metrics.get(CxxMetricsFactory.Key.PUBLIC_UNDOCUMENTED_API_KEY).key();
publicDocumentedAPIDensityKey = metrics.get(CxxMetricsFactory.Key.PUBLIC_DOCUMENTED_API_DENSITY_KEY).key();

ComplexFunctionsKey = metrics.get(CxxMetricsFactory.Key.COMPLEX_FUNCTIONS_KEY).key();
ComplexFunctionsPercKey = metrics.get(CxxMetricsFactory.Key.COMPLEX_FUNCTIONS_PERC_KEY).key();
ComplexFunctionsLocKey = metrics.get(CxxMetricsFactory.Key.COMPLEX_FUNCTIONS_LOC_KEY).key();
ComplexFunctionsLocPercKey = metrics.get(CxxMetricsFactory.Key.COMPLEX_FUNCTIONS_LOC_PERC_KEY).key();
complexFunctionsKey = metrics.get(CxxMetricsFactory.Key.COMPLEX_FUNCTIONS_KEY).key();
complexFunctionsPercKey = metrics.get(CxxMetricsFactory.Key.COMPLEX_FUNCTIONS_PERC_KEY).key();
complexFunctionsLocKey = metrics.get(CxxMetricsFactory.Key.COMPLEX_FUNCTIONS_LOC_KEY).key();
complexFunctionsLocPercKey = metrics.get(CxxMetricsFactory.Key.COMPLEX_FUNCTIONS_LOC_PERC_KEY).key();

BigFunctionsKey = metrics.get(CxxMetricsFactory.Key.BIG_FUNCTIONS_KEY).key();
BigFunctionsPercKey = metrics.get(CxxMetricsFactory.Key.BIG_FUNCTIONS_PERC_KEY).key();
BigFunctionsLocKey = metrics.get(CxxMetricsFactory.Key.BIG_FUNCTIONS_LOC_KEY).key();
BigFunctionsLocPercKey = metrics.get(CxxMetricsFactory.Key.BIG_FUNCTIONS_LOC_PERC_KEY).key();
bigFunctionsKey = metrics.get(CxxMetricsFactory.Key.BIG_FUNCTIONS_KEY).key();
bigFunctionsPercKey = metrics.get(CxxMetricsFactory.Key.BIG_FUNCTIONS_PERC_KEY).key();
bigFunctionsLocKey = metrics.get(CxxMetricsFactory.Key.BIG_FUNCTIONS_LOC_KEY).key();
bigFunctionsLocPercKey = metrics.get(CxxMetricsFactory.Key.BIG_FUNCTIONS_LOC_PERC_KEY).key();

LocInFunctionsKey = metrics.get(CxxMetricsFactory.Key.LOC_IN_FUNCTIONS_KEY).key();
locInFunctionsKey = metrics.get(CxxMetricsFactory.Key.LOC_IN_FUNCTIONS_KEY).key();

inputMetrics = new String[] { PublicAPIKey, PublicUndocumentedAPIKey, CoreMetrics.FUNCTIONS_KEY, LocInFunctionsKey,
ComplexFunctionsKey, ComplexFunctionsLocKey, BigFunctionsKey, BigFunctionsLocKey };
outputMetrics = new String[] { PublicDocumentedAPIDensityKey, ComplexFunctionsPercKey, ComplexFunctionsLocPercKey,
BigFunctionsPercKey, BigFunctionsLocPercKey };
inputMetrics = new String[] { publicAPIKey, publicUndocumentedAPIKey, CoreMetrics.FUNCTIONS_KEY, locInFunctionsKey,
complexFunctionsKey, complexFunctionsLocKey, bigFunctionsKey, bigFunctionsLocKey };
outputMetrics = new String[] { publicDocumentedAPIDensityKey, complexFunctionsPercKey, complexFunctionsLocPercKey,
bigFunctionsPercKey, bigFunctionsLocPercKey };
}

public String[] getInputMetrics() {
Expand All @@ -109,14 +109,14 @@ public MeasureComputerDefinition define(MeasureComputerDefinitionContext defCont

@Override
public void compute(MeasureComputerContext context) {
compute(context, PublicUndocumentedAPIKey, PublicAPIKey, PublicDocumentedAPIDensityKey, true);
compute(context, ComplexFunctionsKey, CoreMetrics.FUNCTIONS_KEY, ComplexFunctionsPercKey, false);
compute(context, ComplexFunctionsLocKey, LocInFunctionsKey, ComplexFunctionsLocPercKey, false);
compute(context, BigFunctionsKey, CoreMetrics.FUNCTIONS_KEY, BigFunctionsPercKey, false);
compute(context, BigFunctionsLocKey, LocInFunctionsKey, BigFunctionsLocPercKey, false);
compute(context, publicUndocumentedAPIKey, publicAPIKey, publicDocumentedAPIDensityKey, true);
compute(context, complexFunctionsKey, CoreMetrics.FUNCTIONS_KEY, complexFunctionsPercKey, false);
compute(context, complexFunctionsLocKey, locInFunctionsKey, complexFunctionsLocPercKey, false);
compute(context, bigFunctionsKey, CoreMetrics.FUNCTIONS_KEY, bigFunctionsPercKey, false);
compute(context, bigFunctionsLocKey, locInFunctionsKey, bigFunctionsLocPercKey, false);
}

private void compute(MeasureComputerContext context, String valueKey, String totalKey, String densityKey,
private static void compute(MeasureComputerContext context, String valueKey, String totalKey, String densityKey,
boolean calculateReminingPercent) {
final Component component = context.getComponent();

Expand All @@ -126,9 +126,10 @@ private void compute(MeasureComputerContext context, String valueKey, String tot
LOG.error("Component {}: not enough data to calcualte measure {}", context.getComponent().getKey(), densityKey);
return;
}
if (context.getMeasure(densityKey) != null) {
final Measure existingMeasure = context.getMeasure(densityKey);
if (existingMeasure != null) {
LOG.error("Component {}: measure {} already calculated, value = {}", component.getKey(), densityKey,
context.getMeasure(densityKey).getDoubleValue());
existingMeasure.getDoubleValue());
return;
}

Expand All @@ -138,10 +139,13 @@ private void compute(MeasureComputerContext context, String valueKey, String tot
value = Integer.max(total - value, 0);
}

final double density = (total >= value && total != 0) ? (double) value / (double) total * 100.0 : 0.0;
double density = 0.0;
if (total >= value && total != 0) {
density = (double) value / (double) total * 100.0;
}

LOG.info("Component {}: add measure {}, value {}", component.getKey(), densityKey, density);
context.addMeasure(densityKey, density);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.sonar.sslr.api.Trivia;
import com.sonar.sslr.impl.ast.AstXmlPrinter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.sonar.api.utils.log.Logger;
Expand Down Expand Up @@ -70,8 +69,7 @@
*
* @param <G>
*/
public abstract class AbstractCxxPublicApiVisitor<G extends Grammar> extends SquidCheck<G>
implements AstVisitor {
public abstract class AbstractCxxPublicApiVisitor<G extends Grammar> extends SquidCheck<G> {

private static final Logger LOG = Loggers.get(AbstractCxxPublicApiVisitor.class);

Expand Down Expand Up @@ -887,7 +885,7 @@ private static boolean isDoxygenCommentBlock(String comment) {
|| comment.startsWith("///") || comment.startsWith("//!");
}

public AbstractCxxPublicApiVisitor<G> withHeaderFileSuffixes(List<String> headerFileSuffixes) {
public final AbstractCxxPublicApiVisitor<G> withHeaderFileSuffixes(List<String> headerFileSuffixes) {
this.headerFileSuffixes = new ArrayList<>(headerFileSuffixes);
return this;
}
Expand Down

0 comments on commit f1ae36d

Please sign in to comment.