Skip to content

Commit 339fe1b

Browse files
committed
Optimize code
1 parent 89fba3e commit 339fe1b

File tree

417 files changed

+3133
-1144
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

417 files changed

+3133
-1144
lines changed

src/Nager.Country.Translation/TranslationProvider.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ public TranslationProvider()
2626
var interfaceType = typeof(ILanguageTranslation);
2727
var types = AppDomain.CurrentDomain.GetAssemblies()
2828
.SelectMany(s => s.GetLoadableTypes())
29-
.Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass);
29+
.Where(p => p is not null &&
30+
interfaceType.IsAssignableFrom(p) &&
31+
p.IsClass);
3032

3133
foreach (var type in types)
3234
{
35+
if (type is null)
36+
{
37+
continue;
38+
}
39+
3340
var languageTranslation = (ILanguageTranslation?)Activator.CreateInstance(type);
3441
if (languageTranslation is null)
3542
{

src/Nager.Country.UnitTest/CountryDataValidationTest.cs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public async Task CompareWithMledozeCountryProject()
2323
var json = await httpClient.GetStringAsync("https://raw.githubusercontent.com/mledoze/countries/master/dist/countries.json");
2424
var items = JsonConvert.DeserializeObject<MledozeCountry[]>(json);
2525

26+
if (items is null)
27+
{
28+
Assert.Fail("Failure deserialize");
29+
}
30+
2631
ICountryProvider countryProvider = new CountryProvider();
2732
foreach (var countryCode in (Alpha2Code[])Enum.GetValues(typeof(Alpha2Code)))
2833
{
@@ -76,47 +81,47 @@ private string GetSubRegion(SubRegion subRegion)
7681

7782
public class MledozeCountry
7883
{
79-
public Name Name { get; set; }
80-
public string[] Tld { get; set; }
81-
public string Cca2 { get; set; }
84+
public required Name Name { get; set; }
85+
public required string[] Tld { get; set; }
86+
public required string Cca2 { get; set; }
8287
public int? Ccn3 { get; set; }
83-
public string Cca3 { get; set; }
84-
public string Cioc { get; set; }
88+
public required string Cca3 { get; set; }
89+
public required string Cioc { get; set; }
8590
public bool? Independent { get; set; }
86-
public string Status { get; set; }
87-
public dynamic Currencies { get; set; }
88-
public string[] CallingCode { get; set; }
89-
public string[] Capital { get; set; }
90-
public string[] AltSpellings { get; set; }
91-
public string Region { get; set; }
92-
public string Subregion { get; set; }
91+
public required string Status { get; set; }
92+
public required dynamic Currencies { get; set; }
93+
public string[] CallingCode { get; set; } = [];
94+
public string[] Capital { get; set; } = [];
95+
public string[] AltSpellings { get; set; } = [];
96+
public required string Region { get; set; }
97+
public required string Subregion { get; set; }
9398
//public Languages languages { get; set; }
9499
//public Dictionary<string, NameNative> translations { get; set; }
95-
public List<double> LatLng { get; set; }
96-
public string Demonym { get; set; }
100+
public required List<double> LatLng { get; set; }
101+
public string? Demonym { get; set; }
97102
public bool Landlocked { get; set; }
98-
public string[] Borders { get; set; }
103+
public string[] Borders { get; set; } = [];
99104
public double Area { get; set; }
100-
public string Flag { get; set; }
105+
public required string Flag { get; set; }
101106
}
102107

103108
public class Name
104109
{
105-
public string Common { get; set; }
106-
public string Official { get; set; }
107-
public Dictionary<string, NameNative> Native { get; set; }
110+
public required string Common { get; set; }
111+
public required string Official { get; set; }
112+
public required Dictionary<string, NameNative> Native { get; set; }
108113
}
109114

110115
public class NameNative
111116
{
112-
public string Common { get; set; }
113-
public string Official { get; set; }
117+
public required string Common { get; set; }
118+
public required string Official { get; set; }
114119
}
115120

116121
public class Currency
117122
{
118-
public string Name { get; set; }
119-
public string Symbol { get; set; }
123+
public required string Name { get; set; }
124+
public string? Symbol { get; set; }
120125
}
121126
}
122127
}

src/Nager.Country.UnitTest/CountryTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public void GetCountryByNameConsiderTranslation(string countryName)
7575
ICountryProvider countryProvider = new CountryProvider();
7676

7777
var countryInfo = countryProvider.GetCountryByNameConsiderTranslation(countryName);
78+
if (countryInfo is null)
79+
{
80+
Assert.Fail("CountryInfo is null");
81+
}
82+
7883
Assert.AreEqual(Alpha2Code.AT, countryInfo.Alpha2Code);
7984
}
8085

src/Nager.Country.UnitTest/CurrencyTest.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@ private ICurrency[] GetCurrencies()
2020
var currenies = new List<ICurrency>();
2121
foreach (var currencyClass in currencyClasses)
2222
{
23-
var currency = (ICurrency)Activator.CreateInstance(currencyClass);
23+
var currency = (ICurrency?)Activator.CreateInstance(currencyClass);
24+
if (currency is null)
25+
{
26+
continue;
27+
}
28+
2429
currenies.Add(currency);
2530
}
2631

27-
return currenies.ToArray();
32+
return [.. currenies];
2833
}
2934

3035
[TestMethod]

src/Nager.Country.UnitTest/Nager.Country.UnitTest.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55

6+
<Nullable>enable</Nullable>
7+
68
<IsPackable>false</IsPackable>
79
</PropertyGroup>
810

src/Nager.Country/CountryInfos/AfghanistanCountryInfo.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,50 @@ namespace Nager.Country.CountryInfos
55
/// <summary>
66
/// Afghanistan
77
/// </summary>
8-
public class AfghanistanCountryInfo : ICountryInfo
8+
public sealed class AfghanistanCountryInfo : ICountryInfo
99
{
1010
/// <inheritdoc/>
1111
public string CommonName => "Afghanistan";
12+
1213
/// <inheritdoc/>
1314
public string OfficialName => "Islamic Republic of Afghanistan";
15+
1416
/// <inheritdoc/>
1517
public string NativeName => "افغانستان";
18+
1619
/// <inheritdoc/>
1720
public Alpha2Code Alpha2Code => Alpha2Code.AF;
21+
1822
/// <inheritdoc/>
1923
public Alpha3Code Alpha3Code => Alpha3Code.AFG;
24+
2025
/// <inheritdoc/>
2126
public int NumericCode => 004;
27+
2228
/// <inheritdoc/>
23-
public string[] TLD => new [] { ".af" };
29+
public string[] TLD => [".af"];
30+
2431
/// <inheritdoc/>
2532
public Region Region => Region.Asia;
33+
2634
/// <inheritdoc/>
2735
public SubRegion SubRegion => SubRegion.SouthernAsia;
2836

2937
/// <inheritdoc/>
30-
public Alpha2Code[] BorderCountries => new Alpha2Code[]
31-
{
38+
public Alpha2Code[] BorderCountries =>
39+
[
3240
Alpha2Code.IR,
3341
Alpha2Code.PK,
3442
Alpha2Code.TM,
3543
Alpha2Code.UZ,
3644
Alpha2Code.TJ,
3745
Alpha2Code.CN,
38-
};
46+
];
3947

4048
/// <inheritdoc/>
41-
public ICurrency[] Currencies => new [] { new AfnCurrency() };
49+
public ICurrency[] Currencies => [new AfnCurrency()];
4250

4351
/// <inheritdoc/>
44-
public string[] CallingCodes => new [] { "93" };
52+
public string[] CallingCodes => ["93"];
4553
}
4654
}

src/Nager.Country/CountryInfos/AlandIslandsCountryInfo.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,42 @@ namespace Nager.Country.CountryInfos
55
/// <summary>
66
/// Åland Islands
77
/// </summary>
8-
public class AlandIslandsCountryInfo : ICountryInfo
8+
public sealed class AlandIslandsCountryInfo : ICountryInfo
99
{
1010
/// <inheritdoc/>
1111
public string CommonName => "Åland Islands";
12+
1213
/// <inheritdoc/>
1314
public string OfficialName => "Åland Islands";
15+
1416
/// <inheritdoc/>
1517
public string NativeName => "Åland";
18+
1619
/// <inheritdoc/>
1720
public Alpha2Code Alpha2Code => Alpha2Code.AX;
21+
1822
/// <inheritdoc/>
1923
public Alpha3Code Alpha3Code => Alpha3Code.ALA;
24+
2025
/// <inheritdoc/>
2126
public int NumericCode => 248;
27+
2228
/// <inheritdoc/>
23-
public string[] TLD => new [] { ".ax" };
29+
public string[] TLD => [".ax"];
2430

2531
/// <inheritdoc/>
2632
public Region Region => Region.Europe;
33+
2734
/// <inheritdoc/>
2835
public SubRegion SubRegion => SubRegion.NorthernEurope;
2936

3037
/// <inheritdoc/>
31-
public Alpha2Code[] BorderCountries => new Alpha2Code[]
32-
{
33-
};
38+
public Alpha2Code[] BorderCountries => [];
3439

3540
/// <inheritdoc/>
36-
public ICurrency[] Currencies => new [] { new EurCurrency() };
41+
public ICurrency[] Currencies => [new EurCurrency()];
42+
3743
/// <inheritdoc/>
38-
public string[] CallingCodes => new [] { "358" };
44+
public string[] CallingCodes => ["358"];
3945
}
4046
}

src/Nager.Country/CountryInfos/AlbaniaCountryInfo.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,47 @@ namespace Nager.Country.CountryInfos
55
/// <summary>
66
/// Albania
77
/// </summary>
8-
public class AlbaniaCountryInfo : ICountryInfo
8+
public sealed class AlbaniaCountryInfo : ICountryInfo
99
{
1010
/// <inheritdoc/>
1111
public string CommonName => "Albania";
12+
1213
/// <inheritdoc/>
1314
public string OfficialName => "Republic of Albania";
15+
1416
/// <inheritdoc/>
1517
public string NativeName => "Shqipëria";
18+
1619
/// <inheritdoc/>
1720
public Alpha2Code Alpha2Code => Alpha2Code.AL;
21+
1822
/// <inheritdoc/>
1923
public Alpha3Code Alpha3Code => Alpha3Code.ALB;
24+
2025
/// <inheritdoc/>
2126
public int NumericCode => 008;
27+
2228
/// <inheritdoc/>
23-
public string[] TLD => new [] { ".al" };
29+
public string[] TLD => [".al"];
2430

2531
/// <inheritdoc/>
2632
public Region Region => Region.Europe;
33+
2734
/// <inheritdoc/>
2835
public SubRegion SubRegion => SubRegion.SouthernEurope;
2936

3037
/// <inheritdoc/>
31-
public Alpha2Code[] BorderCountries => new Alpha2Code[]
32-
{
38+
public Alpha2Code[] BorderCountries =>
39+
[
3340
Alpha2Code.ME,
3441
Alpha2Code.GR,
3542
Alpha2Code.MK,
36-
};
43+
];
3744

3845
/// <inheritdoc/>
39-
public ICurrency[] Currencies => new [] { new AllCurrency() };
46+
public ICurrency[] Currencies => [new AllCurrency()];
47+
4048
/// <inheritdoc/>
41-
public string[] CallingCodes => new [] { "355" };
49+
public string[] CallingCodes => ["355"];
4250
}
4351
}

src/Nager.Country/CountryInfos/AlgeriaCountryInfo.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,51 @@ namespace Nager.Country.CountryInfos
55
/// <summary>
66
/// Algeria
77
/// </summary>
8-
public class AlgeriaCountryInfo : ICountryInfo
8+
public sealed class AlgeriaCountryInfo : ICountryInfo
99
{
1010
/// <inheritdoc/>
1111
public string CommonName => "Algeria";
12+
1213
/// <inheritdoc/>
1314
public string OfficialName => "People's Democratic Republic of Algeria";
15+
1416
/// <inheritdoc/>
1517
public string NativeName => "الجزائر";
18+
1619
/// <inheritdoc/>
1720
public Alpha2Code Alpha2Code => Alpha2Code.DZ;
21+
1822
/// <inheritdoc/>
1923
public Alpha3Code Alpha3Code => Alpha3Code.DZA;
24+
2025
/// <inheritdoc/>
2126
public int NumericCode => 012;
27+
2228
/// <inheritdoc/>
23-
public string[] TLD => new [] { ".dz", "الجزائر." };
29+
public string[] TLD => [".dz", "الجزائر."];
2430

2531
/// <inheritdoc/>
2632
public Region Region => Region.Africa;
33+
2734
/// <inheritdoc/>
2835
public SubRegion SubRegion => SubRegion.NorthernAfrica;
2936

3037
/// <inheritdoc/>
31-
public Alpha2Code[] BorderCountries => new Alpha2Code[]
32-
{
38+
public Alpha2Code[] BorderCountries =>
39+
[
3340
Alpha2Code.TN,
3441
Alpha2Code.LY,
3542
Alpha2Code.NE,
3643
Alpha2Code.EH,
3744
Alpha2Code.MR,
3845
Alpha2Code.ML,
3946
Alpha2Code.MA,
40-
};
47+
];
4148

4249
/// <inheritdoc/>
43-
public ICurrency[] Currencies => new [] { new DzdCurrency() };
50+
public ICurrency[] Currencies => [new DzdCurrency()];
51+
4452
/// <inheritdoc/>
45-
public string[] CallingCodes => new [] { "213" };
53+
public string[] CallingCodes => ["213"];
4654
}
4755
}

0 commit comments

Comments
 (0)