From 6307f9fd207922f1eb88998e980b3abb04724c9c Mon Sep 17 00:00:00 2001 From: Blank Date: Tue, 3 Nov 2026 08:20:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(architecture-analyzers):=20=E6=8B=86?= =?UTF-8?q?=E5=88=86=E9=9D=99=E6=80=81=E7=BB=84=E5=90=88=E6=A0=B9=E5=88=86?= =?UTF-8?q?=E6=9E=90=E5=99=A8=E9=99=8D=E4=BD=8E=E8=AE=A4=E7=9F=A5=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SonarCloud S3776 CRITICAL:StaticCompositionRootAnalyzer.Initialize 认知复杂度 48 超阈值 15。等价拆分为七个私有静态辅助方法 (装配状态面注册/候选判定/成员上报/类型提取/reason 文案、 组合入口面注册/目标方法判定),Initialize 仅保留类型解析、 全空守卫与两路注册;符号面与操作面守卫不共享辅助方法, 注册条件与顺序、豁免集合、reason 文案、上报位置逐字不变, 公共 API 零变化。等价性经 38 例差分测试与既有 5 例回归验证。 Linear: GFX-1207 --- .../StaticCompositionRootAnalyzer.cs | 191 +++++++++++------- 1 file changed, 120 insertions(+), 71 deletions(-) diff --git a/GameFrameX.Architecture.Analyzers/Analyzers/Architecture/StaticCompositionRootAnalyzer.cs b/GameFrameX.Architecture.Analyzers/Analyzers/Architecture/StaticCompositionRootAnalyzer.cs index 03e2712b4..4550869cd 100644 --- a/GameFrameX.Architecture.Analyzers/Analyzers/Architecture/StaticCompositionRootAnalyzer.cs +++ b/GameFrameX.Architecture.Analyzers/Analyzers/Architecture/StaticCompositionRootAnalyzer.cs @@ -93,84 +93,133 @@ public override void Initialize(AnalysisContext context) return; } - var mechanismAssemblyName = MechanismAssemblyName; + RegisterStaticAssemblyStateAnalysis(compilationContext, runtimeType, hostType); - compilationContext.RegisterSymbolAction(symbolContext => + if (compositionType == null) { - var type = (INamedTypeSymbol)symbolContext.Symbol; - if (!type.IsStatic || type.TypeKind == TypeKind.Error || type.IsImplicitlyDeclared) - { - return; - } - - var assemblyName = type.ContainingAssembly.Identity.Name; - if (assemblyName == mechanismAssemblyName || ArchitectureSymbolFacts.ShouldIgnoreAssembly(assemblyName)) - { - return; - } - - foreach (var member in type.GetMembers()) - { - var memberType = member switch - { - IFieldSymbol field => field.Type, - IPropertySymbol property => property.Type, - _ => null, - }; - - if (memberType == null) - { - continue; - } - - if (SymbolEqualityComparer.Default.Equals(memberType, runtimeType)) - { - symbolContext.ReportDiagnostic(Diagnostic.Create(Rule, member.Locations[0], type.Name, assemblyName, "static member holding CapabilityRuntime")); - return; - } - - if (SymbolEqualityComparer.Default.Equals(memberType, hostType)) - { - symbolContext.ReportDiagnostic(Diagnostic.Create(Rule, member.Locations[0], type.Name, assemblyName, "static member holding CapabilityHost")); - return; - } - } - }, SymbolKind.NamedType); + return; + } - if (compositionType == null) + RegisterComposeEntryAnalysis(compilationContext, compositionType); + }); + } + + /// + /// 注册装配状态面分析:非豁免程序集的静态类若声明 CapabilityRuntime/CapabilityHost + /// 类型的静态字段或属性(含私有),按首个命中成员上报 GFX0022。 + /// + private static void RegisterStaticAssemblyStateAnalysis(CompilationStartAnalysisContext compilationContext, INamedTypeSymbol runtimeType, INamedTypeSymbol hostType) + { + compilationContext.RegisterSymbolAction(symbolContext => + { + var type = (INamedTypeSymbol)symbolContext.Symbol; + if (!IsReportableStaticType(type, out var assemblyName)) { return; } - compilationContext.RegisterOperationAction(operationContext => + ReportStaticAssemblyStateMembers(symbolContext, type, assemblyName, runtimeType, hostType); + }, SymbolKind.NamedType); + } + + /// 静态类候选判定:非误差/非隐式声明,且所在程序集不在机制与测试豁免集合内。 + private static bool IsReportableStaticType(INamedTypeSymbol type, out string assemblyName) + { + assemblyName = null; + if (!type.IsStatic || type.TypeKind == TypeKind.Error || type.IsImplicitlyDeclared) + { + return false; + } + + assemblyName = type.ContainingAssembly.Identity.Name; + return assemblyName != MechanismAssemblyName && !ArchitectureSymbolFacts.ShouldIgnoreAssembly(assemblyName); + } + + /// 逐成员检查装配状态:首个命中 CapabilityRuntime/CapabilityHost 的字段或属性上报后停止。 + private static void ReportStaticAssemblyStateMembers(SymbolAnalysisContext symbolContext, INamedTypeSymbol type, string assemblyName, INamedTypeSymbol runtimeType, INamedTypeSymbol hostType) + { + foreach (var member in type.GetMembers()) + { + var memberType = GetDeclaredMemberType(member); + if (memberType == null) + { + continue; + } + + var reason = DescribeAssemblyStateMemberType(memberType, runtimeType, hostType); + if (reason != null) { - var invocation = (Microsoft.CodeAnalysis.Operations.IInvocationOperation)operationContext.Operation; - var targetMethod = invocation.TargetMethod; - if (targetMethod == null || !string.Equals(targetMethod.Name, "Compose", System.StringComparison.Ordinal)) - { - return; - } - - var containingType = targetMethod.ContainingType; - if (containingType == null || !SymbolEqualityComparer.Default.Equals(containingType, compositionType)) - { - return; - } - - var callerType = operationContext.ContainingSymbol?.ContainingType; - if (callerType == null || !callerType.IsStatic || callerType.TypeKind == TypeKind.Error) - { - return; - } - - var assemblyName = callerType.ContainingAssembly.Identity.Name; - if (assemblyName == mechanismAssemblyName || ArchitectureSymbolFacts.ShouldIgnoreAssembly(assemblyName)) - { - return; - } - - operationContext.ReportDiagnostic(Diagnostic.Create(Rule, invocation.Syntax.GetLocation(), callerType.Name, assemblyName, "static entry invoking CapabilityComposition.Compose")); - }, OperationKind.Invocation); - }); + symbolContext.ReportDiagnostic(Diagnostic.Create(Rule, member.Locations[0], type.Name, assemblyName, reason)); + return; + } + } + } + + /// 提取字段/属性声明类型;其余成员种类返回 null。 + private static ITypeSymbol GetDeclaredMemberType(ISymbol member) + { + return member switch + { + IFieldSymbol field => field.Type, + IPropertySymbol property => property.Type, + _ => null, + }; + } + + /// 命中机制装配状态类型时给出诊断 reason 文案,否则返回 null。 + private static string DescribeAssemblyStateMemberType(ITypeSymbol memberType, INamedTypeSymbol runtimeType, INamedTypeSymbol hostType) + { + if (SymbolEqualityComparer.Default.Equals(memberType, runtimeType)) + { + return "static member holding CapabilityRuntime"; + } + + if (SymbolEqualityComparer.Default.Equals(memberType, hostType)) + { + return "static member holding CapabilityHost"; + } + + return null; + } + + /// + /// 注册组合入口面分析:非豁免程序集的静态类成员方法内调用 CapabilityComposition.Compose(两个重载均算) + /// 时按调用点上报 GFX0022。 + /// + private static void RegisterComposeEntryAnalysis(CompilationStartAnalysisContext compilationContext, INamedTypeSymbol compositionType) + { + compilationContext.RegisterOperationAction(operationContext => + { + var invocation = (Microsoft.CodeAnalysis.Operations.IInvocationOperation)operationContext.Operation; + if (!IsCapabilityCompositionEntryPoint(invocation.TargetMethod, compositionType)) + { + return; + } + + var callerType = operationContext.ContainingSymbol?.ContainingType; + if (callerType == null || !callerType.IsStatic || callerType.TypeKind == TypeKind.Error) + { + return; + } + + var assemblyName = callerType.ContainingAssembly.Identity.Name; + if (assemblyName == MechanismAssemblyName || ArchitectureSymbolFacts.ShouldIgnoreAssembly(assemblyName)) + { + return; + } + + operationContext.ReportDiagnostic(Diagnostic.Create(Rule, invocation.Syntax.GetLocation(), callerType.Name, assemblyName, "static entry invoking CapabilityComposition.Compose")); + }, OperationKind.Invocation); + } + + /// 目标方法判定:名为 Compose 且声明于 CapabilityComposition 类型(两个重载均命中)。 + private static bool IsCapabilityCompositionEntryPoint(IMethodSymbol targetMethod, INamedTypeSymbol compositionType) + { + if (targetMethod == null || !string.Equals(targetMethod.Name, "Compose", System.StringComparison.Ordinal)) + { + return false; + } + + return SymbolEqualityComparer.Default.Equals(targetMethod.ContainingType, compositionType); } }