Skip to content

Commit ee1d966

Browse files
Add implementation for min and max in cyclomatic metric
1 parent ef0a27c commit ee1d966

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

rust-code-analysis-web/src/web/server.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ mod tests {
631631
"spaces": {"kind": "unit",
632632
"start_line": 1,
633633
"end_line": 4,
634-
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
634+
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0},
635635
"cognitive": {"sum": 0.0, "average": 0.0},
636636
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
637637
"nexits": {"sum": 0.0, "average": 0.0},
@@ -658,7 +658,7 @@ mod tests {
658658
"spaces": [{"kind": "function",
659659
"start_line": 3,
660660
"end_line": 4,
661-
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
661+
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min":1.0, "max":1.0},
662662
"cognitive": {"sum": 0.0, "average": 0.0},
663663
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
664664
"nexits": {"sum": 0.0, "average": 0.0},
@@ -711,7 +711,7 @@ mod tests {
711711
"spaces": {"kind": "unit",
712712
"start_line": 1,
713713
"end_line": 2,
714-
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
714+
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0},
715715
"cognitive": {"sum": 0.0, "average": 0.0},
716716
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
717717
"nexits": {"sum": 0.0, "average": 0.0},
@@ -760,7 +760,7 @@ mod tests {
760760
"spaces": {"kind": "unit",
761761
"start_line": 1,
762762
"end_line": 2,
763-
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0},
763+
"metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min": 1.0,"max": 1.0},
764764
"cognitive": {"sum": 0.0, "average": 0.0},
765765
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
766766
"nexits": {"sum": 0.0, "average": 0.0},
@@ -787,7 +787,7 @@ mod tests {
787787
"spaces": [{"kind": "function",
788788
"start_line": 1,
789789
"end_line": 2,
790-
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0},
790+
"metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min": 1.0,"max": 1.0},
791791
"cognitive": {"sum": 0.0, "average": 0.0},
792792
"nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0},
793793
"nexits": {"sum": 0.0, "average": 0.0},

src/metrics/cyclomatic.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub struct Stats {
1010
cyclomatic_sum: f64,
1111
cyclomatic: f64,
1212
n: usize,
13+
cyclomatic_max: f64,
14+
cyclomatic_min: f64,
1315
}
1416

1517
impl Default for Stats {
@@ -18,6 +20,8 @@ impl Default for Stats {
1820
cyclomatic_sum: 0.,
1921
cyclomatic: 1.,
2022
n: 1,
23+
cyclomatic_max: 0.,
24+
cyclomatic_min: f64::MAX,
2125
}
2226
}
2327
}
@@ -30,6 +34,8 @@ impl Serialize for Stats {
3034
let mut st = serializer.serialize_struct("cyclomatic", 2)?;
3135
st.serialize_field("sum", &self.cyclomatic_sum())?;
3236
st.serialize_field("average", &self.cyclomatic_average())?;
37+
st.serialize_field("min", &self.cyclomatic_min())?;
38+
st.serialize_field("max", &self.cyclomatic_max())?;
3339
st.end()
3440
}
3541
}
@@ -38,16 +44,22 @@ impl fmt::Display for Stats {
3844
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3945
write!(
4046
f,
41-
"sum: {}, average: {}",
47+
"sum: {}, average: {}, min: {}, max: {}",
4248
self.cyclomatic_sum(),
4349
self.cyclomatic_average(),
50+
self.cyclomatic_min(),
51+
self.cyclomatic_max()
4452
)
4553
}
4654
}
4755

4856
impl Stats {
4957
/// Merges a second `Cyclomatic` metric into the first one
5058
pub fn merge(&mut self, other: &Stats) {
59+
//Calculate minimum and maximum values
60+
self.cyclomatic_max = self.cyclomatic_max.max(other.cyclomatic_max);
61+
self.cyclomatic_min = self.cyclomatic_min.min(other.cyclomatic_min);
62+
5163
self.cyclomatic_sum += other.cyclomatic_sum;
5264
self.n += other.n;
5365
}
@@ -68,9 +80,18 @@ impl Stats {
6880
pub fn cyclomatic_average(&self) -> f64 {
6981
self.cyclomatic_sum() / self.n as f64
7082
}
71-
83+
/// Returns the `Cyclomatic` maximum value
84+
pub fn cyclomatic_max(&self) -> f64 {
85+
self.cyclomatic_max
86+
}
87+
/// Returns the `Cyclomatic` minimum value
88+
pub fn cyclomatic_min(&self) -> f64 {
89+
self.cyclomatic_min
90+
}
7291
/// Last step for computing minimum and maximum value and update cyclomatic_sum
7392
pub fn compute_minmax(&mut self) {
93+
self.cyclomatic_max = self.cyclomatic_max.max(self.cyclomatic);
94+
self.cyclomatic_min = self.cyclomatic_min.min(self.cyclomatic);
7495
self.cyclomatic_sum += self.cyclomatic;
7596
}
7697
}
@@ -202,7 +223,9 @@ mod tests {
202223
cyclomatic,
203224
[(cyclomatic_sum, 6, usize)],
204225
[
205-
(cyclomatic_average, 3.0), // nspace = 2 (func and unit)
226+
(cyclomatic_average, 3.0), // nspace = 2 (func and unit)
227+
(cyclomatic_max, 5.0),
228+
(cyclomatic_min, 1.0)
206229
]
207230
);
208231
}
@@ -220,6 +243,8 @@ mod tests {
220243
[(cyclomatic_sum, 4, usize)],
221244
[
222245
(cyclomatic_average, 2.0), // nspace = 2 (func and unit)
246+
(cyclomatic_max, 3.0),
247+
(cyclomatic_min, 1.0)
223248
]
224249
);
225250
}
@@ -240,7 +265,9 @@ mod tests {
240265
cyclomatic,
241266
[(cyclomatic_sum, 5, usize)],
242267
[
243-
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
268+
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
269+
(cyclomatic_max, 4.0),
270+
(cyclomatic_min, 1.0)
244271
]
245272
);
246273
}
@@ -270,6 +297,8 @@ mod tests {
270297
[(cyclomatic_sum, 5, usize)],
271298
[
272299
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
300+
(cyclomatic_max, 4.0),
301+
(cyclomatic_min, 1.0)
273302
]
274303
);
275304
}
@@ -295,6 +324,8 @@ mod tests {
295324
[(cyclomatic_sum, 5, usize)],
296325
[
297326
(cyclomatic_average, 2.5), // nspace = 2 (func and unit)
327+
(cyclomatic_max, 4.0),
328+
(cyclomatic_min, 1.0)
298329
]
299330
);
300331
}
@@ -329,6 +360,8 @@ mod tests {
329360
[(cyclomatic_sum, 7, usize)],
330361
[
331362
(cyclomatic_average, 3.5), // nspace = 2 (func and unit)
363+
(cyclomatic_max, 4.0),
364+
(cyclomatic_min, 3.0)
332365
]
333366
);
334367
}
@@ -367,6 +400,8 @@ mod tests {
367400
[(cyclomatic_sum, 7, usize)],
368401
[
369402
(cyclomatic_average, 3.5), // nspace = 2 (func and unit)
403+
(cyclomatic_max, 4.0),
404+
(cyclomatic_min, 3.0)
370405
]
371406
);
372407
}

0 commit comments

Comments
 (0)