src: Update init
[nit.git] / src / metrics / static_types_metrics.nit
index 3f28cee..4dd38d0 100644 (file)
 # Metrics on the usage of explicit static types.
 module static_types_metrics
 
-private import metrics_base
-import modelbuilder
-import modelize_class
-import frontend
+import metrics_base
+import modelize
 
 redef class ToolContext
-       var static_types_metrics_phase = new StaticTypesMetricsPhase(self, null)
+       var static_types_metrics_phase: Phase = new StaticTypesMetricsPhase(self, null)
 end
 
 private class StaticTypesMetricsPhase
        super Phase
-       redef fun process_mainmodule(mainmodule)
+       redef fun process_mainmodule(mainmodule, given_mmodules)
        do
                if not toolcontext.opt_static_types.value and not toolcontext.opt_all.value then return
                compute_static_types_metrics(toolcontext.modelbuilder)
@@ -43,18 +41,14 @@ private class ATypeCounterVisitor
 
        var typecount: Counter[MType]
 
-       # Get a new visitor on a classef to add type count in `typecount'.
-       init(modelbuilder: ModelBuilder, nclassdef: AClassdef, typecount: Counter[MType])
-       do
-               self.modelbuilder = modelbuilder
-               self.nclassdef = nclassdef
-               self.typecount = typecount
-       end
-
        redef fun visit(n)
        do
-               if n isa AType then
-                       var mtype = modelbuilder.resolve_mtype(self.nclassdef, n)
+               if n isa AAnnotation then return
+
+               if n isa AType then do
+                       var mclassdef = self.nclassdef.mclassdef
+                       if mclassdef == null then break
+                       var mtype = modelbuilder.resolve_mtype(mclassdef, n)
                        if mtype != null then
                                self.typecount.inc(mtype)
                        end
@@ -79,35 +73,10 @@ do
 
        # Display data
        print "--- Metrics of the explitic static types ---"
-       print "Total number of explicit static types: {typecount.total}"
-       if typecount.total == 0 then return
-
-       # types sorted by usage
-       var types = typecount.sort
+       print "Total number of explicit static types: {typecount.sum}"
+       if typecount.sum == 0 then return
 
-       # Display most used types (ie the last of `types')
-       print "Most used types: "
-       var min = 10
-       if types.length < min then min = types.length
-       for i in [0..min[ do
-               var t = types[types.length-i-1]
-               print "  {t}: {typecount[t]}"
-       end
-
-       # Compute the distribution of type usage
-       print "Distribution of type usage:"
-       var count = 0
-       var sum = 0
-       var limit = 1
-       for t in types do
-               if typecount[t] > limit then
-                       print "  <={limit}: {count} ({div(count*100,types.length)}% of types; {div(sum*100,typecount.total)}% of usage)"
-                       count = 0
-                       sum = 0
-                       while typecount[t] > limit do limit = limit * 2
-               end
-               count += 1
-               sum += typecount[t]
-       end
-       print "  <={limit}: {count} ({div(count*100,types.length)}% of types; {div(sum*100,typecount.total)}% of usage)"
+       print "Statistics of type usage:"
+       typecount.print_summary
+       typecount.print_elements(10)
 end