detectProblematicDependencies method

String detectProblematicDependencies(
  1. List<ZenScope> scopes
)

Generate a report of problematic dependencies

Implementation

String detectProblematicDependencies(List<ZenScope> scopes) {
  final buffer = StringBuffer();

  if (!ZenConfig.enableDependencyVisualization) {
    return 'Dependency detection is disabled. Enable it with ZenConfig.enableDependencyVisualization = true';
  }

  buffer.writeln('=== DEPENDENCY ANALYSIS ===\n');
  var problemFound = false;

  // Check for dependencies registered in multiple scopes
  final typeToScopes = <Type, List<ZenScope>>{};
  final instanceToScopes = <String, List<ZenScope>>{};

  for (final scope in scopes) {
    if (scope.isDisposed) continue;

    final deps = scope.findAllOfType<Object>();
    for (final dep in deps) {
      final type = dep.runtimeType;
      final instanceKey = '${type.toString()}_${dep.hashCode}';

      typeToScopes.putIfAbsent(type, () => []).add(scope);
      instanceToScopes.putIfAbsent(instanceKey, () => []).add(scope);
    }
  }

  // Report types registered in multiple scopes
  for (final entry in typeToScopes.entries) {
    if (entry.value.length > 1) {
      buffer.writeln(
          'MULTIPLE REGISTRATIONS: ${entry.key} is registered in multiple scopes:');
      for (final scope in entry.value) {
        buffer.writeln('  - ${scope.name ?? scope.id}');
      }
      problemFound = true;
      buffer.writeln();
    }
  }

  // Check for potential memory leaks (same instance in multiple scopes)
  for (final entry in instanceToScopes.entries) {
    if (entry.value.length > 1) {
      buffer.writeln(
          'POTENTIAL MEMORY LEAK: Same instance registered in multiple scopes:');
      for (final scope in entry.value) {
        buffer.writeln('  - ${scope.name ?? scope.id}');
      }
      problemFound = true;
      buffer.writeln();
    }
  }

  if (!problemFound) {
    buffer.writeln('No problematic dependencies detected');
  }

  buffer.writeln(
      '\nNOTE: Advanced circular dependency detection requires constructor');
  buffer.writeln(
      'dependency analysis, which is not available with the current scope API.');

  return buffer.toString();
}