1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
16 module method_analyze_metrics
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
24 fun call_analyze_methods
(mclassdef
: MClassDef, model_builder
: ModelBuilder, view
: ModelView): Array[MMethodDef] do
25 var mmethoddefs
= new Array[MMethodDef]
26 for m_prop
in mclassdef
.collect_intro_and_redef_mpropdefs
(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
))
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
48 public
class MethodAnalyzeMetrics
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]
58 if not n
isa ABlockExpr then
59 if n
.first_location
!= null then
60 line_number
.inc
(n
.first_location
.line_start
)
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
75 redef class MMethodDef
77 var total_self_call
= 0
78 var class_call
= new Counter[MClassType]