@@ -74,6 +74,11 @@ private static string GetFullTypeName(ClassDeclarationSyntax classDeclaration)
7474 return classDeclaration . Identifier . Text ;
7575 }
7676
77+ private static string GetSimpleTypeName ( ClassDeclarationSyntax classDeclaration )
78+ {
79+ return classDeclaration . Identifier . Text ;
80+ }
81+
7782 public static void AnalyzeProjects ( IEnumerable < string > projectFiles )
7883 {
7984 foreach ( var projectFile in projectFiles )
@@ -104,15 +109,19 @@ public static void AnalyzeFile(string filePath, string sourceCode)
104109
105110 Parallel . ForEach ( classes , classDeclaration =>
106111 {
107- var className = GetFullTypeName ( classDeclaration ) ;
112+ var className = GetSimpleTypeName ( classDeclaration ) ;
108113 var dependencies = ExtractDependencies ( classDeclaration ) ;
109114
110115 foreach ( var dependency in dependencies )
111116 {
112117 var relation = new DependencyRelation
113118 {
114- SourceClass = GetFullTypeName ( classDeclaration ) ,
115- TargetClass = dependency ,
119+ SourceClass = GetSimpleTypeName ( classDeclaration ) ,
120+ SourceNamespace = classDeclaration . Ancestors ( ) . OfType < NamespaceDeclarationSyntax > ( ) . FirstOrDefault ( ) ? . Name . ToString ( ) ?? "" ,
121+ SourceAssembly = classDeclaration . SyntaxTree . GetRoot ( ) . DescendantNodes ( ) . OfType < CompilationUnitSyntax > ( ) . FirstOrDefault ( ) ? . Usings . FirstOrDefault ( ) ? . Name . ToString ( ) ?? "" ,
122+ TargetClass = dependency . Split ( '.' ) [ ( dependency . Split ( '.' ) . Length - 1 ) ] ,
123+ TargetNamespace = dependency . Contains ( "." ) ? dependency . Substring ( 0 , dependency . LastIndexOf ( '.' ) ) : "" ,
124+ TargetAssembly = dependency . Contains ( "." ) ? dependency . Substring ( 0 , dependency . LastIndexOf ( '.' ) ) : "" ,
116125 FilePath = filePath ,
117126 StartLine = classDeclaration . GetLocation ( ) . GetLineSpan ( ) . StartLinePosition . Line
118127 } ;
@@ -140,7 +149,7 @@ private static IEnumerable<string> ExtractDependencies(ClassDeclarationSyntax cl
140149 {
141150 var dependencies = new HashSet < string > ( ) ;
142151 var usings = GetUsingsWithCurrentNamespace ( classDeclaration ) ;
143-
152+
144153
145154 AnalyzeInheritance ( classDeclaration , usings , dependencies ) ;
146155 AnalyzeClassMembers ( classDeclaration , usings , dependencies ) ;
@@ -335,13 +344,47 @@ private static List<string> SplitGenericType(string typeName)
335344
336345 public static List < DependencyRelation > GetDependencies ( )
337346 {
338- return _dependencyMap . SelectMany ( kvp => kvp . Value ) . ToList ( ) ?? new List < DependencyRelation > ( ) ;
347+ var allDependencies = _dependencyMap . SelectMany ( kvp => kvp . Value ) . ToList ( ) ;
348+ CalculateDegrees ( allDependencies ) ;
349+ return allDependencies ;
350+ }
351+
352+ private static void CalculateDegrees ( List < DependencyRelation > dependencies )
353+ {
354+ var incomingDegrees = new ConcurrentDictionary < string , int > ( ) ;
355+ var outgoingDegrees = new ConcurrentDictionary < string , int > ( ) ;
356+
357+ // Calculate degrees
358+ foreach ( var dep in dependencies )
359+ {
360+ // Outgoing degree
361+ outgoingDegrees . AddOrUpdate (
362+ dep . SourceClass ,
363+ 1 ,
364+ ( key , count ) => count + 1
365+ ) ;
366+
367+ // Incoming degree
368+ incomingDegrees . AddOrUpdate (
369+ dep . TargetClass ,
370+ 1 ,
371+ ( key , count ) => count + 1
372+ ) ;
373+ }
374+
375+ // Update relations with calculated degrees
376+ foreach ( var dep in dependencies )
377+ {
378+ dep . OutgoingDegree = outgoingDegrees . GetValueOrDefault ( dep . SourceClass ) ;
379+ dep . IncomingDegree = incomingDegrees . GetValueOrDefault ( dep . TargetClass ) ;
380+ }
339381 }
340382
341383 public static void Clear ( )
342384 {
343385 _dependencyMap . Clear ( ) ;
344386 _solutionClasses . Clear ( ) ;
345387 }
388+
346389 }
347390}
0 commit comments