diff --git a/rust-code-analysis-web/src/web/server.rs b/rust-code-analysis-web/src/web/server.rs index dc5a3184d..d5d90617b 100644 --- a/rust-code-analysis-web/src/web/server.rs +++ b/rust-code-analysis-web/src/web/server.rs @@ -633,7 +633,7 @@ mod tests { "end_line": 4, "metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0}, "cognitive": {"sum": 0.0, "average": 0.0, "min":0.0, "max":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}, + "nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0, "closures_max": 0.0, "closures_min": 0.0, "functions_max": 0.0, "functions_min": 0.0}, "nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0}, "halstead": {"bugs": 0.000_942_552_557_372_941_4, "difficulty": 1.0, @@ -660,7 +660,7 @@ mod tests { "end_line": 4, "metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min":1.0, "max":1.0}, "cognitive": {"sum": 0.0, "average": 0.0 , "min":0.0, "max":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}, + "nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0, "closures_max": 0.0, "closures_min": 0.0, "functions_max": 0.0, "functions_min": 0.0}, "nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0}, "halstead": {"bugs": 0.000_942_552_557_372_941_4, "difficulty": 1.0, @@ -713,7 +713,7 @@ mod tests { "end_line": 2, "metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min":1.0, "max":1.0}, "cognitive": {"sum": 0.0, "average": 0.0 , "min":0.0, "max":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}, + "nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0, "closures_max": 0.0, "closures_min": 0.0, "functions_max": 0.0, "functions_min": 0.0}, "nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0}, "halstead": {"bugs": 0.000_942_552_557_372_941_4, "difficulty": 1.0, @@ -762,7 +762,7 @@ mod tests { "end_line": 2, "metrics": {"cyclomatic": {"sum": 2.0, "average": 1.0, "min": 1.0,"max": 1.0}, "cognitive": {"sum": 0.0, "average": 0.0 , "min":0.0, "max":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}, + "nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0, "closures_max": 0.0, "closures_min": 0.0, "functions_max": 0.0, "functions_min": 0.0}, "nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0}, "halstead": {"bugs": 0.000_942_552_557_372_941_4, "difficulty": 1.0, @@ -789,7 +789,7 @@ mod tests { "end_line": 2, "metrics": {"cyclomatic": {"sum": 1.0, "average": 1.0, "min": 1.0,"max": 1.0}, "cognitive": {"sum": 0.0, "average": 0.0 , "min":0.0, "max":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}, + "nargs": {"total_functions": 0.0, "average_functions": 0.0, "total_closures": 0.0, "average_closures": 0.0, "total": 0.0, "average": 0.0, "closures_max": 0.0, "closures_min": 0.0, "functions_max": 0.0, "functions_min": 0.0}, "nexits": {"sum": 0.0, "average": 0.0,"min":0.0,"max":0.0}, "halstead": {"bugs": 0.000_942_552_557_372_941_4, "difficulty": 1.0, diff --git a/src/metrics/nargs.rs b/src/metrics/nargs.rs index 208c37a44..72e1d79d7 100644 --- a/src/metrics/nargs.rs +++ b/src/metrics/nargs.rs @@ -13,6 +13,12 @@ use crate::*; pub struct Stats { fn_nargs: usize, closure_nargs: usize, + fn_nargs_sum: usize, + closure_nargs_sum: usize, + fn_nargs_min: usize, + closure_nargs_min: usize, + fn_nargs_max: usize, + closure_nargs_max: usize, total_functions: usize, total_closures: usize, } @@ -22,6 +28,12 @@ impl Default for Stats { Self { fn_nargs: 0, closure_nargs: 0, + fn_nargs_sum: 0, + closure_nargs_sum: 0, + fn_nargs_min: usize::MAX, + closure_nargs_min: usize::MAX, + fn_nargs_max: 0, + closure_nargs_max: 0, total_functions: 0, total_closures: 0, } @@ -33,13 +45,17 @@ impl Serialize for Stats { where S: Serializer, { - let mut st = serializer.serialize_struct("nargs", 6)?; - st.serialize_field("total_functions", &self.fn_args())?; - st.serialize_field("total_closures", &self.closure_args())?; + let mut st = serializer.serialize_struct("nargs", 10)?; + st.serialize_field("total_functions", &self.fn_args_sum())?; + st.serialize_field("total_closures", &self.closure_args_sum())?; st.serialize_field("average_functions", &self.fn_args_average())?; st.serialize_field("average_closures", &self.closure_args_average())?; st.serialize_field("total", &self.nargs_total())?; st.serialize_field("average", &self.nargs_average())?; + st.serialize_field("functions_min", &self.fn_args_min())?; + st.serialize_field("functions_max", &self.fn_args_max())?; + st.serialize_field("closures_min", &self.closure_args_min())?; + st.serialize_field("closures_max", &self.closure_args_max())?; st.end() } } @@ -48,13 +64,17 @@ impl fmt::Display for Stats { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, - "total_functions: {}, total_closures: {}, average_functions: {}, average_closures: {}, total: {}, average: {}", + "total_functions: {}, total_closures: {}, average_functions: {}, average_closures: {}, total: {}, average: {}, functions_min: {}, functions_max: {}, closures_min: {}, closures_max: {}", self.fn_args(), self.closure_args(), self.fn_args_average(), self.closure_args_average(), self.nargs_total(), - self.nargs_average() + self.nargs_average(), + self.fn_args_min(), + self.fn_args_max(), + self.closure_args_min(), + self.closure_args_max() ) } } @@ -62,8 +82,12 @@ impl fmt::Display for Stats { impl Stats { /// Merges a second `NArgs` metric into the first one pub fn merge(&mut self, other: &Stats) { - self.fn_nargs += other.fn_nargs; - self.closure_nargs += other.closure_nargs; + self.closure_nargs_min = self.closure_nargs_min.min(other.closure_nargs_min); + self.closure_nargs_max = self.closure_nargs_max.max(other.closure_nargs_max); + self.fn_nargs_min = self.fn_nargs_min.min(other.fn_nargs_min); + self.fn_nargs_max = self.fn_nargs_max.max(other.fn_nargs_max); + self.fn_nargs_sum += other.fn_nargs_sum; + self.closure_nargs_sum += other.closure_nargs_sum; } /// Returns the number of function arguments in a space. @@ -78,23 +102,35 @@ impl Stats { self.closure_nargs as f64 } + /// Returns the number of function arguments sum in a space. + #[inline(always)] + pub fn fn_args_sum(&self) -> f64 { + self.fn_nargs_sum as f64 + } + + /// Returns the number of closure arguments sum in a space. + #[inline(always)] + pub fn closure_args_sum(&self) -> f64 { + self.closure_nargs_sum as f64 + } + /// Returns the average number of functions arguments in a space. #[inline(always)] pub fn fn_args_average(&self) -> f64 { - self.fn_nargs as f64 / self.total_functions.max(1) as f64 + self.fn_nargs_sum as f64 / self.total_functions.max(1) as f64 } /// Returns the average number of closures arguments in a space. #[inline(always)] pub fn closure_args_average(&self) -> f64 { - self.closure_nargs as f64 / self.total_closures.max(1) as f64 + self.closure_nargs_sum as f64 / self.total_closures.max(1) as f64 } /// Returns the total number of arguments of each function and /// closure in a space. #[inline(always)] pub fn nargs_total(&self) -> f64 { - self.fn_args() + self.closure_args() + self.fn_args_sum() + self.closure_args_sum() } /// Returns the `NArgs` metric average value @@ -105,7 +141,35 @@ impl Stats { pub fn nargs_average(&self) -> f64 { self.nargs_total() / (self.total_functions + self.total_closures).max(1) as f64 } + /// Returns the minimum number of function arguments in a space. + #[inline(always)] + pub fn fn_args_min(&self) -> f64 { + self.fn_nargs_min as f64 + } + /// Returns the maximum number of function arguments in a space. + #[inline(always)] + pub fn fn_args_max(&self) -> f64 { + self.fn_nargs_max as f64 + } + /// Returns the minimum number of closure arguments in a space. + #[inline(always)] + pub fn closure_args_min(&self) -> f64 { + self.closure_nargs_min as f64 + } + /// Returns the maximum number of closure arguments in a space. + #[inline(always)] + pub fn closure_args_max(&self) -> f64 { + self.closure_nargs_max as f64 + } + pub fn compute_minmax(&mut self) { + self.closure_nargs_min = self.closure_nargs_min.min(self.closure_nargs); + self.closure_nargs_max = self.closure_nargs_max.max(self.closure_nargs); + self.fn_nargs_min = self.fn_nargs_min.min(self.fn_nargs); + self.fn_nargs_max = self.fn_nargs_max.max(self.fn_nargs); + self.closure_nargs_sum += self.closure_nargs; + self.fn_nargs_sum += self.fn_nargs; + } pub(crate) fn finalize(&mut self, total_functions: usize, total_closures: usize) { self.total_functions = total_functions; self.total_closures = total_closures; @@ -185,8 +249,8 @@ mod tests { PythonParser, nargs, [ - (fn_args, 0, usize), - (closure_args, 0, usize), + (fn_args_sum, 0, usize), + (closure_args_sum, 0, usize), (nargs_total, 0, usize) ], [ @@ -205,8 +269,8 @@ mod tests { RustParser, nargs, [ - (fn_args, 0, usize), - (closure_args, 0, usize), + (fn_args_sum, 0, usize), + (closure_args_sum, 0, usize), (nargs_total, 0, usize) ], [ @@ -225,8 +289,8 @@ mod tests { CppParser, nargs, [ - (fn_args, 0, usize), - (closure_args, 0, usize), + (fn_args_sum, 0, usize), + (closure_args_sum, 0, usize), (nargs_total, 0, usize) ], [ @@ -245,8 +309,8 @@ mod tests { JavascriptParser, nargs, [ - (fn_args, 0, usize), - (closure_args, 0, usize), + (fn_args_sum, 0, usize), + (closure_args_sum, 0, usize), (nargs_total, 0, usize) ], [ @@ -267,9 +331,13 @@ mod tests { PythonParser, nargs, [ - (fn_args, 2, usize), - (closure_args, 0, usize), - (nargs_total, 2, usize) + (fn_args_sum, 2, usize), + (closure_args_sum, 0, usize), + (nargs_total, 2, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.0), @@ -291,9 +359,13 @@ mod tests { RustParser, nargs, [ - (fn_args, 2, usize), - (closure_args, 0, usize), - (nargs_total, 2, usize) + (fn_args_sum, 2, usize), + (closure_args_sum, 0, usize), + (nargs_total, 2, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.0), @@ -315,9 +387,13 @@ mod tests { CppParser, nargs, [ - (fn_args, 2, usize), - (closure_args, 0, usize), - (nargs_total, 2, usize) + (fn_args_sum, 2, usize), + (closure_args_sum, 0, usize), + (nargs_total, 2, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.0), @@ -337,9 +413,13 @@ mod tests { JavascriptParser, nargs, [ - (fn_args, 2, usize), - (closure_args, 0, usize), - (nargs_total, 2, usize) + (fn_args_sum, 2, usize), + (closure_args_sum, 0, usize), + (nargs_total, 2, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.0), @@ -357,9 +437,13 @@ mod tests { PythonParser, nargs, [ - (fn_args, 0, usize), - (closure_args, 1, usize), - (nargs_total, 1, usize) + (fn_args_sum, 0, usize), + (closure_args_sum, 1, usize), + (nargs_total, 1, usize), + (fn_args_min, 0, usize), + (fn_args_max, 0, usize), + (closure_args_min, 1, usize), + (closure_args_max, 1, usize), ], [ (fn_args_average, 0.0), @@ -377,9 +461,13 @@ mod tests { RustParser, nargs, [ - (fn_args, 0, usize), - (closure_args, 1, usize), - (nargs_total, 1, usize) + (fn_args_sum, 0, usize), + (closure_args_sum, 1, usize), + (nargs_total, 1, usize), + (fn_args_min, 0, usize), + (fn_args_max, 0, usize), + (closure_args_min, 0, usize), + (closure_args_max, 1, usize), ], [ (fn_args_average, 0.0), @@ -397,9 +485,13 @@ mod tests { CppParser, nargs, [ - (fn_args, 0, usize), - (closure_args, 2, usize), - (nargs_total, 2, usize) + (fn_args_sum, 0, usize), + (closure_args_sum, 2, usize), + (nargs_total, 2, usize), + (fn_args_min, 0, usize), + (fn_args_max, 0, usize), + (closure_args_min, 2, usize), + (closure_args_max, 2, usize), ], [ (fn_args_average, 0.0), @@ -417,9 +509,13 @@ mod tests { JavascriptParser, nargs, [ - (fn_args, 0, usize), - (closure_args, 2, usize), - (nargs_total, 2, usize) + (fn_args_sum, 0, usize), + (closure_args_sum, 2, usize), + (nargs_total, 2, usize), + (fn_args_min, 0, usize), + (fn_args_max, 0, usize), + (closure_args_min, 0, usize), + (closure_args_max, 2, usize), ], [ (fn_args_average, 0.0), @@ -442,9 +538,13 @@ mod tests { PythonParser, nargs, [ - (fn_args, 4, usize), - (closure_args, 0, usize), - (nargs_total, 4, usize) + (fn_args_sum, 4, usize), + (closure_args_sum, 0, usize), + (nargs_total, 4, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.0), @@ -464,9 +564,13 @@ mod tests { PythonParser, nargs, [ - (fn_args, 5, usize), - (closure_args, 0, usize), - (nargs_total, 5, usize) + (fn_args_sum, 5, usize), + (closure_args_sum, 0, usize), + (nargs_total, 5, usize), + (fn_args_min, 0, usize), + (fn_args_max, 3, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.5), @@ -493,9 +597,13 @@ mod tests { RustParser, nargs, [ - (fn_args, 4, usize), - (closure_args, 0, usize), - (nargs_total, 4, usize) + (fn_args_sum, 4, usize), + (closure_args_sum, 0, usize), + (nargs_total, 4, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.0), @@ -519,9 +627,13 @@ mod tests { RustParser, nargs, [ - (fn_args, 5, usize), - (closure_args, 0, usize), - (nargs_total, 5, usize) + (fn_args_sum, 5, usize), + (closure_args_sum, 0, usize), + (nargs_total, 5, usize), + (fn_args_min, 0, usize), + (fn_args_max, 3, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.5), @@ -548,9 +660,13 @@ mod tests { CppParser, nargs, [ - (fn_args, 4, usize), - (closure_args, 0, usize), - (nargs_total, 4, usize) + (fn_args_sum, 4, usize), + (closure_args_sum, 0, usize), + (nargs_total, 4, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.0), @@ -574,9 +690,13 @@ mod tests { CppParser, nargs, [ - (fn_args, 5, usize), - (closure_args, 0, usize), - (nargs_total, 5, usize) + (fn_args_sum, 5, usize), + (closure_args_sum, 0, usize), + (nargs_total, 5, usize), + (fn_args_min, 0, usize), + (fn_args_max, 3, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.5), @@ -599,9 +719,13 @@ mod tests { JavascriptParser, nargs, [ - (fn_args, 4, usize), - (closure_args, 0, usize), - (nargs_total, 4, usize) + (fn_args_sum, 4, usize), + (closure_args_sum, 0, usize), + (nargs_total, 4, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.0), @@ -621,9 +745,13 @@ mod tests { JavascriptParser, nargs, [ - (fn_args, 5, usize), - (closure_args, 0, usize), - (nargs_total, 5, usize) + (fn_args_sum, 5, usize), + (closure_args_sum, 0, usize), + (nargs_total, 5, usize), + (fn_args_min, 0, usize), + (fn_args_max, 3, usize), + (closure_args_min, 0, usize), + (closure_args_max, 0, usize), ], [ (fn_args_average, 2.5), @@ -646,9 +774,13 @@ mod tests { PythonParser, nargs, [ - (fn_args, 3, usize), - (closure_args, 2, usize), - (nargs_total, 5, usize) + (fn_args_sum, 3, usize), + (closure_args_sum, 2, usize), + (nargs_total, 5, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 2, usize), ], [ (fn_args_average, 1.5), @@ -673,9 +805,13 @@ mod tests { RustParser, nargs, [ - (fn_args, 3, usize), - (closure_args, 3, usize), - (nargs_total, 6, usize) + (fn_args_sum, 3, usize), + (closure_args_sum, 3, usize), + (nargs_total, 6, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 2, usize), ], [ (fn_args_average, 1.5), @@ -697,9 +833,13 @@ mod tests { CppParser, nargs, [ - (fn_args, 3, usize), - (closure_args, 3, usize), - (nargs_total, 6, usize) + (fn_args_sum, 3, usize), + (closure_args_sum, 3, usize), + (nargs_total, 6, usize), + (fn_args_min, 0, usize), + (fn_args_max, 3, usize), + (closure_args_min, 0, usize), + (closure_args_max, 3, usize), ], [ (fn_args_average, 3.0), @@ -724,9 +864,13 @@ mod tests { JavascriptParser, nargs, [ - (fn_args, 6, usize), - (closure_args, 1, usize), - (nargs_total, 7, usize) + (fn_args_sum, 6, usize), + (closure_args_sum, 1, usize), + (nargs_total, 7, usize), + (fn_args_min, 0, usize), + (fn_args_max, 2, usize), + (closure_args_min, 0, usize), + (closure_args_max, 1, usize), ], [ (fn_args_average, 2.), diff --git a/src/spaces.rs b/src/spaces.rs index d47cf0737..b6327cda5 100644 --- a/src/spaces.rs +++ b/src/spaces.rs @@ -200,6 +200,7 @@ fn compute_minmax(state: &mut State) { state.space.metrics.cyclomatic.compute_minmax(); state.space.metrics.nexits.compute_minmax(); state.space.metrics.cognitive.compute_minmax(); + state.space.metrics.nargs.compute_minmax(); } fn finalize(state_stack: &mut Vec, diff_level: usize) {