or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

ast.mdcpd.mdindex.mdlanguage-module.mdmetrics.mdmultifile.mdrules.md

metrics.mddocs/

0

# Metrics

1

2

Complexity and quality metrics calculation for Apex code analysis. Provides cyclomatic complexity, cognitive complexity, and weighted method count metrics.

3

4

## Capabilities

5

6

### Built-in Metrics

7

8

Pre-defined metrics for measuring code complexity and quality.

9

10

```java { .api }

11

/**

12

* Built-in metrics for Apex code analysis

13

*/

14

public class ApexMetrics {

15

/**

16

* Cyclomatic Complexity metric - measures the number of independent paths through code

17

* Applied to methods and constructors

18

*/

19

public static final Metric<ASTMethod, Integer> CYCLO;

20

21

/**

22

* Cognitive Complexity metric - measures how difficult code is to understand

23

* Applied to methods and constructors, considers nesting and control flow

24

*/

25

public static final Metric<ASTMethod, Integer> COGNITIVE_COMPLEXITY;

26

27

/**

28

* Weighted Method Count metric - sum of complexities of all methods in a class

29

* Applied to classes, interfaces, and enums

30

*/

31

public static final Metric<ASTUserClass, Integer> WEIGHED_METHOD_COUNT;

32

}

33

```

34

35

### Metric Computation

36

37

Computing metrics for AST nodes using the PMD metrics framework.

38

39

```java { .api }

40

/**

41

* Utility class for computing metrics on AST nodes

42

* Part of PMD core framework

43

*/

44

public class MetricsUtil {

45

/**

46

* Compute a metric for a given AST node

47

* @param metric - The metric to compute

48

* @param node - The AST node to analyze

49

* @return The computed metric value

50

*/

51

public static <T, R> R computeMetric(Metric<T, R> metric, T node);

52

53

/**

54

* Check if metrics are supported for a given node type

55

* @param nodeType - The class of AST node

56

* @return true if metrics can be computed for this node type

57

*/

58

public static boolean supportsAll(Class<?> nodeType);

59

}

60

```

61

62

**Usage Examples:**

63

64

```java

65

import net.sourceforge.pmd.lang.apex.metrics.ApexMetrics;

66

import net.sourceforge.pmd.lang.metrics.MetricsUtil;

67

68

// Compute cyclomatic complexity for a method

69

public class ComplexityAnalyzer extends ApexVisitorBase<Object, Object> {

70

@Override

71

public Object visit(ASTMethod node, Object data) {

72

// Calculate cyclomatic complexity

73

int cyclomaticComplexity = MetricsUtil.computeMetric(ApexMetrics.CYCLO, node);

74

System.out.println("Method " + node.getName() + " has cyclomatic complexity: " + cyclomaticComplexity);

75

76

// Calculate cognitive complexity

77

int cognitiveComplexity = MetricsUtil.computeMetric(ApexMetrics.COGNITIVE_COMPLEXITY, node);

78

System.out.println("Method " + node.getName() + " has cognitive complexity: " + cognitiveComplexity);

79

80

// Flag high complexity methods

81

if (cyclomaticComplexity > 10) {

82

System.out.println("WARNING: High cyclomatic complexity in method " + node.getName());

83

}

84

85

return super.visit(node, data);

86

}

87

88

@Override

89

public Object visit(ASTUserClass node, Object data) {

90

// Calculate weighted method count for class

91

int weightedMethodCount = MetricsUtil.computeMetric(ApexMetrics.WEIGHED_METHOD_COUNT, node);

92

System.out.println("Class " + node.getQualifiedName().getClassName() +

93

" has weighted method count: " + weightedMethodCount);

94

95

// Flag complex classes

96

if (weightedMethodCount > 50) {

97

System.out.println("WARNING: High complexity class " + node.getQualifiedName().getClassName());

98

}

99

100

return super.visit(node, data);

101

}

102

}

103

104

// Use in rule or analysis tool

105

ComplexityAnalyzer analyzer = new ComplexityAnalyzer();

106

apexFile.jjtAccept(analyzer, null);

107

108

// Check if metrics are supported for a node type

109

boolean supportsMetrics = MetricsUtil.supportsAll(ASTMethod.class); // true

110

boolean supportsMetrics2 = MetricsUtil.supportsAll(ASTVariableExpression.class); // false

111

```