Skip to content

Commit

Permalink
Add implementation for min and max in cyclomatic metric
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannitangredi committed Nov 23, 2021
1 parent ef0a27c commit ee1d966
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
10 changes: 5 additions & 5 deletions rust-code-analysis-web/src/web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ mod tests {
"spaces": {"kind": "unit",
"start_line": 1,
"end_line": 4,
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
Expand All @@ -658,7 +658,7 @@ mod tests {
"spaces": [{"kind": "function",
"start_line": 3,
"end_line": 4,
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min":1.0, "max":1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
Expand Down Expand Up @@ -711,7 +711,7 @@ mod tests {
"spaces": {"kind": "unit",
"start_line": 1,
"end_line": 2,
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
Expand Down Expand Up @@ -760,7 +760,7 @@ mod tests {
"spaces": {"kind": "unit",
"start_line": 1,
"end_line": 2,
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min": 1.0,"max": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
Expand All @@ -787,7 +787,7 @@ mod tests {
"spaces": [{"kind": "function",
"start_line": 1,
"end_line": 2,
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min": 1.0,"max": 1.0},
"cognitive": {"sum": 0.0, "average": 0.0},
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
"nexits": {"sum": 0.0, "average": 0.0},
Expand Down
43 changes: 39 additions & 4 deletions src/metrics/cyclomatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub struct Stats {
cyclomatic_sum: f64,
cyclomatic: f64,
n: usize,
cyclomatic_max: f64,
cyclomatic_min: f64,
}

impl Default for Stats {
Expand All @@ -18,6 +20,8 @@ impl Default for Stats {
cyclomatic_sum: 0.,
cyclomatic: 1.,
n: 1,
cyclomatic_max: 0.,
cyclomatic_min: f64::MAX,
}
}
}
Expand All @@ -30,6 +34,8 @@ impl Serialize for Stats {
let mut st = serializer.serialize_struct("cyclomatic", 2)?;
st.serialize_field("sum", &self.cyclomatic_sum())?;
st.serialize_field("average", &self.cyclomatic_average())?;
st.serialize_field("min", &self.cyclomatic_min())?;
st.serialize_field("max", &self.cyclomatic_max())?;
st.end()
}
}
Expand All @@ -38,16 +44,22 @@ impl fmt::Display for Stats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"sum: {}, average: {}",
"sum: {}, average: {}, min: {}, max: {}",
self.cyclomatic_sum(),
self.cyclomatic_average(),
self.cyclomatic_min(),
self.cyclomatic_max()
)
}
}

impl Stats {
/// Merges a second `Cyclomatic` metric into the first one
pub fn merge(&mut self, other: &Stats) {
//Calculate minimum and maximum values
self.cyclomatic_max = self.cyclomatic_max.max(other.cyclomatic_max);
self.cyclomatic_min = self.cyclomatic_min.min(other.cyclomatic_min);

self.cyclomatic_sum += other.cyclomatic_sum;
self.n += other.n;
}
Expand All @@ -68,9 +80,18 @@ impl Stats {
pub fn cyclomatic_average(&self) -> f64 {
self.cyclomatic_sum() / self.n as f64
}

/// Returns the `Cyclomatic` maximum value
pub fn cyclomatic_max(&self) -> f64 {
self.cyclomatic_max
}
/// Returns the `Cyclomatic` minimum value
pub fn cyclomatic_min(&self) -> f64 {
self.cyclomatic_min
}
/// Last step for computing minimum and maximum value and update cyclomatic_sum
pub fn compute_minmax(&mut self) {
self.cyclomatic_max = self.cyclomatic_max.max(self.cyclomatic);
self.cyclomatic_min = self.cyclomatic_min.min(self.cyclomatic);
self.cyclomatic_sum += self.cyclomatic;
}
}
Expand Down Expand Up @@ -202,7 +223,9 @@ mod tests {
cyclomatic,
[(cyclomatic_sum, 6, usize)],
[
(cyclomatic_average, 3.0), // nspace = 2 (func and unit)
(cyclomatic_average, 3.0), // nspace = 2 (func and unit)
(cyclomatic_max, 5.0),
(cyclomatic_min, 1.0)
]
);
}
Expand All @@ -220,6 +243,8 @@ mod tests {
[(cyclomatic_sum, 4, usize)],
[
(cyclomatic_average, 2.0), // nspace = 2 (func and unit)
(cyclomatic_max, 3.0),
(cyclomatic_min, 1.0)
]
);
}
Expand All @@ -240,7 +265,9 @@ mod tests {
cyclomatic,
[(cyclomatic_sum, 5, usize)],
[
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 1.0)
]
);
}
Expand Down Expand Up @@ -270,6 +297,8 @@ mod tests {
[(cyclomatic_sum, 5, usize)],
[
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 1.0)
]
);
}
Expand All @@ -295,6 +324,8 @@ mod tests {
[(cyclomatic_sum, 5, usize)],
[
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 1.0)
]
);
}
Expand Down Expand Up @@ -329,6 +360,8 @@ mod tests {
[(cyclomatic_sum, 7, usize)],
[
(cyclomatic_average, 3.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 3.0)
]
);
}
Expand Down Expand Up @@ -367,6 +400,8 @@ mod tests {
[(cyclomatic_sum, 7, usize)],
[
(cyclomatic_average, 3.5), // nspace = 2 (func and unit)
(cyclomatic_max, 4.0),
(cyclomatic_min, 3.0)
]
);
}
Expand Down

0 comments on commit ee1d966

Please sign in to comment.