2
// CodeMetricsServices.cs
5
// nikhil <${AuthorEmail}>
7
// Copyright (c) 2009 nikhil
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
32
using MonoDevelop.Core;
34
using MonoDevelop.Ide.Gui;
35
using MonoDevelop.Projects;
36
using MonoDevelop.Projects.Dom;
37
using MonoDevelop.Projects.Dom.Parser;
38
using MonoDevelop.Projects.Dom.Output;
40
using Mono.TextEditor;
42
using ICSharpCode.NRefactory.Ast;
43
using ICSharpCode.NRefactory.AstBuilder;
45
namespace MonoDevelop.CodeMetrics
47
public class CodeMetricsService
49
public static void AddTypes (ProjectProperties projectprop, MetricsContext ctx)
51
ProjectDom dom = ProjectDomService.GetProjectDom (projectprop.Project);
52
foreach (IType ob in dom.Types) {
53
projectprop.AddInstance(ob);
57
internal static void ProcessInnerTypes(ProjectProperties projprop)
59
foreach (var namesp in projprop.Namespaces) {
60
namesp.Value.ProcessClasses();
61
projprop.CyclometricComplexity += namesp.Value.CyclometricComplexity;
62
projprop.LOCReal += namesp.Value.LOCReal;
63
projprop.LOCComments += namesp.Value.LOCComments;
65
foreach (var cls in projprop.Classes) {
66
cls.Value.ProcessInnerClasses();
67
projprop.CyclometricComplexity += cls.Value.CyclometricComplexity;
68
projprop.LOCReal += cls.Value.LOCReal;
69
projprop.LOCComments += cls.Value.LOCComments;
73
public static string GenerateAssemblyMetricText()
75
// TODO General stuff about the assembly
79
public static string GenerateTypeMetricText(IProperties item)
81
if(item is NamespaceProperties){
83
return GenerateNamespaceMetricText((NamespaceProperties)item).ToString();
85
} else if (item is MethodProperties) {
87
return GenerateMethodMetricText((MethodProperties)item).ToString();
89
} else if (item is ClassProperties) {
91
return GenerateClassMetricText((ClassProperties)item).ToString();
93
} else if (item is InterfaceProperties) {
95
return GenerateInterfaceMetricText((InterfaceProperties)item).ToString();
97
} else if (item is EnumProperties) {
99
return GenerateEnumMetricText((EnumProperties)item).ToString();
101
} else if (item is DelegateProperties) {
103
return GenerateDelegateMetricText((DelegateProperties)item).ToString();
105
} else if (item is StructProperties) {
107
return GenerateStructMetricText((StructProperties)item).ToString();
114
private static StringBuilder GenerateNamespaceMetricText(NamespaceProperties item)
116
StringBuilder results = new StringBuilder();
117
results.Append(GettextCatalog.GetString("\nName : ") + item.FullName);
118
results.Append(GettextCatalog.GetString("\nTotal number of classes : ") + item.Classes.Count);
119
results.Append(GettextCatalog.GetString("\nTotal number of methods : ") + item.MethodCount);
120
results.Append(GettextCatalog.GetString("\nTotal number of fields : ") + item.FieldCount);
121
results.Append(GettextCatalog.GetString("\nClass Coupling : ") + item.ClassCoupling);
125
private static StringBuilder GenerateClassMetricText(ClassProperties item)
127
StringBuilder results = new StringBuilder();
128
results.Append(GettextCatalog.GetString("\nName : ") + item.FullName);
129
results.Append(GettextCatalog.GetString("\nDepth of inheritance : ") + item.DepthOfInheritance);
130
results.Append(GettextCatalog.GetString("\nNumber of children : ") + item.FanOut);
131
results.Append(GettextCatalog.GetString("\nAfferent Coupling : ") + item.AfferentCoupling);
132
results.Append(GettextCatalog.GetString("\nEfferent Coupling : ") + item.EfferentCoupling);
133
results.Append(GettextCatalog.GetString("\nData abstraction coupling : ") + item.DataAbstractionCoupling);
134
results.Append(GettextCatalog.GetString("\nConstructors : ") + item.ConstructorCount);
135
results.Append(GettextCatalog.GetString("\nDelegates : ") + item.DelegateCount);
136
results.Append(GettextCatalog.GetString("\nEvents : ") + item.EventCount);
137
results.Append(GettextCatalog.GetString("\nFields : ") + item.FieldCount);
138
results.Append(GettextCatalog.GetString("\nInner classes : ") + item.InnerClassCount);
139
results.Append(GettextCatalog.GetString("\nStructs : ") + item.StructCount);
140
results.Append(GettextCatalog.GetString("\nMethods : ") + item.MethodCount);
141
results.Append(GettextCatalog.GetString("\nProperties : ") + item.Class.PropertyCount);
142
results.Append(GettextCatalog.GetString("\nLack of cohesion of methods : ") + item.LCOM);
143
results.Append(GettextCatalog.GetString("\nLack of cohesion of methods (Henderson-Sellers) : ") + item.LCOM_HS);
147
private static StringBuilder GenerateMethodMetricText(MethodProperties item)
149
StringBuilder results = new StringBuilder();
150
results.Append(GettextCatalog.GetString("\nName : " + item.FullName));
151
results.Append(GettextCatalog.GetString("\nTotal number of local variables : " + item.NumberOfVariables));
152
results.Append(GettextCatalog.GetString("\nTotal number of parameters : " + item.ParameterCount));
153
results.Append(GettextCatalog.GetString("\nAfferent Coupling : " + item.AfferentCoupling));
154
results.Append(GettextCatalog.GetString("\nEfferent Coupling : " + item.EfferentCoupling));
159
private static StringBuilder GenerateStructMetricText (StructProperties item)
161
StringBuilder results = new StringBuilder();
162
results.Append(GettextCatalog.GetString("\nName : " + item.FullName));
163
results.Append(GettextCatalog.GetString("\nTotal number of fields : " + item.Struct.FieldCount));
164
results.Append(GettextCatalog.GetString("\nTotal number of properties : " + item.Struct.PropertyCount));
168
private static StringBuilder GenerateInterfaceMetricText (InterfaceProperties item)
170
StringBuilder results = new StringBuilder();
171
results.Append(GettextCatalog.GetString("\nName : " + item.FullName));
172
results.Append(GettextCatalog.GetString("\nTotal number of properties : " + item.Interface.PropertyCount));
173
results.Append(GettextCatalog.GetString("\nTotal number of methods : " + item.Interface.MethodCount));
177
private static StringBuilder GenerateDelegateMetricText (DelegateProperties item)
179
StringBuilder results = new StringBuilder();
180
results.Append(GettextCatalog.GetString("\nName : " + item.FullName));
181
results.Append(GettextCatalog.GetString("\nReturn Type : " + item.Delegate.ReturnType));
182
results.Append(GettextCatalog.GetString("\nTotal number of parameters : " + item.Delegate.Parameters.Count));
186
private static StringBuilder GenerateEnumMetricText (EnumProperties item)
188
StringBuilder results = new StringBuilder();
189
results.Append(GettextCatalog.GetString("\nName : " + item.FullName));
190
results.Append(GettextCatalog.GetString("\nTotal number of inner types : " + item.Enum.InnerTypeCount));