Nitsmell : Adding new code smells and print console updated
[nit.git] / src / metrics / method_analyze_metrics.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 # This module call visitor for get number of line, total attributs call and total of self attributes call
15
16 module method_analyze_metrics
17
18 # We usualy need specific phases
19 # NOTE: `frontend` is sufficent in most case (it is often too much)
20 import nitsmell_toolcontext
21 import mclassdef_collect
22
23
24 fun call_analyze_methods(mclassdef: MClassDef, model_builder: ModelBuilder): Array[MMethodDef] do
25 var mmethoddefs = new Array[MMethodDef]
26 for m_prop in mclassdef.collect_intro_and_redef_mpropdefs(model_builder.model.private_view) do
27 var n_prop = model_builder.mpropdef2node(m_prop)
28 #Check if the property is a method definition
29 if n_prop isa AMethPropdef and m_prop isa MMethodDef then
30 if n_prop.n_methid isa AIdMethid then
31 #Call visitor to analyse the method
32 var visitor = new MethodAnalyzeMetrics(n_prop)
33 visitor.enter_visit(n_prop)
34 mmethoddefs.add(set_analyse_result_methoddef(m_prop,visitor))
35 end
36 end
37 end
38 return mmethoddefs
39 end
40
41 fun set_analyse_result_methoddef(mmethoddef: MMethodDef, visitor: MethodAnalyzeMetrics): MMethodDef do
42 mmethoddef.line_number = visitor.line_number.length
43 mmethoddef.total_self_call = visitor.total_self_call
44 mmethoddef.class_call = visitor.class_call
45 return mmethoddef
46 end
47
48 public class MethodAnalyzeMetrics
49 super Visitor
50 var ameth_prop_def: AMethPropdef
51 var line_number = new Counter[nullable Int]
52 var total_self_call = 0
53 var class_call = new Counter[MClassType]
54
55 redef fun visit(n) do
56 n.visit_all(self)
57 if n isa AExpr then
58 if not n isa ABlockExpr then
59 if n.first_location != null then
60 line_number.inc(n.first_location.line_start)
61 end
62 end
63 end
64 if n isa ASendExpr then
65 var callsite = n.callsite
66 if callsite != null then
67 var class_site_recv = callsite.recv
68 if class_site_recv isa MClassType then class_call.inc(class_site_recv)
69 if callsite.recv_is_self then self.total_self_call += 1
70 end
71 end
72 end
73 end
74
75 redef class MMethodDef
76 var line_number = 0
77 var total_self_call = 0
78 var class_call = new Counter[MClassType]
79 end