Skip to content

Commit

Permalink
Fixing collection initialization warnings (IDE0028, IDE0300, IDE0301)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenedekFarkas committed Dec 12, 2024
1 parent 0889de7 commit 03bcabb
Show file tree
Hide file tree
Showing 45 changed files with 105 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public class OrderPart : ContentPart
/// <summary>
/// Gets the order's line items.
/// </summary>
public IList<OrderLineItem> LineItems { get; } = new List<OrderLineItem>();
public IList<OrderLineItem> LineItems { get; } = [];

/// <summary>
/// Gets additional costs that don't belong to an <see cref="OrderLineItem"/>, such as taxes and shipping.
/// </summary>
public IList<OrderAdditionalCost> AdditionalCosts { get; } = new List<OrderAdditionalCost>();
public IList<OrderAdditionalCost> AdditionalCosts { get; } = [];

/// <summary>
/// Gets the amounts charged for this order. Typically, a single credit card charge.
/// </summary>
public IList<Payment> Charges { get; } = new List<Payment>();
public IList<Payment> Charges { get; } = [];

public TextField Email { get; set; } = new();
public TextField Phone { get; set; } = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -46,7 +45,7 @@ public ShoppingCart()
/// </para>
/// </remarks>
public ShoppingCart(IEnumerable<ShoppingCartItem> items) =>
Items = new List<ShoppingCartItem>(items ?? Enumerable.Empty<ShoppingCartItem>());
Items = new List<ShoppingCartItem>(items ?? []);

/// <summary>
/// Initializes a new instance of the <see cref="ShoppingCart"/> class.
Expand All @@ -62,10 +61,6 @@ public ShoppingCart(params ShoppingCartItem[] items)
/// </summary>
/// <param name="items">The new list of items.</param>
/// <returns>A new shopping cart with all properties identical to this, but with a different list of items.</returns>
[SuppressMessage(
"Performance",
"CA1822",
Justification = $"Keep non-static in case {nameof(ShoppingCart)} gets additional properties in the future.")]
public ShoppingCart With(IEnumerable<ShoppingCartItem> items) => new(items);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ShoppingCartItem WithPrices(IEnumerable<PrioritizedPrice> prices) =>
/// <param name="price">The price to add.</param>
/// <returns>The new shopping cart item.</returns>
public ShoppingCartItem WithPrice(PrioritizedPrice price) =>
new(Quantity, ProductSku, Attributes, Prices.Concat(new[] { price }));
new(Quantity, ProductSku, Attributes, Prices.Concat([price]));

/// <summary>
/// Creates a new shopping cart item that is a clone of this, but with a different quantity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class ShoppingCartViewModel
public string Id { get; set; }

[JsonIgnore]
public IList<LocalizedHtmlString> InvalidReasons { get; } = new List<LocalizedHtmlString>();
public IList<LocalizedHtmlString> InvalidReasons { get; } = [];

[JsonIgnore]
public IList<LocalizedHtmlString> Headers { get; } = new List<LocalizedHtmlString>();
public IList<LocalizedHtmlString> Headers { get; } = [];

[JsonIgnore]
public IList<List<IShape>> TableShapes { get; } = new List<List<IShape>>();
public IList<ShoppingCartLineViewModel> Lines { get; } = new List<ShoppingCartLineViewModel>();
public IList<Amount> Totals { get; } = new List<Amount>();
public IList<List<IShape>> TableShapes { get; } = [];
public IList<ShoppingCartLineViewModel> Lines { get; } = [];
public IList<Amount> Totals { get; } = [];

public IList<Amount> GetTotalsOrThrowIfEmpty() =>
Totals.Any()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static SelectList GetSelectableCurrencies(
{
var currencies = mode switch
{
CurrencySelectionMode.DefaultCurrency => new[] { moneyService.DefaultCurrency },
CurrencySelectionMode.SpecificCurrency => new[] { moneyService.GetCurrency(specificCurrencyIsoCode) },
CurrencySelectionMode.DefaultCurrency => [moneyService.DefaultCurrency],
CurrencySelectionMode.SpecificCurrency => [moneyService.GetCurrency(specificCurrencyIsoCode)],
CurrencySelectionMode.AllCurrencies => moneyService.Currencies,
_ => throw new ArgumentOutOfRangeException(nameof(mode)),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class InventoryPart : ContentPart
public NumericField MinimumOrderQuantity { get; set; } = new();
public HtmlField OutOfStockMessage { get; set; } = new();

public IList<string> InventoryKeys { get; } = new List<string>();
public IList<string> InventoryKeys { get; } = [];

public string ProductSku { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lombiq.HelpfulLibraries.OrchardCore.Users;
using Lombiq.HelpfulLibraries.OrchardCore.Users;
using OrchardCore.Security.Permissions;
using System.Collections.Generic;

Expand All @@ -9,8 +9,5 @@ public class Permissions : AdminPermissionBase
public static readonly Permission ManageExactlySettings =
new(nameof(ManageExactlySettings), "Manage Exactly settings.");

protected override IEnumerable<Permission> AdminPermissions => new[]
{
ManageExactlySettings,
};
protected override IEnumerable<Permission> AdminPermissions => [ManageExactlySettings];
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OrchardCore.Commerce.AddressDataType;
using OrchardCore.Commerce.AddressDataType;
using Stripe.Checkout;
using System.Collections.Generic;

Expand All @@ -11,7 +11,7 @@ public class SubscriptionCheckoutEndpointViewModel

// This is an API model so we don't need to make it read-only.
#pragma warning disable CA2227 // CA2227: Change 'SessionLineItemOptions' to be read-only by removing the property setter
public IList<SessionLineItemOptions> SessionLineItemOptions { get; set; } = new List<SessionLineItemOptions>();
public IList<SessionLineItemOptions> SessionLineItemOptions { get; set; } = [];
#pragma warning restore CA2227
public PaymentMode PaymentMode { get; set; }
public string Phone { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ public class ApiPermissions : AdminPermissionBase
public static readonly Permission CommerceApiOrderStripe =
new(nameof(CommerceApiStripePayment), "Access Commerce Stripe Order APIs");

private static readonly IReadOnlyList<Permission> _adminPermissions = new[]
{
CommerceApiStripePayment,
};
private static readonly IReadOnlyList<Permission> _adminPermissions = [CommerceApiStripePayment];
protected override IEnumerable<Permission> AdminPermissions => _adminPermissions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ public class Permissions : AdminPermissionBase
{
public static readonly Permission ManageStripeApiSettings = new(nameof(ManageStripeApiSettings), "Manage Stripe API Settings");

private static readonly IReadOnlyList<Permission> _adminPermissions = new[]
{
ManageStripeApiSettings,
};
private static readonly IReadOnlyList<Permission> _adminPermissions = [ManageStripeApiSettings];

protected override IEnumerable<Permission> AdminPermissions => _adminPermissions;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OrchardCore.Commerce.Abstractions.Models;
using OrchardCore.Commerce.Abstractions.Models;
using System.Collections.Generic;

namespace OrchardCore.Commerce.Payment.Stripe.ViewModels;
Expand All @@ -7,7 +7,7 @@ public class StripeCreateSubscriptionViewModel
{
public string ShoppingCartId { get; set; }
public string CustomerId { get; set; }
public IList<string> PriceIds { get; } = new List<string>();
public IList<string> PriceIds { get; } = [];

public OrderPart OrderPart { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace OrchardCore.Commerce.Payment.Constants;
public static class CurrencyCollectionConstants
{
public static readonly IEnumerable<string> SpecialCases = new List<string> { "HUF", "TWD", "UGX" };
public static readonly IEnumerable<string> ZeroDecimalCurrencies = new List<string>
{
public static readonly IEnumerable<string> SpecialCases = ["HUF", "TWD", "UGX"];
public static readonly IEnumerable<string> ZeroDecimalCurrencies =
[
"BIF",
"CLP",
"DJF",
Expand All @@ -22,5 +22,5 @@ public static class CurrencyCollectionConstants
"XAF",
"XOF",
"XPF",
};
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ public class ApiPermissions : AdminPermissionBase
public static readonly Permission CommerceApiPayment =
new(nameof(CommerceApiPayment), "Access Commerce Payment APIs");

private static readonly IReadOnlyList<Permission> _adminPermissions = new[]
{
CommerceApiPayment,
};
private static readonly IReadOnlyList<Permission> _adminPermissions = [CommerceApiPayment];
protected override IEnumerable<Permission> AdminPermissions => _adminPermissions;
}
4 changes: 2 additions & 2 deletions src/Modules/OrchardCore.Commerce.Payment/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
Name = "Orchard Core Commerce - Payment",
Category = "Commerce",
Description = "Payment for Orchard Core Commerce.",
Dependencies = new[] { ContentFields, Tax }
Dependencies = [ContentFields, Tax]
)]

[assembly: Feature(
Id = DummyProvider,
Name = "Orchard Core Commerce - Payment - Dummy Provider",
Category = "Commerce",
Description = "Dummy payment provider used for development and testing.",
Dependencies = new[] { Payment }
Dependencies = [Payment]
)]
6 changes: 1 addition & 5 deletions src/Modules/OrchardCore.Commerce.Payment/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ public class Permissions : AdminPermissionBase
public static readonly Permission ManageOrders = new(nameof(ManageOrders), "Manage Orders");
public static readonly Permission Checkout = new(nameof(Checkout), "Ability to checkout");

private static readonly IReadOnlyList<Permission> _adminPermissions = new[]
{
ManageOrders,
Checkout,
};
private static readonly IReadOnlyList<Permission> _adminPermissions = [ManageOrders, Checkout];

protected override IEnumerable<Permission> AdminPermissions => _adminPermissions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using OrchardCore.Commerce.Abstractions.Models;
using OrchardCore.Commerce.MoneyDataType;
using OrchardCore.DisplayManagement;
using System;
using System.Collections.Generic;

namespace OrchardCore.Commerce.Payment.ViewModels;
Expand All @@ -16,7 +15,7 @@ public class CheckoutViewModel : PaymentViewModel, ICheckoutViewModel
public Amount GrossTotal { get; init; }

[BindNever]
public IEnumerable<SelectListItem> Regions { get; set; } = Array.Empty<SelectListItem>();
public IEnumerable<SelectListItem> Regions { get; set; } = [];

[BindNever]
public IDictionary<string, IDictionary<string, string>> Provinces { get; } =
Expand All @@ -26,7 +25,7 @@ public class CheckoutViewModel : PaymentViewModel, ICheckoutViewModel

public bool IsInvalid { get; set; }

public IEnumerable<IShape> CheckoutShapes { get; init; } = Array.Empty<IShape>();
public IEnumerable<IShape> CheckoutShapes { get; init; } = [];

public CheckoutViewModel(OrderPart orderPart, Amount singleCurrencyTotal, Amount netTotal)
: base(orderPart, singleCurrencyTotal, netTotal) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OrchardCore.Commerce.Promotion.ViewModels;

public class DiscountPartUpdateScriptViewModel
{
public IList<DiscountPartUpdateScriptViewModelItem> Items { get; } = new List<DiscountPartUpdateScriptViewModelItem>();
public IList<DiscountPartUpdateScriptViewModelItem> Items { get; } = [];

public void Add(string selector, Amount oldValue, Amount newValue) =>
Items.Add(new DiscountPartUpdateScriptViewModelItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace OrchardCore.Commerce.Tax.Models;

public class TaxRateSettings
{
public IList<TaxRateSetting> Rates { get; } = new List<TaxRateSetting>();
public IList<TaxRateSetting> Rates { get; } = [];

public void CopyFrom(TaxRateSettings other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ public class TaxRatePermissions : AdminPermissionBase
public static readonly Permission ManageCustomTaxRates =
new(nameof(ManageCustomTaxRates), "Manage Custom Tax Rates");

protected override IEnumerable<Permission> AdminPermissions { get; } = new[]
{
ManageCustomTaxRates,
};
protected override IEnumerable<Permission> AdminPermissions { get; } = [ManageCustomTaxRates];
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public static async Task<IEnumerable<string>> GetProductAttributesCombinationsAs

private static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
IEnumerable<IEnumerable<T>> emptyProduct = [[]];
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
accumulator.SelectMany(
_ => sequence,
(accumulatorSequence, item) => accumulatorSequence.Concat(new[] { item })));
(accumulatorSequence, item) => accumulatorSequence.Concat([item])));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public static async Task<IDictionary<string, ProductPart>> GetSkuProductsAsync(
.ToDictionary(productPart => productPart.Sku);

public static async Task<ShoppingCartItem> AddPriceAsync(this IPriceService priceService, ShoppingCartItem item) =>
(await priceService.AddPricesAsync(new[] { item })).Single();
(await priceService.AddPricesAsync([item])).Single();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace OrchardCore.Commerce.Abstractions;
public static class ProductServiceExtensions
{
public static async Task<ProductPart> GetProductAsync(this IProductService service, string sku) =>
string.IsNullOrEmpty(sku) ? null : (await service.GetProductsAsync(new[] { sku })).SingleOrDefault();
string.IsNullOrEmpty(sku) ? null : (await service.GetProductsAsync([sku])).SingleOrDefault();

public static async Task<IDictionary<string, ProductPart>> GetProductDictionaryAsync(
this IProductService service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace OrchardCore.Commerce.Models;

public class ProductListFilters
{
public IList<string> OrderBy { get; } = new List<string>();
public IList<string> OrderBy { get; } = [];
public IDictionary<string, string> FilterValues { get; } = new Dictionary<string, string>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public class ShoppingCartCellViewModel
public int LineIndex { get; set; }
public int ColumnIndex { get; set; }
public ShoppingCartLineViewModel Line { get; set; }
public IList<(IProductAttributeValue Value, string Type, int Index)> ProductAttributes { get; } =
new List<(IProductAttributeValue Value, string Type, int Index)>();
public IList<(IProductAttributeValue Value, string Type, int Index)> ProductAttributes { get; } = [];

public string Name => $"cart.lines[{LineIndex.ToTechnicalString()}]";
}
2 changes: 1 addition & 1 deletion src/Modules/OrchardCore.Commerce/Models/TieredPricePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace OrchardCore.Commerce.Models;
public class TieredPricePart : ContentPart
{
public Amount DefaultPrice { get; set; }
public IList<PriceTier> PriceTiers { get; } = new List<PriceTier>();
public IList<PriceTier> PriceTiers { get; } = [];

public Amount GetPriceForQuantity(IMoneyService moneyService, int quantity)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Modules/OrchardCore.Commerce/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class Permissions : AdminPermissionBase
public static readonly Permission ManagePriceDisplaySettings = new(nameof(ManagePriceDisplaySettings), "Manage Price Display Settings");
public static readonly Permission ManageRegionSettings = new(nameof(ManageRegionSettings), "Manage Region Settings");

private static readonly IReadOnlyList<Permission> _adminPermissions = new[]
{
private static readonly IReadOnlyList<Permission> _adminPermissions =
[
ManageCurrencySettings,
ManageRegionSettings,
ManagePriceDisplaySettings,
};
];

protected override IEnumerable<Permission> AdminPermissions => _adminPermissions;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OrchardCore.Commerce.Indexes;
using OrchardCore.Commerce.Indexes;
using OrchardCore.Commerce.Models;
using OrchardCore.ContentManagement;
using System;
Expand All @@ -18,7 +18,7 @@ public class BasePriceFilterProvider : IProductListFilterProvider
public Task<bool> IsApplicableAsync(ProductListPart productList) => Task.FromResult(true);

public Task<IEnumerable<string>> GetOrderByOptionIdsAsync(ProductListPart productList) =>
Task.FromResult<IEnumerable<string>>(new[] { PriceAscOrderById, PriceDescOrderById });
Task.FromResult<IEnumerable<string>>([PriceAscOrderById, PriceDescOrderById]);

public Task<IEnumerable<string>> GetFilterIdsAsync(ProductListPart productListPart) =>
Task.FromResult(Enumerable.Empty<string>());
Expand Down
2 changes: 1 addition & 1 deletion src/Modules/OrchardCore.Commerce/Services/PriceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<IList<ShoppingCartItem>> AddPricesAsync(IList<ShoppingCartItem
// Take out subsets where items are individually applicable to the remaining providers.
foreach (var provider in remainingProviders)
{
var applicable = await unhandled.WhereAsync(pair => provider.IsApplicableAsync(new[] { pair.Item }));
var applicable = await unhandled.WhereAsync(pair => provider.IsApplicableAsync([pair.Item]));
if (!applicable.Any()) continue;

unhandled.RemoveAll(applicable.Contains);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<IList<ShoppingCartItem>> UpdateAsync(IList<ShoppingCartItem> m
return item;
}));

return updatedModel.ToList();
return [.. updatedModel];
}

private async Task<ShoppingCartItem> AddPriceToShoppingCartItemAsync(ShoppingCartItem item, ProductPart productPart)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ public async Task<IEnumerable<string>> GetFilterIdsAsync(ProductListPart product
}

private async Task<IList<IProductListFilterProvider>> GetOrderedApplicableProvidersAsync(ProductListPart productList) =>
(await _productListQueryProviders
.WhereAsync(async provider => await provider.IsApplicableAsync(productList)))
.OrderBy(provider => provider.Order)
.ToList();
[.. (await _productListQueryProviders
.WhereAsync(async provider => await provider.IsApplicableAsync(productList)))
.OrderBy(provider => provider.Order)];
}
Loading

0 comments on commit 03bcabb

Please sign in to comment.