Skip to content

Commit

Permalink
Fix SonarQube issues (#268)
Browse files Browse the repository at this point in the history
* fix several SQ issues
* start using new c# features
  • Loading branch information
lg2de authored Dec 9, 2023
1 parent 50a70ce commit 3eca0e4
Show file tree
Hide file tree
Showing 48 changed files with 489 additions and 678 deletions.
2 changes: 1 addition & 1 deletion src/SimpleAccounting/AppBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace lg2de.SimpleAccounting;
Justification = "The bootstrapper is responsible to configure all classes.")]
public class AppBootstrapper : BootstrapperBase
{
private readonly SimpleContainer container = new SimpleContainer();
private readonly SimpleContainer container = new();

public AppBootstrapper()
{
Expand Down
6 changes: 4 additions & 2 deletions src/SimpleAccounting/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace lg2de.SimpleAccounting.Extensions;
using System;

/// <summary>
/// Implements extensions on <see cref="DateTime"/>.
/// Implements extensions on <see cref="DateTime" />.
/// </summary>
internal static class DateTimeExtensions
{
Expand All @@ -20,7 +20,9 @@ public static DateTime ToDateTime(this uint date)
return new DateTime(
(int)date / YearFactor,
(int)(date / MonthFactor) % MonthFactor,
(int)date % MonthFactor);
(int)date % MonthFactor,
0, 0, 0,
DateTimeKind.Local);
}

public static uint ToAccountingDate(this DateTime date)
Expand Down
20 changes: 12 additions & 8 deletions src/SimpleAccounting/Infrastructure/ImportFileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace lg2de.SimpleAccounting.Infrastructure;
using lg2de.SimpleAccounting.Model;
using lg2de.SimpleAccounting.Presentation;

internal sealed class ImportFileLoader : IDisposable
internal sealed partial class ImportFileLoader : IDisposable
{
private readonly List<AccountDefinition> accounts;
private readonly CultureInfo cultureInfo;
private readonly string fileName;
private readonly AccountDefinitionImportMapping importMapping;
private readonly Regex duplicateSpaceExpression = new Regex(@"\s+", RegexOptions.Compiled);
private readonly Regex duplicateSpaceExpression = DuplicateSpaceRegex();

private StreamReader? streamReader;

Expand Down Expand Up @@ -60,18 +60,18 @@ internal IEnumerable<ImportEntryViewModel> ImportBookings(TextReader reader, Csv
{
var dateField =
this.importMapping.Columns
.FirstOrDefault(x => x.Target == AccountDefinitionImportMappingColumnTarget.Date)?.Source
.Find(x => x.Target == AccountDefinitionImportMappingColumnTarget.Date)?.Source
?? "date";
var nameField =
this.importMapping.Columns
.FirstOrDefault(x => x.Target == AccountDefinitionImportMappingColumnTarget.Name)?.Source
.Find(x => x.Target == AccountDefinitionImportMappingColumnTarget.Name)?.Source
?? "name";
var textField =
this.importMapping.Columns
.FirstOrDefault(x => x.Target == AccountDefinitionImportMappingColumnTarget.Text);
.Find(x => x.Target == AccountDefinitionImportMappingColumnTarget.Text);
var valueField =
this.importMapping.Columns
.FirstOrDefault(x => x.Target == AccountDefinitionImportMappingColumnTarget.Value)?.Source
.Find(x => x.Target == AccountDefinitionImportMappingColumnTarget.Value)?.Source
?? "value";

using var csv = new CsvReader(reader, configuration);
Expand Down Expand Up @@ -108,7 +108,8 @@ private ImportEntryViewModel ImportBooking(
text ??= string.Empty;
if (!string.IsNullOrEmpty(textField.IgnorePattern))
{
text = Regex.Replace(text, textField.IgnorePattern, string.Empty);
text = Regex.Replace(
text, textField.IgnorePattern, string.Empty, RegexOptions.None, TimeSpan.FromSeconds(1));
}

text = this.duplicateSpaceExpression.Replace(text, " ");
Expand Down Expand Up @@ -141,4 +142,7 @@ bool PatternPredicate(AccountDefinitionImportMappingPattern pattern)
return true;
}
}
}

[GeneratedRegex(@"\s+", RegexOptions.Compiled)]
private static partial Regex DuplicateSpaceRegex();
}
7 changes: 3 additions & 4 deletions src/SimpleAccounting/Infrastructure/ProjectFileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace lg2de.SimpleAccounting.Infrastructure;

using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -29,11 +28,11 @@ public ProjectFileLoader(Settings settings, IDialogs dialogs, IFileSystem fileSy
this.fileSystem = fileSystem;
this.processApi = processApi;

this.settings.RecentProjects ??= new StringCollection();
this.settings.SecuredDrives ??= new StringCollection();
this.settings.RecentProjects ??= [];
this.settings.SecuredDrives ??= [];
}

public AccountingData ProjectData { get; private set; } = new AccountingData();
public AccountingData ProjectData { get; private set; } = new();

/// <summary>
/// Gets a value indicating whether project has been migrated.
Expand Down
47 changes: 22 additions & 25 deletions src/SimpleAccounting/Model/AccountingData.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public partial class AccountingData

public AccountingData()
{
this.Accounts = new List<AccountingDataAccountGroup>();
this.Journal = new List<AccountingDataJournal>();
this.Accounts = [];
this.Journal = [];
this.Setup = new AccountingDataSetup();
}

Expand Down Expand Up @@ -64,26 +64,23 @@ internal static AccountingData GetTemplateProject()
var year = (ushort)DateTime.Now.Year;
var defaultAccounts = new List<AccountDefinition>
{
new AccountDefinition { ID = 100, Name = "Bank account", Type = AccountDefinitionType.Asset },
new AccountDefinition { ID = 400, Name = "Salary", Type = AccountDefinitionType.Income },
new AccountDefinition { ID = 600, Name = "Food", Type = AccountDefinitionType.Expense },
new AccountDefinition { ID = 990, Name = "Carryforward", Type = AccountDefinitionType.Carryforward }
new() { ID = 100, Name = "Bank account", Type = AccountDefinitionType.Asset },
new() { ID = 400, Name = "Salary", Type = AccountDefinitionType.Income },
new() { ID = 600, Name = "Food", Type = AccountDefinitionType.Expense },
new() { ID = 990, Name = "Carryforward", Type = AccountDefinitionType.Carryforward }
};
var accountJournal = new AccountingDataJournal
{
Year = year.ToString(CultureInfo.InvariantCulture),
DateStart = (uint)year * 10000 + 101,
DateEnd = (uint)year * 10000 + 1231,
Booking = new List<AccountingDataJournalBooking>()
Booking = []
};
return new AccountingData
{
Setup = new AccountingDataSetup { Currency = "" },
Accounts = new List<AccountingDataAccountGroup>
{
new AccountingDataAccountGroup { Name = "Default", Account = defaultAccounts }
},
Journal = new List<AccountingDataJournal> { accountJournal }
Accounts = [new AccountingDataAccountGroup { Name = "Default", Account = defaultAccounts }],
Journal = [accountJournal]
};
}

Expand Down Expand Up @@ -112,7 +109,7 @@ internal AccountingDataJournal CloseYear(
{
DateStart = currentModelJournal.DateStart + 10000,
DateEnd = currentModelJournal.DateEnd + 10000,
Booking = new List<AccountingDataJournalBooking>()
Booking = []
};
newYearJournal.Year = newYearJournal.DateStart.ToDateTime().Year.ToString(CultureInfo.InvariantCulture);
this.Journal.Add(newYearJournal);
Expand Down Expand Up @@ -149,8 +146,8 @@ internal AccountingDataJournal CloseYear(
{
Date = newYearJournal.DateStart,
ID = bookingId,
Debit = new List<BookingValue>(),
Credit = new List<BookingValue>(),
Debit = [],
Credit = [],
Opening = true
};
newYearJournal.Booking.Add(newBooking);
Expand Down Expand Up @@ -196,15 +193,15 @@ private bool MergeYearsIntoJournal()
var result = false;
foreach (var year in this.Years)
{
this.Journal ??= new List<AccountingDataJournal>();
this.Journal ??= [];

string oldYearName = year.Name.ToString(CultureInfo.InvariantCulture);
var journal = this.Journal.SingleOrDefault(x => x.Year == oldYearName);
if (journal == null)
{
journal = new AccountingDataJournal
{
Year = oldYearName, Booking = new List<AccountingDataJournalBooking>()
Year = oldYearName, Booking = []
};
this.Journal.Add(journal);
}
Expand Down Expand Up @@ -296,8 +293,8 @@ public partial class AccountDefinitionImportMapping
public bool IsValid()
{
return
this.Columns.Any(x => x.Target == AccountDefinitionImportMappingColumnTarget.Date)
&& this.Columns.Any(x => x.Target == AccountDefinitionImportMappingColumnTarget.Value);
this.Columns.Exists(x => x.Target == AccountDefinitionImportMappingColumnTarget.Date)
&& this.Columns.Exists(x => x.Target == AccountDefinitionImportMappingColumnTarget.Value);
}
}

Expand All @@ -317,14 +314,14 @@ public static AccountingDataJournal SafeGetLatest(this IList<AccountingDataJourn
new AccountingDataJournal
{
Year = today.Year.ToString(CultureInfo.InvariantCulture),
DateStart = new DateTime(today.Year, 1, 1).ToAccountingDate(),
DateEnd = new DateTime(today.Year, december, decemberLast).ToAccountingDate(),
Booking = new List<AccountingDataJournalBooking>()
DateStart = new DateTime(today.Year, 1, 1, 0, 0, 0, DateTimeKind.Local).ToAccountingDate(),
DateEnd = new DateTime(today.Year, december, decemberLast, 0, 0, 0, DateTimeKind.Local).ToAccountingDate(),
Booking = []
});
}

var latest = journals.Last();
latest.Booking ??= new List<AccountingDataJournalBooking>();
var latest = journals[^1];
latest.Booking ??= [];

return latest;
}
Expand Down Expand Up @@ -356,7 +353,7 @@ internal string FormatName()
public partial class AccountDefinitionImportMappingPattern
{
private Regex? regex;
internal Regex Regex => this.regex ??= new Regex(this.Expression, RegexOptions.Compiled);
internal Regex Regex => this.regex ??= new Regex(this.Expression, RegexOptions.Compiled, TimeSpan.FromSeconds(1));
}

public partial class BookingValue
Expand Down
8 changes: 4 additions & 4 deletions src/SimpleAccounting/Model/ProjectData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,25 +349,25 @@ private EditBookingViewModel BookingFromJournal(
if (journalEntry.Credit.Count > 1)
{
journalEntry.Credit.Select(x => x.ToSplitModel()).ToList().ForEach(bookingModel.CreditSplitEntries.Add);
var theDebit = journalEntry.Debit.First();
var theDebit = journalEntry.Debit[0];
bookingModel.DebitAccount = theDebit.Account;
bookingModel.BookingText = theDebit.Text;
bookingModel.BookingValue = theDebit.Value.ToViewModel();
}
else if (journalEntry.Debit.Count > 1)
{
journalEntry.Debit.Select(x => x.ToSplitModel()).ToList().ForEach(bookingModel.DebitSplitEntries.Add);
var theCredit = journalEntry.Credit.First();
var theCredit = journalEntry.Credit[0];
bookingModel.CreditAccount = theCredit.Account;
bookingModel.BookingText = theCredit.Text;
bookingModel.BookingValue = theCredit.Value.ToViewModel();
}
else
{
var theDebit = journalEntry.Debit.First();
var theDebit = journalEntry.Debit[0];
bookingModel.DebitAccount = theDebit.Account;
bookingModel.BookingValue = theDebit.Value.ToViewModel();
bookingModel.CreditAccount = journalEntry.Credit.First().Account;
bookingModel.CreditAccount = journalEntry.Credit[0].Account;
bookingModel.BookingText = theDebit.Text;
}

Expand Down
6 changes: 3 additions & 3 deletions src/SimpleAccounting/Presentation/AccountDesignViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AccountDesignViewModel()
this.Type = AccountDefinitionType.Credit;
this.IsActivated = true;

this.Groups = new List<AccountingDataAccountGroup> { new AccountingDataAccountGroup { Name = "MyGroup" } };
this.Groups = new List<AccountingDataAccountGroup> { new() { Name = "MyGroup" } };
this.Group = this.Groups.First();

this.IsImportActive = true;
Expand All @@ -40,11 +40,11 @@ public AccountDesignViewModel()
this.ImportValueSource = "Value column";
this.ImportValueIgnorePattern = "ignore value";

this.ImportRemoteAccounts = new List<AccountDefinition> { new AccountDefinition { ID = 100, Name = "Bank" } };
this.ImportRemoteAccounts = new List<AccountDefinition> { new() { ID = 100, Name = "Bank" } };
this.ImportPatterns.Add(
new ImportPatternViewModel
{
Expression = "RegEx", Value = 29.95, Account = this.ImportRemoteAccounts.First()
Expression = "RegEx", Value = 29.95, Account = this.ImportRemoteAccounts[0]
});
}
}
7 changes: 3 additions & 4 deletions src/SimpleAccounting/Presentation/AccountJournalViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public AccountJournalViewModel(IProjectData projectData)
this.projectData = projectData;
}

public ObservableCollection<AccountJournalItemViewModel> Items { get; }
= new ObservableCollection<AccountJournalItemViewModel>();
public ObservableCollection<AccountJournalItemViewModel> Items { get; } = [];

public AccountJournalItemViewModel? SelectedItem
{
Expand All @@ -43,9 +42,9 @@ public void Rebuild(ulong accountNumber)

var relevantBookings =
this.projectData.CurrentYear.Booking
.Where(b => b.Credit.Any(x => x.Account == accountNumber))
.Where(b => b.Credit.Exists(x => x.Account == accountNumber))
.Concat(
this.projectData.CurrentYear.Booking.Where(b => b.Debit.Any(x => x.Account == accountNumber)))
this.projectData.CurrentYear.Booking.Where(b => b.Debit.Exists(x => x.Account == accountNumber)))
.OrderBy(x => x.Date)
.ThenBy(x => x.ID);
double creditSum = 0;
Expand Down
3 changes: 1 addition & 2 deletions src/SimpleAccounting/Presentation/AccountViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public bool IsImportActive

public IList<AccountDefinition> ImportRemoteAccounts { get; set; } = new List<AccountDefinition>();

public ObservableCollection<ImportPatternViewModel> ImportPatterns { get; set; } =
new ObservableCollection<ImportPatternViewModel>();
public ObservableCollection<ImportPatternViewModel> ImportPatterns { get; set; } = [];

public ICommand SaveCommand => new RelayCommand(
_ => this.TryClose(true),
Expand Down
21 changes: 10 additions & 11 deletions src/SimpleAccounting/Presentation/AccountsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public AccountsViewModel(IWindowManager windowManager, IProjectData projectData)
this.projectData = projectData;
}

public ObservableCollection<AccountViewModel> AccountList { get; }
= new ObservableCollection<AccountViewModel>();
public ObservableCollection<AccountViewModel> AccountList { get; } = [];

public AccountViewModel? SelectedAccount
{
Expand Down Expand Up @@ -96,19 +95,19 @@ AccountViewModel CreateViewModel(
}

accountModel.IsImportActive = true;
var dateColumn = account.ImportMapping.Columns?.FirstOrDefault(
var dateColumn = account.ImportMapping.Columns?.Find(
x => x.Target == AccountDefinitionImportMappingColumnTarget.Date);
accountModel.ImportDateSource = dateColumn?.Source;
accountModel.ImportDateIgnorePattern = dateColumn?.IgnorePattern;
var nameColumn = account.ImportMapping.Columns?.FirstOrDefault(
var nameColumn = account.ImportMapping.Columns?.Find(
x => x.Target == AccountDefinitionImportMappingColumnTarget.Name);
accountModel.ImportNameSource = nameColumn?.Source;
accountModel.ImportNameIgnorePattern = nameColumn?.IgnorePattern;
var textColumn = account.ImportMapping.Columns?.FirstOrDefault(
var textColumn = account.ImportMapping.Columns?.Find(
x => x.Target == AccountDefinitionImportMappingColumnTarget.Text);
accountModel.ImportTextSource = textColumn?.Source;
accountModel.ImportTextIgnorePattern = textColumn?.IgnorePattern;
var valueColumn = account.ImportMapping.Columns?.FirstOrDefault(
var valueColumn = account.ImportMapping.Columns?.Find(
x => x.Target == AccountDefinitionImportMappingColumnTarget.Value);
accountModel.ImportValueSource = valueColumn?.Source;
accountModel.ImportValueIgnorePattern = valueColumn?.IgnorePattern;
Expand All @@ -124,7 +123,7 @@ AccountViewModel CreateViewModel(
{
Expression = x.Expression,
AccountId = x.AccountID,
Value = x.ValueSpecified ? x.Value.ToViewModel() : (double?)null
Value = x.ValueSpecified ? x.Value.ToViewModel() : null
}));

return accountModel;
Expand Down Expand Up @@ -156,7 +155,7 @@ public void SelectFirstAccount()
public void ShowNewAccountDialog()
{
// setup new view model for the new account
var accountGroup = this.projectData.Storage.Accounts.First();
var accountGroup = this.projectData.Storage.Accounts[0];
var accountVm = new AccountViewModel
{
DisplayName = Resources.Header_CreateAccount,
Expand Down Expand Up @@ -293,8 +292,8 @@ private void SaveImportConfiguration(AccountViewModel viewModel, AccountDefiniti
// save required items
accountDefinition.ImportMapping = new AccountDefinitionImportMapping
{
Columns = new List<AccountDefinitionImportMappingColumn>
{
Columns =
[
new AccountDefinitionImportMappingColumn
{
Target = AccountDefinitionImportMappingColumnTarget.Date,
Expand All @@ -307,7 +306,7 @@ private void SaveImportConfiguration(AccountViewModel viewModel, AccountDefiniti
Source = viewModel.ImportValueSource,
IgnorePattern = viewModel.ImportValueIgnorePattern
}
}
]
};

// save optional items
Expand Down
Loading

0 comments on commit 3eca0e4

Please sign in to comment.