Skip to content

Commit

Permalink
コード分析の警告に対処する (#1395)
Browse files Browse the repository at this point in the history
* インスタンスメンバーを使用しないメソッドを静的メソッドに変更

* テストクラス内での配列初期化方法を簡略化

* 簡略化された using 宣言を利用するように修正

* 不要なusingディレクティブを削除

* リストの初期化にコレクション初期化子を使用するよう修正

* インスタンス生成にオブジェクト初期化子を使用するように修正

* コレクション初期化子を使用するように修正

* AnyのかわりにCountやLengthと比較するよう修正

* メソッドのカスタム属性の引数にnameofを利用して引数名を指定するよう修正

* 不要なusingを削除

* 読み取り専用フィールドに変更

* コレクション初期化子を使うように修正

* AnyのかわりにCountを使って比較するように修正
  • Loading branch information
tsuna-can-se authored Jun 28, 2024
1 parent 5e7b328 commit febec08
Show file tree
Hide file tree
Showing 43 changed files with 132 additions and 145 deletions.
2 changes: 1 addition & 1 deletion samples/ConsoleAppWithDI/solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ internal class ValidatableParameter : IValidatableObject
{
yield return new ValidationResult(
"1 週間より前の日付は指定できません。",
new string[] { nameof(this.ExecuteDate) });
[nameof(this.ExecuteDate)]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Maris.ConsoleApp.Core;
/// </summary>
public class InvalidParameterException : Exception
{
private readonly List<ValidationResult> validationResults = new();
private readonly List<ValidationResult> validationResults = [];

/// <summary>
/// <see cref="InvalidParameterException"/> クラスの新しいインスタンスを初期化します。
Expand Down Expand Up @@ -46,7 +46,7 @@ public override string Message
{
get
{
if (this.validationResults.Any())
if (this.validationResults.Count != 0)
{
return base.Message +
Messages.InvalidCommandParameterDetails.Embed(ValidationResultsToString(this.validationResults));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace Maris.ConsoleApp.Hosting;
/// </summary>
internal class CommandParameterTypeCollection : IEnumerable<Type>
{
private readonly Dictionary<string, Type> commandParameterTypes = new();
private readonly List<Assembly> loadedAssemblies = new();
private readonly Dictionary<string, Type> commandParameterTypes = [];
private readonly List<Assembly> loadedAssemblies = [];

/// <summary>
/// <see cref="CommandParameterTypeCollection"/> クラスの新しいインスタンスを初期化します。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal ConsoleAppContext CreateConsoleAppContext(
Messages.CommandParameterIsNotExists.Embed(typeof(CommandAttribute), assemblies));
}

var param = Parser.Default.ParseArguments(args, commandParameterTypes.ToArray());
var param = Parser.Default.ParseArguments(args, [.. commandParameterTypes]);
if (param is null || param.Tag == ParserResultType.NotParsed)
{
this.appProcess.Exit(this.settings.DefaultValidationErrorExitCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class ProductsRepository : IProductsRepository

static ProductsRepository()
{
categories = new List<ProductCategory>
{
categories =
[
new ProductCategory { Id = 1, CategoryName = "本" },
new ProductCategory { Id = 2, CategoryName = "音楽" },
new ProductCategory { Id = 3, CategoryName = "パソコン" },
};
];

products = new List<Product>
{
products =
[
new Product() { Id = 1, Name = "本1", ProductCategoryId = 1, ProductCategory = categories[0], UnitPrice = 1000 },
new Product() { Id = 2, Name = "本2", ProductCategoryId = 1, ProductCategory = categories[0], UnitPrice = 1100 },
new Product() { Id = 3, Name = "本3", ProductCategoryId = 1, ProductCategory = categories[0], UnitPrice = 1200 },
Expand All @@ -46,7 +46,7 @@ static ProductsRepository()
new Product() { Id = 22, Name = "パソコン 6", ProductCategoryId = 3, ProductCategory = categories[2], UnitPrice = 250000 },
new Product() { Id = 23, Name = "パソコン 7", ProductCategoryId = 3, ProductCategory = categories[2], UnitPrice = 260000 },
new Product() { Id = 24, Name = "パソコン 8", ProductCategoryId = 3, ProductCategory = categories[2], UnitPrice = 270000 },
};
];

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ public async Task AddItemToBasketAsync(string buyerId, long catalogItemId, int a
var basket = await this.GetOrCreateBasketForUserAsync(buyerId, cancellationToken);

// カタログリポジトリに存在しないカタログアイテムが指定されていないか確認
var (existsAll, catalogItems) = await this.catalogDomainService.ExistsAllAsync(new[] { catalogItemId }, cancellationToken);
var (existsAll, catalogItems) = await this.catalogDomainService.ExistsAllAsync([catalogItemId], cancellationToken);
if (!existsAll)
{
var notExistingInRepositoryCatalogIds = new List<long>() { catalogItemId };
List<long> notExistingInRepositoryCatalogIds = [catalogItemId];
throw new CatalogItemNotExistingInRepositoryException(notExistingInRepositoryCatalogIds);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class AssetTypes
/// </summary>
public const string Png = "png";

private static readonly HashSet<string> SupportedAssetTypes = new() { Png };
private static readonly HashSet<string> SupportedAssetTypes = [Png];

/// <summary>
/// 指定したアセットタイプがサポートされているかどうか示す値を取得します。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Dressca.ApplicationCore.Baskets;
/// </summary>
public class Basket
{
private readonly List<BasketItem> items = new();
private readonly List<BasketItem> items = [];
private string buyerId;

/// <summary>
Expand Down Expand Up @@ -107,8 +107,5 @@ public Account GetAccount()
/// 買い物かごアイテムが空かどうか示す値を取得します。
/// </summary>
/// <returns>買い物かごアイテムが空の場合は <see langword="true"/> 、そうでない場合は <see langword="false"/> 。</returns>
public bool IsEmpty()
{
return !this.items.Any();
}
public bool IsEmpty() => this.items.Count == 0;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Xml.Linq;
using Dressca.ApplicationCore.Resources;

namespace Dressca.ApplicationCore.Catalog;
Expand All @@ -10,7 +9,7 @@ namespace Dressca.ApplicationCore.Catalog;
/// </summary>
public class CatalogBrand
{
private readonly List<CatalogItem> items = new();
private readonly List<CatalogItem> items = [];
private string name;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Dressca.ApplicationCore.Catalog;
/// </summary>
public class CatalogCategory
{
private readonly List<CatalogItem> items = new();
private readonly List<CatalogItem> items = [];
private string name;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ await this.catalogRepository.FindAsync(
var notExistsCatalogItemIds = catalogItemIds
.Where(catalogItemId => !items.Any(catalogItem => catalogItem.Id == catalogItemId))
.ToArray();
if (notExistsCatalogItemIds.Any())
if (notExistsCatalogItemIds.Length != 0)
{
this.logger.LogInformation(
Events.CatalogItemIdDoesNotExistInRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Dressca.ApplicationCore.Catalog;
/// </summary>
public class CatalogItem
{
private readonly List<CatalogItemAsset> assets = new();
private readonly List<CatalogItemAsset> assets = [];
private CatalogCategory? catalogCategory;
private CatalogBrand? catalogBrand;
private string name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Dressca.ApplicationCore.Ordering;
/// </summary>
public class Order
{
private readonly List<OrderItem> orderItems = new();
private readonly List<OrderItem> orderItems = [];
private readonly TimeProvider timeProvider;
private readonly Account? account;
private string buyerId;
Expand All @@ -34,7 +34,7 @@ public Order(List<OrderItem> orderItems)
/// <exception cref="ArgumentNullException"><paramref name="timeProvider"/> が <see langword="null"/> です。</exception>
internal Order(List<OrderItem> orderItems, TimeProvider timeProvider)
{
if (orderItems is null || !orderItems.Any())
if (orderItems is null || orderItems.Count == 0)
{
throw new ArgumentException(Messages.ArgumentIsNullOrEmptyList, nameof(orderItems));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Dressca.ApplicationCore.Ordering;
/// </summary>
public class OrderItem
{
private readonly List<OrderItemAsset> assets = new();
private readonly List<OrderItemAsset> assets = [];
private Order? order;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void Configure(EntityTypeBuilder<Asset> builder)

builder.HasIndex(asset => asset.AssetCode);

builder.HasData(new Asset[]
{
builder.HasData(
[
new() { AssetCode = "b52dc7f712d94ca5812dd995bf926c04", AssetType = AssetTypes.Png, Id = 1L }, // TOP 用
new() { AssetCode = "80bc8e167ccb4543b2f9d51913073492", AssetType = AssetTypes.Png, Id = 2L }, // TOP 用
new() { AssetCode = "05d38fad5693422c8a27dd5b14070ec8", AssetType = AssetTypes.Png, Id = 3L }, // TOP 用
Expand All @@ -40,6 +40,6 @@ public void Configure(EntityTypeBuilder<Asset> builder)
new() { AssetCode = "ab2e78eb7fe3408aadbf1e17a9945a8c", AssetType = AssetTypes.Png, Id = 13L },
new() { AssetCode = "0e557e96bc054f10bc91c27405a83e85", AssetType = AssetTypes.Png, Id = 14L },
new() { AssetCode = "e622b0098808492cb883831c05486b58", AssetType = AssetTypes.Png, Id = 15L }, // now printing
});
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public void Configure(EntityTypeBuilder<CatalogBrand> builder)
.HasMaxLength(128)
.IsRequired();

builder.HasData(new CatalogBrand[]
{
builder.HasData(
[
new() { Name = "高級なブランド", Id = 1L },
new() { Name = "カジュアルなブランド", Id = 2L },
new() { Name = "ノーブランド", Id = 3L },
});
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public void Configure(EntityTypeBuilder<CatalogCategory> builder)
.HasMaxLength(128)
.IsRequired();

builder.HasData(new CatalogCategory[]
{
builder.HasData(
[
new() { Name = "服", Id = 1L },
new() { Name = "バッグ", Id = 2L },
new() { Name = "シューズ", Id = 3L },
});
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void Configure(EntityTypeBuilder<CatalogItemAsset> builder)
.OnDelete(DeleteBehavior.Cascade)
.HasConstraintName("FK_CatalogItemAssets_CatalogItems");

builder.HasData(new CatalogItemAsset[]
{
builder.HasData(
[
new() { AssetCode = "45c22ba3da064391baac91341067ffe9", CatalogItemId = 1L, Id = 1L },
new() { AssetCode = "4aed07c4ed5d45a5b97f11acedfbb601", CatalogItemId = 2L, Id = 2L },
new() { AssetCode = "082b37439ecc44919626ba00fc60ee85", CatalogItemId = 3L, Id = 3L },
Expand All @@ -36,6 +36,6 @@ public void Configure(EntityTypeBuilder<CatalogItemAsset> builder)
new() { AssetCode = "cf151206efd344e1b86854f4aa49fdef", CatalogItemId = 9L, Id = 9L },
new() { AssetCode = "ab2e78eb7fe3408aadbf1e17a9945a8c", CatalogItemId = 10L, Id = 10L },
new() { AssetCode = "0e557e96bc054f10bc91c27405a83e85", CatalogItemId = 11L, Id = 11L },
});
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void Configure(EntityTypeBuilder<CatalogItem> builder)

builder.HasIndex(catalogItem => catalogItem.ProductCode);

builder.HasData(new CatalogItem[]
{
builder.HasData(
[
new() { CatalogCategoryId = 1L, CatalogBrandId = 3L, Description = "定番の無地ロングTシャツです。", Name = "クルーネック Tシャツ - ブラック", Price = 1980m, ProductCode = "C000000001", Id = 1L },
new() { CatalogCategoryId = 1L, CatalogBrandId = 2L, Description = "暖かいのに着膨れしない起毛デニムです。", Name = "裏起毛 スキニーデニム", Price = 4800m, ProductCode = "C000000002", Id = 2L },
new() { CatalogCategoryId = 1L, CatalogBrandId = 1L, Description = "あたたかく肌ざわりも良いウール100%のロングコートです。", Name = "ウールコート", Price = 49800m, ProductCode = "C000000003", Id = 3L },
Expand All @@ -51,6 +51,6 @@ public void Configure(EntityTypeBuilder<CatalogItem> builder)
new() { CatalogCategoryId = 2L, CatalogBrandId = 1L, Description = "エレガントな雰囲気を放つキルティングデザインです。", Name = "レザー チェーンショルダーバッグ", Price = 258000m, ProductCode = "B000000005", Id = 9L },
new() { CatalogCategoryId = 3L, CatalogBrandId = 2L, Description = "柔らかいソールは快適な履き心地で、ランニングに最適です。", Name = "ランニングシューズ - ブルー", Price = 12800m, ProductCode = "S000000001", Id = 10L },
new() { CatalogCategoryId = 3L, CatalogBrandId = 1L, Description = "イタリアの職人が丁寧に手作業で作り上げた一品です。", Name = "メダリオン ストレートチップ ドレスシューズ", Price = 23800m, ProductCode = "S000000002", Id = 11L },
});
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Dressca.SystemCommon;
/// </summary>
public class BusinessError
{
private readonly List<string> errorMessages = new();
private readonly List<string> errorMessages = [];

/// <summary>
/// <see cref="BusinessError"/> クラスの新しいインスタンスを初期化します。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Dressca.SystemCommon;
/// </summary>
public class BusinessErrorCollection : IEnumerable<BusinessError>
{
private readonly Dictionary<string, BusinessError> businessErrors = new();
private readonly Dictionary<string, BusinessError> businessErrors = [];

/// <summary>
/// <see cref="BusinessErrorCollection"/> クラスの新しいインスタンスを初期化します。
Expand Down Expand Up @@ -36,7 +36,7 @@ public void AddOrMerge(BusinessError newBusinessError)
ArgumentNullException.ThrowIfNull(newBusinessError);
if (this.businessErrors.TryGetValue(newBusinessError.ErrorCode, out var businessError))
{
businessError.AddErrorMessages(newBusinessError.ErrorMessages.ToArray());
businessError.AddErrorMessages([.. newBusinessError.ErrorMessages]);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public interface IObjectMapper<TFrom, TTo>
/// </summary>
/// <param name="value">変換するオブジェクト。</param>
/// <returns>変換後のオブジェクト。</returns>
[return: NotNullIfNotNull("value")]
[return: NotNullIfNotNull(nameof(value))]
TTo? Convert(TFrom? value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class ObjectExtensions
/// </exception>
public static void ThrowIfNull(
[NotNull] this object? argument,
[CallerArgumentExpression("argument")] string? paramName = null)
[CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (argument is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Dressca.SystemCommon.Text.Json;
/// </summary>
public static class DefaultJsonSerializerOptions
{
private static JsonSerializerOptions options = new JsonSerializerOptions
private static readonly JsonSerializerOptions Options = new()
{
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
};
Expand All @@ -20,6 +20,6 @@ public static class DefaultJsonSerializerOptions
/// <returns><see cref="JsonSerializerOptions"/> のインスタンス。</returns>
public static JsonSerializerOptions GetInstance()
{
return options;
return Options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public class BasketResponse
/// <summary>
/// 買い物かごアイテムのリストを取得または設定します。
/// </summary>
public IList<BasketItemResponse> BasketItems { get; set; } = new List<BasketItemResponse>();
public IList<BasketItemResponse> BasketItems { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ public class CatalogItemResponse
/// <summary>
/// アセットコードの一覧を取得または設定します。
/// </summary>
public IList<string> AssetCodes { get; set; } = new List<string>();
public IList<string> AssetCodes { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ public class CatalogItemSummaryResponse
/// <summary>
/// アセットコードの一覧を取得または設定します。
/// </summary>
public IList<string> AssetCodes { get; set; } = new List<string>();
public IList<string> AssetCodes { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ public class OrderResponse
/// <summary>
/// 注文アイテムのリストを取得または設定します。
/// </summary>
public IList<OrderItemResponse> OrderItems { get; set; } = new List<OrderItemResponse>();
public IList<OrderItemResponse> OrderItems { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PagedList<T>
/// <param name="pageSize">1 ページあたりの件数。</param>
public PagedList(IList<T>? items, int totalCount, int page, int pageSize)
{
this.Items = items ?? new List<T>();
this.Items = items ?? [];
this.TotalCount = totalCount;
this.Page = page;
this.PageSize = pageSize;
Expand Down
Loading

0 comments on commit febec08

Please sign in to comment.