Skip to content

Commit

Permalink
Fix code quality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource committed Apr 22, 2024
1 parent 6bbd57d commit 3542373
Showing 1 changed file with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,29 @@ protected override void Initialize(SonarAnalysisContext context) =>
var type = (INamedTypeSymbol)symbolStart.Symbol;
if (type.IsControllerType())
{
symbolStart.RegisterSyntaxNodeAction(nodeContext =>
{
if (nodeContext.SemanticModel.GetDeclaredSymbol(nodeContext.Node) is IMethodSymbol method
&& method.IsControllerMethod())
{
var actionParameters = method.Parameters
.Select(x => x.Type)
.OfType<INamedTypeSymbol>()
.Where(x => !IgnoreType(x))
.SelectMany(x => RelatedTypesToExamine(x))
.Distinct();
foreach (var parameter in actionParameters)
{
CheckInvalidProperties(parameter, nodeContext, examinedTypes);
}
}
}, SyntaxKind.MethodDeclaration);
symbolStart.RegisterSyntaxNodeAction(nodeContext => ProcessControllerMethods(nodeContext, examinedTypes), SyntaxKind.MethodDeclaration);
}
}, SymbolKind.NamedType);
});

private static void ProcessControllerMethods(SonarSyntaxNodeReportingContext context, ConcurrentDictionary<ITypeSymbol, bool> examinedTypes)
{
if (context.SemanticModel.GetDeclaredSymbol(context.Node) is IMethodSymbol method
&& method.IsControllerMethod())
{
var actionParameters = method.Parameters
.Select(x => x.Type)
.OfType<INamedTypeSymbol>()
.Where(x => !IgnoreType(x))
.SelectMany(x => RelatedTypesToExamine(x))
.Distinct();
foreach (var parameter in actionParameters)
{
CheckInvalidProperties(parameter, context, examinedTypes);
}
}
}

private static void CheckInvalidProperties(INamedTypeSymbol parameterType, SonarSyntaxNodeReportingContext context, ConcurrentDictionary<ITypeSymbol, bool> examinedTypes)
{
var invalidProperties = GetAllDeclaredProperties(parameterType, examinedTypes)
Expand All @@ -101,13 +103,13 @@ private static void CheckInvalidProperties(INamedTypeSymbol parameterType, Sonar

private static bool IgnoreType(ITypeSymbol type) =>
type.IsAny(IgnoredTypes)
|| !type.DeclaringSyntaxReferences.Any()
|| (!type.DeclaringSyntaxReferences.Any()
&& !type.Is(KnownType.System_Collections_Generic_IEnumerable_T)
&& !type.Implements(KnownType.System_Collections_Generic_IEnumerable_T);
&& !type.Implements(KnownType.System_Collections_Generic_IEnumerable_T));

private static bool CanBeNull(ITypeSymbol type) =>
type is ITypeParameterSymbol { HasValueTypeConstraint: false }
|| type.IsReferenceType && type.NullableAnnotation() != NullableAnnotation.NotAnnotated
|| (type.IsReferenceType && type.NullableAnnotation() != NullableAnnotation.NotAnnotated)
|| type.IsNullableValueType();

private static IEnumerable<IPropertySymbol> GetAllDeclaredProperties(ITypeSymbol type, ConcurrentDictionary<ITypeSymbol, bool> examinedTypes)
Expand Down

0 comments on commit 3542373

Please sign in to comment.