Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Jan 22, 2025
1 parent fd5e699 commit d909085
Show file tree
Hide file tree
Showing 27 changed files with 71 additions and 71 deletions.
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/ApiInvocationProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,8 @@ private void VisitUsingDirectives(
{
var namespacesSyntaxWalker = new NamespacesSyntaxWalker(semanticModel, semantic);
namespacesSyntaxWalker.Visit(node);
var namespaces = namespacesSyntaxWalker.ToArray();
if (namespaces.Any())
var namespaces = namespacesSyntaxWalker.ToList();
if (namespaces.Count > 0)
{
metadataVisitor.VisitUsingDirectives(new MdUsingDirectives(namespaces.ToImmutableArray(), ImmutableArray<string>.Empty));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private IReadOnlyList<AttributeData> GetAttributes(ISymbol member, INamedTypeSym

return symbolNames.GetGlobalName(unboundTypeSymbol) == symbolNames.GetGlobalName(attributeType);
})
.ToArray();
.ToList();

private static INamedTypeSymbol? GetUnboundTypeSymbol(INamedTypeSymbol? typeSymbol) =>
typeSymbol is null
Expand Down
6 changes: 3 additions & 3 deletions src/Pure.DI.Core/Core/Code/ApiMembersBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CompositionCode Build(CompositionCode composition)
membersCounter++;

apiCode.AppendLine();
var resolvers = resolversBuilder.Build(new RootContext(composition.Source.Source, composition.Roots)).ToArray();
var resolvers = resolversBuilder.Build(new RootContext(composition.Source.Source, composition.Roots)).ToList();
if (isCommentsEnabled)
{
apiCode.AppendLine("/// <summary>");
Expand All @@ -84,7 +84,7 @@ public CompositionCode Build(CompositionCode composition)

membersCounter++;

if (resolvers.Length > 0)
if (resolvers.Count > 0)
{
apiCode.AppendLine();
CreateObjectConflictsResolverMethod($"{Names.SystemNamespace}Type type",
Expand Down Expand Up @@ -119,7 +119,7 @@ public CompositionCode Build(CompositionCode composition)

membersCounter++;

if (resolvers.Length > 0)
if (resolvers.Count > 0)
{
apiCode.AppendLine();
CreateObjectConflictsResolverMethod($"{Names.SystemNamespace}Type type, object{nullable} tag",
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/ArgFieldsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ internal sealed class ArgFieldsBuilder(ITypeResolver typeResolver)
{
public CompositionCode Build(CompositionCode composition)
{
var classArgs = composition.Args.GetArgsOfKind(ArgKind.Class).ToArray();
if (!classArgs.Any())
var classArgs = composition.Args.GetArgsOfKind(ArgKind.Class).ToList();
if (classArgs.Count == 0)
{
return composition;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/ClassCommenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public void AddComments(CompositionCode composition, Unit unit)
.OrderByDescending(root => root.IsPublic)
.ThenBy(root => root.DisplayName)
.ThenBy(root => root.Node.Binding)
.ToArray();
.ToList();

if (orderedRoots.Length > 0)
if (orderedRoots.Count > 0)
{
var rootComments = comments.FormatList(
"<b>Composition roots:</b>",
Expand Down
6 changes: 3 additions & 3 deletions src/Pure.DI.Core/Core/Code/ClassDiagramBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public LinesBuilder Build(CompositionCode composition)
var hasResolveMethods = composition.Source.Source.Hints.IsResolveEnabled;
var rootProperties = composition.Roots.ToDictionary(i => i.Injection, i => i);
var compositionLines = new LinesBuilder();
if (hasResolveMethods || rootProperties.Any())
if (hasResolveMethods || rootProperties.Count > 0)
{
compositionLines.AppendLine($"class {composition.Source.Source.Name.ClassName} {{");
using (lines.Indent())
Expand Down Expand Up @@ -134,8 +134,8 @@ public LinesBuilder Build(CompositionCode composition)

if (dependency.Source.Arg is { } arg)
{
var tags = arg.Binding.Contracts.SelectMany(i => i.Tags.Select(tag => tag.Value)).ToArray();
lines.AppendLine($"{targetType} o-- {sourceType} : {(tags.Any() ? FormatTags(tags) + " " : "")}Argument \\\"{arg.Source.ArgName}\\\"");
var tags = arg.Binding.Contracts.SelectMany(i => i.Tags.Select(tag => tag.Value)).ToList();
lines.AppendLine($"{targetType} o-- {sourceType} : {(tags.Count > 0 ? FormatTags(tags) + " " : "")}Argument \\\"{arg.Source.ArgName}\\\"");
}
else
{
Expand Down
17 changes: 8 additions & 9 deletions src/Pure.DI.Core/Core/Code/Comments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ public IEnumerable<string> FilterHints(IEnumerable<string> comments) =>

public IHints GetHints(IEnumerable<string> comments)
{
var hints = (
from comment in comments
select HintCommentRegex.Match(comment)
into match
where match.Success
select match)
.ToArray();
var hints =
from comment in comments
select HintCommentRegex.Match(comment)
into match
where match.Success
select match;

var result = new Hints();
foreach (var hint in hints)
Expand All @@ -49,8 +48,8 @@ where match.Success

public IEnumerable<string> Format(IEnumerable<string> comments, bool escape)
{
var allComments = comments.ToArray();
var count = allComments.Length;
var allComments = comments.ToList();
var count = allComments.Count;
for (var i = 0; i < count; i++)
{
var comment = allComments[i];
Expand Down
12 changes: 6 additions & 6 deletions src/Pure.DI.Core/Core/Code/CompositionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public CompositionCode Build(DependencyGraph graph)
}

var singletons = map.GetSingletons().ToImmutableArray();
var totalDisposables = singletons.Where(i => nodeInfo.IsDisposableAny(i.Node)).ToArray();
var disposables = singletons.Where(i => nodeInfo.IsDisposable(i.Node)).ToArray();
var asyncDisposables = singletons.Where(i => nodeInfo.IsAsyncDisposable(i.Node)).ToArray();
var totalDisposables = singletons.Where(i => nodeInfo.IsDisposableAny(i.Node)).ToList();
var disposables = singletons.Where(i => nodeInfo.IsDisposable(i.Node)).ToList();
var asyncDisposables = singletons.Where(i => nodeInfo.IsAsyncDisposable(i.Node)).ToList();
var publicRoots = roots
.OrderByDescending(i => i.IsPublic)
.ThenBy(i => i.Node.Binding.Id)
Expand All @@ -95,9 +95,9 @@ public CompositionCode Build(DependencyGraph graph)
singletons,
GetRootArgs(allArgs).ToImmutableArray(),
publicRoots,
totalDisposables.Length,
disposables.Length,
asyncDisposables.Length,
totalDisposables.Count,
disposables.Count,
asyncDisposables.Count,
totalDisposables.Count(i => i.Node.Lifetime == Lifetime.Scoped),
isThreadSafe,
ImmutableArray<Line>.Empty);
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/ConstructCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ private void BuildArray(BuildContext ctx, in DpConstruct array)
{
var variable = ctx.Variable;
var instantiation = $"new {typeResolver.Resolve(ctx.DependencyGraph.Source, array.Source.ElementType)}[{variable.Args.Count.ToString()}] {{ {string.Join(", ", variable.Args.Select(i => ctx.BuildTools.OnInjected(ctx, i.Current)))} }}";
var onCreated = ctx.BuildTools.OnCreated(ctx, variable).ToArray();
if (onCreated.Any())
var onCreated = ctx.BuildTools.OnCreated(ctx, variable).ToList();
if (onCreated.Count > 0)
{
ctx.Code.AppendLine($"{ctx.BuildTools.GetDeclaration(variable)}{variable.VariableName} = {instantiation};");
ctx.Code.AppendLines(onCreated);
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/DefaultConstructorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal sealed class DefaultConstructorBuilder(
{
public CompositionCode Build(CompositionCode composition)
{
if (composition.Args.Any())
if (composition.Args.Length > 0)
{
return composition;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Pure.DI.Core/Core/Code/ImplementationCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public void Build(BuildContext ctx, in DpImplementation implementation)
void VisitMethodAction(BuildContext context) => injections.MethodInjection(variable.VariableName, context, method, methodArgs);
}

var onCreatedStatements = ctx.BuildTools.OnCreated(ctx, ctx.Variable).ToArray();
var onCreatedStatements = ctx.BuildTools.OnCreated(ctx, ctx.Variable).ToList();
var hasOnCreatedStatements = ctx.BuildTools.OnCreated(ctx, variable).Any();
var hasAlternativeInjections = visits.Any();
var hasAlternativeInjections = visits.Count > 0;
var tempVariableInit =
ctx.DependencyGraph.Source.Hints.IsThreadSafeEnabled
&& ctx.Variable.Node.Lifetime is not Lifetime.Transient and not Lifetime.PerBlock
Expand All @@ -72,9 +72,9 @@ public void Build(BuildContext ctx, in DpImplementation implementation)
{
ctx = ctx with { Variable = variable with { NameOverride = variable.VariableDeclarationName + "Temp" } };
ctx.Code.AppendLine($"{typeResolver.Resolve(ctx.Variable.Setup, ctx.Variable.InstanceType)} {ctx.Variable.VariableDeclarationName};");
if (onCreatedStatements.Any())
if (onCreatedStatements.Count > 0)
{
onCreatedStatements = ctx.BuildTools.OnCreated(ctx, ctx.Variable).ToArray();
onCreatedStatements = ctx.BuildTools.OnCreated(ctx, ctx.Variable).ToList();
}
}

Expand Down Expand Up @@ -117,17 +117,17 @@ private string CreateInstantiation(
var variable = ctx.Variable;
var required = requiredFields.Select(i => (Variable: i.RequiredVariable, i.RequiredField.Field.Name))
.Concat(requiredProperties.Select(i => (Variable: i.RequiredVariable, i.RequiredProperty.Property.Name)))
.ToArray();
.ToList();

var args = string.Join(", ", constructorArgs.Select(i => ctx.BuildTools.OnInjected(ctx, i)));
code.Append(variable.InstanceType.IsTupleType ? $"({args})" : $"new {typeResolver.Resolve(variable.Setup, variable.InstanceType)}({args})");
if (required.Any())
if (required.Count > 0)
{
code.Append(" { ");
for (var index = 0; index < required.Length; index++)
for (var index = 0; index < required.Count; index++)
{
var (v, name) = required[index];
code.Append($"{name} = {ctx.BuildTools.OnInjected(ctx, v)}{(index < required.Length - 1 ? ", " : "")}");
code.Append($"{name} = {ctx.BuildTools.OnInjected(ctx, v)}{(index < required.Count - 1 ? ", " : "")}");
}

code.Append(" }");
Expand Down
6 changes: 3 additions & 3 deletions src/Pure.DI.Core/Core/Code/ParameterizedConstructorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ internal sealed class ParameterizedConstructorBuilder(
{
public CompositionCode Build(CompositionCode composition)
{
if (!composition.Args.Any())
if (composition.Args.Length == 0)
{
return composition;
}

var code = composition.Code;
var membersCounter = composition.MembersCount;
if (!composition.Args.Any())
if (composition.Args.Length == 0)
{
return composition;
}

constructorCommenter.AddComments(composition, Unit.Shared);

code.AppendLine($"[{Names.OrdinalAttributeName}(128)]");
var classArgs = composition.Args.GetArgsOfKind(ArgKind.Class).ToArray();
var classArgs = composition.Args.GetArgsOfKind(ArgKind.Class).ToList();
code.AppendLine($"public {composition.Source.Source.Name.ClassName}({string.Join(", ", classArgs.Select(arg => $"{typeResolver.Resolve(composition.Source.Source, arg.InstanceType)} {arg.Node.Arg?.Source.ArgName}"))})");
code.AppendLine("{");
using (code.Indent())
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/ResolverFieldsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public CompositionCode Build(CompositionCode composition)
return composition;
}

var resolvers = resolversBuilder.Build(new RootContext(composition.Source.Source, composition.Roots)).ToArray();
if (!resolvers.Any())
var resolvers = resolversBuilder.Build(new RootContext(composition.Source.Source, composition.Roots)).ToList();
if (resolvers.Count == 0)
{
return composition;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class RootMethodsBuilder(
{
public CompositionCode Build(CompositionCode composition)
{
if (!composition.Roots.Any())
if (composition.Roots.Length == 0)
{
return composition;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/ScopeConstructorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public CompositionCode Build(CompositionCode composition)
using (code.Indent())
{
code.AppendLine($"{Names.RootFieldName} = ({Names.ParentScopeArgName} ?? throw new {Names.SystemNamespace}ArgumentNullException(nameof({Names.ParentScopeArgName}))).{Names.RootFieldName};");
var classArgs = composition.Args.GetArgsOfKind(ArgKind.Class).ToArray();
if (classArgs.Any())
var classArgs = composition.Args.GetArgsOfKind(ArgKind.Class).ToList();
if (classArgs.Count > 0)
{
foreach (var argsField in classArgs)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Pure.DI.Core/Core/Code/StaticConstructorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public CompositionCode Build(CompositionCode composition)
membersCounter++;
}

var resolvers = resolversBuilder.Build(new RootContext(composition.Source.Source, composition.Roots)).ToArray();
if (!resolvers.Any())
var resolvers = resolversBuilder.Build(new RootContext(composition.Source.Source, composition.Roots)).ToList();
if (resolvers.Count == 0)
{
return composition;
}
Expand All @@ -50,7 +50,7 @@ public CompositionCode Build(CompositionCode composition)
code.AppendLine($"{Names.ResolverClassName}<{typeResolver.Resolve(composition.Source.Source, resolver.Type)}>.{Names.ResolverPropertyName} = val{className};");
}

var divisor = Buckets<object, object>.GetDivisor((uint)resolvers.Length);
var divisor = Buckets<object, object>.GetDivisor((uint)resolvers.Count);
var pairs = $"{Names.SystemNamespace}Type, {Names.IResolverTypeName}<{composition.Source.Source.Name.ClassName}, object>";
var bucketsTypeName = $"{Names.ApiNamespace}Buckets<{pairs}>";
var pairTypeName = $"{Names.ApiNamespace}Pair<{pairs}>";
Expand All @@ -59,7 +59,7 @@ public CompositionCode Build(CompositionCode composition)
{
code.AppendLine($"{divisor.ToString()},");
code.AppendLine($"out {Names.BucketSizeFieldName},");
code.AppendLine($"new {pairTypeName}[{resolvers.Length.ToString()}]");
code.AppendLine($"new {pairTypeName}[{resolvers.Count.ToString()}]");
code.AppendLine("{");
using (code.Indent())
{
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/UsingDeclarationsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal sealed class UsingDeclarationsBuilder : IBuilder<CompositionCode, Compo
public CompositionCode Build(CompositionCode composition)
{
var code = composition.Code;
if (!composition.Source.Source.UsingDirectives.Any())
if (composition.Source.Source.UsingDirectives.Length == 0)
{
return composition;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/VariablesMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void Reset()
var classBindings = this
.Where(i => i.Value.Node.Arg is not null || i.Value.Node.Lifetime is not (Lifetime.Singleton or Lifetime.Scoped))
.Select(i => i.Key)
.ToArray();
.ToList();

foreach (var singletonBinding in classBindings)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/ContractsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ISet<Injection> Build(ContractsBuildContext context)
contractTags.Add(context.ContextTag);
}

if (!contractTags.Any())
if (contractTags.Count == 0)
{
contractTags.Add(null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/DependenciesToLinesWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override void VisitConstructor(in Unit ctx, in DpMethod constructor)
public override void VisitMethod(in Unit ctx, in DpMethod method)
{
var typeArgs = "";
if (method.Method.ContainingType.TypeArguments.Any())
if (method.Method.ContainingType.TypeArguments.Length > 0)
{
typeArgs = $"<{string.Join(", ", method.Method.ContainingType.TypeArguments)}>";
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pure.DI.Core/Core/DependencyGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public IEnumerable<DependencyNode> TryBuild(
if (injection.Type is { IsAbstract: false, SpecialType: Microsoft.CodeAnalysis.SpecialType.None })
{
var autoBinding = CreateAutoBinding(setup, targetNode, injection, ++maxId);
return CreateNodes(setup, typeConstructor, autoBinding).ToArray();
return CreateNodes(setup, typeConstructor, autoBinding).ToList();
}

// OnCannotResolve
Expand All @@ -323,7 +323,7 @@ public IEnumerable<DependencyNode> TryBuild(
map[item.Key] = item.Value;
}

foreach (var key in map.Keys.Where(i => ReferenceEquals(i.Tag, MdTag.ContextTag)).ToArray())
foreach (var key in map.Keys.Where(i => ReferenceEquals(i.Tag, MdTag.ContextTag)).ToList())
{
map.Remove(key);
}
Expand Down Expand Up @@ -569,8 +569,8 @@ private MdBinding CreateConstructBinding(
var contracts = new HashSet<Injection>();
foreach (var nestedBinding in setup.Bindings.Where(i => i != targetNode.Binding))
{
var matchedContracts = GetMatchedMdContracts(setup, targetNode.Binding.SemanticModel.Compilation, elementType, nestedBinding).ToArray();
if (matchedContracts.Length == 0)
var matchedContracts = GetMatchedMdContracts(setup, targetNode.Binding.SemanticModel.Compilation, elementType, nestedBinding).ToList();
if (matchedContracts.Count == 0)
{
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pure.DI.Core/Core/ImplementationDependencyNodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public IEnumerable<DependencyNode> Build(DependencyNodeBuildContext ctx)
instanceDpProvider.GetParameters(setup, constructor.Parameters, compilation, ctx.TypeConstructor)));
}

if (!constructors.Any())
if (constructors.Count == 0)
{
throw new CompileErrorException($"The instance of {implementationType} cannot be instantiated due to no accessible constructor available.", implementation.Source.GetLocation(), LogId.ErrorInvalidMetadata);
}
Expand All @@ -69,13 +69,13 @@ public IEnumerable<DependencyNode> Build(DependencyNodeBuildContext ctx)
instanceDp.Methods,
instanceDp.Properties,
instanceDp.Fields))
.ToArray();
.ToList();

var implementationsWithOrdinal = implementations
.Where(i => i.Constructor.Ordinal.HasValue)
.ToArray();
.ToList();

if (implementationsWithOrdinal.Any())
if (implementationsWithOrdinal.Count > 0)
{
foreach (var node in CreateNodes(injectionsWalker, implementationsWithOrdinal.OrderBy(i => i.Constructor.Ordinal)))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/ImplementationVariantsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public IEnumerable<DpImplementation> Build(DpImplementation implementation)
implementation.Methods.Select(method => CreateVariants(method, ImplementationVariantKind.Method))
.Concat(Enumerable.Repeat(CreateVariants(implementation.Constructor, ImplementationVariantKind.Ctor), 1))
.Select(i => new SafeEnumerator<ImplementationVariant>(i.GetEnumerator()))
.ToArray();
.ToList();

try
{
Expand Down
Loading

0 comments on commit d909085

Please sign in to comment.