model: add `MEntity::is_broken`
[nit.git] / src / metrics / inheritance_metrics.nit
index 4ba1dc7..1287c0f 100644 (file)
 # Collect metrics about inheritance usage
 module inheritance_metrics
 
-import model
+import metrics_base
 import mmodules_metrics
 import mclasses_metrics
-import phase
-import frontend
 
 redef class ToolContext
-       var inheritance_metrics_phase = new InheritanceMetricsPhase(self, null)
+       var inheritance_metrics_phase: Phase = new InheritanceMetricsPhase(self, null)
 end
 
 # Extract metrics about inheritance from model.
 private class InheritanceMetricsPhase
        super Phase
-       redef fun process_mainmodule(mainmodule)
+       redef fun process_mainmodule(mainmodule, given_mmodules)
        do
                if not toolcontext.opt_inheritance.value and not toolcontext.opt_all.value then return
+               var csv = toolcontext.opt_csv.value
+               var out = "{toolcontext.opt_dir.value or else "metrics"}/inheritance"
+               out.mkdir
 
                print toolcontext.format_h1("\n# Inheritance metrics")
 
@@ -58,11 +59,11 @@ private class InheritanceMetricsPhase
                var model = toolcontext.modelbuilder.model
                var mmodules = new HashSet[MModule]
                var mclasses = new HashSet[MClass]
-               for mproject in model.mprojects do
+               for mpackage in model.mpackages do
 
-                       print toolcontext.format_h2("\n ## project {mproject}")
+                       print toolcontext.format_h2("\n ## package {mpackage}")
 
-                       for mgroup in mproject.mgroups do
+                       for mgroup in mpackage.mgroups do
                                if mgroup.mmodules.is_empty then continue
 
                                # Scalar metrics
@@ -73,10 +74,14 @@ private class InheritanceMetricsPhase
                                if mod_mclasses.is_empty then continue
                                mmodules.add_all(mgroup.mmodules)
                                mclasses.add_all(mod_mclasses)
+                               cmetrics.clear
                                cmetrics.collect(new HashSet[MClass].from(mod_mclasses))
                                cmetrics.to_console(1, not toolcontext.opt_nocolors.value)
+                               if csv then cmetrics.to_csv.save("{out}/{mgroup}_classes.csv")
+                               hmetrics.clear
                                hmetrics.collect(new HashSet[MModule].from(mgroup.mmodules))
                                hmetrics.to_console(1, not toolcontext.opt_nocolors.value)
+                               if csv then hmetrics.to_csv.save("{out}/{mgroup}_inheritance.csv")
                        end
                end
                if not mclasses.is_empty then
@@ -85,9 +90,11 @@ private class InheritanceMetricsPhase
                        cmetrics.clear
                        cmetrics.collect(mclasses)
                        cmetrics.to_console(1, not toolcontext.opt_nocolors.value)
+                       if csv then cmetrics.to_csv.save("{out}/summary_classes.csv")
                        hmetrics.clear
                        hmetrics.collect(mmodules)
                        hmetrics.to_console(1, not toolcontext.opt_nocolors.value)
+                       if csv then hmetrics.to_csv.save("{out}/summary_inheritance.csv")
                end
        end
 end