Skip to content

Commit

Permalink
Minor analyzer code-fixes (vladimir-aubrecht#3)
Browse files Browse the repository at this point in the history
vladimir-aubrecht authored Mar 5, 2020
1 parent 47d4d6e commit a8034f7
Showing 31 changed files with 125 additions and 157 deletions.
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ public override int GetHashCode()

public override string ToString()
{
return String.Format("{0} {1} {2} {3}", this.Code, this.Name, this.Price, this.Country);
return $"{this.Code} {this.Name} {this.Price} {this.Country}";
}

public static bool operator ==(CurrencyDescriptor cd1, CurrencyDescriptor cd2)
6 changes: 3 additions & 3 deletions StatementParser/ExchangeRateProvider/Models/CurrencyList.cs
Original file line number Diff line number Diff line change
@@ -4,13 +4,13 @@ namespace ExchangeRateProvider.Models
{
internal class CurrencyList : ICurrencyList
{
private Dictionary<string, CurrencyDescriptor> currencyList = new Dictionary<string, CurrencyDescriptor>();
private readonly Dictionary<string, CurrencyDescriptor> currencyList = new Dictionary<string, CurrencyDescriptor>();

public CurrencyDescriptor this[string currencyCode] => currencyList[currencyCode.ToLower()];

public bool IsEmpty { get { return currencyList.Count == 0; } }
public bool IsEmpty => currencyList.Count == 0;

public CurrencyList(IList<CurrencyDescriptor> currencyList)
public CurrencyList(IList<CurrencyDescriptor> currencyList)
{
foreach (var currencyDescriptor in currencyList)
{
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ namespace ExchangeRateProvider.Providers.Czk
{
public class CzechNationalBankProvider : IExchangeProvider
{
private string apiUrl = "http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.xml?date=";
private const string ApiUrl = "http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.xml?date=";

public Task<ICurrencyList> FetchCurrencyListByDateAsync(DateTime date)
{
@@ -38,7 +38,7 @@ private string CreateUrlByDate(DateTime date)
{
var czechDate = date.Day + "." + date.Month + "." + date.Year;

return apiUrl + czechDate;
return ApiUrl + czechDate;
}
}
}
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ namespace ExchangeRateProvider.Providers.Czk
{
public class KurzyCzProvider : IExchangeProvider
{
private string apiUrl = "https://www.kurzy.cz/kurzy-men/jednotny-kurz/";
private const string ApiUrl = "https://www.kurzy.cz/kurzy-men/jednotny-kurz/";

public KurzyCzProvider()
{
@@ -32,12 +32,12 @@ public Task<ICurrencyList> FetchCurrencyListByDateAsync(DateTime date)
{
var responseStream = response.GetResponseStream();
var encoding = Encoding.GetEncoding(response.CharacterSet);
using (var sr = new StreamReader(responseStream, encoding))
{
var htmlContent = sr.ReadToEnd();
htmlDocument.LoadHtml(htmlContent);
}
}

using var sr = new StreamReader(responseStream, encoding);

var htmlContent = sr.ReadToEnd();
htmlDocument.LoadHtml(htmlContent);
}

var table = htmlDocument.DocumentNode.SelectNodes("//*[@id=\"leftcolumn\"]/div[2]/div[2]/table/tr");

@@ -50,7 +50,7 @@ public Task<ICurrencyList> FetchCurrencyListByDateAsync(DateTime date)
var name = this.SanitizeValue(cellNode[0].SelectNodes("a/span")[1].InnerHtml);
var code = this.SanitizeValue(cellNode[2].InnerHtml);
var amount = Convert.ToDecimal(this.SanitizeValue(cellNode[3].InnerHtml));
var price = Decimal.Parse(this.SanitizeValue(cellNode[4].InnerHtml), System.Globalization.NumberStyles.AllowDecimalPoint, CultureInfo.GetCultureInfo("en-US"));
var price = Decimal.Parse(this.SanitizeValue(cellNode[4].InnerHtml), NumberStyles.AllowDecimalPoint, CultureInfo.GetCultureInfo("en-US"));

output.Add(new CurrencyDescriptor(code, name, price, amount, country));
}
@@ -65,7 +65,7 @@ private string SanitizeValue(string value)

private string CreateUrlByDate(DateTime date)
{
return apiUrl + date.Year;
return ApiUrl + date.Year;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Reflection;
using System.Text.RegularExpressions;
using StatementParser.Models;

namespace TaxReporterCLI.Models.Attributes
namespace StatementParser.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class DescriptionAttribute : Attribute
{
private static readonly Regex placeHolderRegex = new Regex("\\{([^\\}]+)\\}", RegexOptions.Compiled);
private static readonly Regex PlaceHolderRegex = new Regex("\\{([^\\}]+)\\}", RegexOptions.Compiled);

private string Description { get; }

public DescriptionAttribute(string description)
@@ -18,14 +19,14 @@ public DescriptionAttribute(string description)
public static string ConstructDescription(PropertyInfo propertyInfo, object instanceHoldingProperty)
{
var attribute = propertyInfo.GetCustomAttribute<DescriptionAttribute>(true);

if (attribute == null)
{
return propertyInfo.Name;
}

var output = attribute.Description;
var matches = placeHolderRegex.Matches(attribute.Description);
var matches = PlaceHolderRegex.Matches(attribute.Description);

foreach (Match match in matches)
{
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using TaxReporterCLI.Models.Attributes;
using StatementParser.Attributes;

namespace StatementParser.Models
{
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ public class DividendTransaction : Transaction
public decimal Income { get; }

public decimal Tax { get; }

public DividendTransaction(DividendTransaction dividendTransaction) : this(dividendTransaction.Broker, dividendTransaction.Date, dividendTransaction.Name, dividendTransaction.Income, dividendTransaction.Tax, dividendTransaction.Currency)
{
}
5 changes: 3 additions & 2 deletions StatementParser/StatementParser/Models/ESPPTransaction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using TaxReporterCLI.Models.Attributes;
using StatementParser.Attributes;

namespace StatementParser.Models
{
@@ -10,15 +10,16 @@ public class ESPPTransaction : Transaction

[Description("Market Price")]
public decimal MarketPrice { get; }

public decimal Amount { get; }

public decimal Profit { get; }

[Description("Total Profit")]
public decimal TotalProfit { get; }

public ESPPTransaction(ESPPTransaction eSPPTransaction) : this(eSPPTransaction.Broker, eSPPTransaction.Date, eSPPTransaction.Name, eSPPTransaction.Currency, eSPPTransaction.PurchasePrice, eSPPTransaction.MarketPrice, eSPPTransaction.Amount)
{

}

public ESPPTransaction(Broker broker, DateTime date, string name, Currency currency, decimal purchasePrice, decimal marketPrice, decimal amount) : base(broker, date, name, currency)
6 changes: 5 additions & 1 deletion StatementParser/StatementParser/Models/SaleTransaction.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using TaxReporterCLI.Models.Attributes;
using StatementParser.Attributes;

namespace StatementParser.Models
{
@@ -29,9 +29,13 @@ public SaleTransaction(Broker broker, DateTime date, string name, Currency curre

[Description("Sale Price")]
public decimal SalePrice { get; }

public decimal Commission { get; }

public decimal Taxes { get; }

public decimal Swap { get; }

public decimal Profit { get; }

public override string ToString()
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System;
using CsvHelper.Configuration.Attributes;
using StatementParser.Models;
@@ -27,8 +26,8 @@ internal class StatementRowModel

public override string ToString()
{
var dateString = Date.HasValue ? Date.Value.ToShortDateString() : null;
var incomeString = Income.HasValue ? Income.Value : 0;
var dateString = Date?.ToShortDateString();
var incomeString = Income ?? 0;
return $"{nameof(Date)}: {dateString} {nameof(Name)}: {Name} {nameof(ISIN)}: {ISIN} {nameof(Description)}: {Description} {nameof(Currency)}: {Currency} {nameof(Income)}: {incomeString}";
}
}
Original file line number Diff line number Diff line change
@@ -12,14 +12,9 @@ namespace StatementParser.Parsers.Brokers.Degiro
internal class DegiroParser : ITransactionParser
{
private bool CanParse(string statementFilePath)
{
if (!File.Exists(statementFilePath) || Path.GetExtension(statementFilePath).ToLowerInvariant() != ".csv")
{
return false;
}

return true;
}
{
return File.Exists(statementFilePath) && Path.GetExtension(statementFilePath).ToLowerInvariant() == ".csv";
}

public IList<Transaction> Parse(string statementFilePath)
{
@@ -51,7 +46,7 @@ public IList<Transaction> Parse(string statementFilePath)

private decimal SearchForTax(IList<StatementRowModel> statementRows, string isin, DateTime date)
{
var row = statementRows.Where(i => i.Date.Value == date && i.ISIN == isin && i.Description.Contains("z dividendy")).FirstOrDefault();
var row = statementRows.FirstOrDefault(i => i.Date.Value == date && i.ISIN == isin && i.Description.Contains("z dividendy"));
return row.Income.Value;
}

@@ -65,12 +60,11 @@ private IList<StatementRowModel> LoadStatementModel(string statementFilePath)
ShortDatePattern = "dd-MM-yyyy"
};

using (var reader = new StreamReader(statementFilePath))
using (var csv = new CsvReader(reader, ci))
{
return csv.GetRecords<StatementRowModel>().Where(i => i.Date != null).ToList();
}
}
using var reader = new StreamReader(statementFilePath);
using var csv = new CsvReader(reader, ci);

return csv.GetRecords<StatementRowModel>().Where(i => i.Date != null).ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Original file line number Diff line number Diff line change
@@ -11,14 +11,9 @@ namespace StatementParser.Parsers.Brokers.FxChoice
internal class FxChoiceStatementParser : ITransactionParser
{
private bool CanParse(string statementFilePath)
{
if (!File.Exists(statementFilePath) || Path.GetExtension(statementFilePath).ToLowerInvariant() != ".htm")
{
return false;
}

return true;
}
{
return File.Exists(statementFilePath) && Path.GetExtension(statementFilePath).ToLowerInvariant() == ".htm";
}

public IList<Transaction> Parse(string statementFilePath)
{
Original file line number Diff line number Diff line change
@@ -13,14 +13,9 @@ namespace StatementParser.Parsers.Brokers.Fidelity
internal class FidelityStatementParser : ITransactionParser
{
private bool CanParse(string statementFilePath)
{
if (!File.Exists(statementFilePath) || Path.GetExtension(statementFilePath).ToLowerInvariant() != ".pdf")
{
return false;
}

return true;
}
{
return File.Exists(statementFilePath) && Path.GetExtension(statementFilePath).ToLowerInvariant() == ".pdf";
}

public IList<Transaction> Parse(string statementFilePath)
{
@@ -52,7 +47,7 @@ public IList<Transaction> Parse(string statementFilePath)

public string SearchForCompanyName(ActivityBuyModel[] activityBuyModels, ESPPModel esppRow)
{
string removeLastCharFunc(decimal number)
static string removeLastCharFunc(decimal number)
{
return number.ToString().Remove(number.ToString().Length - 1);
}
@@ -73,7 +68,7 @@ string removeLastCharFunc(decimal number)

private decimal SearchForTaxString(ActivityTaxesModel[] activityTaxesModels, DateTime date)
{
return activityTaxesModels.Where(i => i.Date == date.ToString("MM/dd", CultureInfo.InvariantCulture)).FirstOrDefault()?.Tax ?? 0;
return activityTaxesModels.FirstOrDefault(i => i.Date == date.ToString("MM/dd", CultureInfo.InvariantCulture))?.Tax ?? 0;
}

private ESPPTransaction CreateESPPTransaction(ActivityBuyModel[] activityBuyModels, ESPPModel esppRow)
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ public static T ReadObject<T>(this CsvReader csvReader, int headerFieldIndex, Fu
dataSets[key].SetValue(output, value);
}

dataSets[key].PropertyType.GetMethod("Add").Invoke(value, new object[] { record });
dataSets[key].PropertyType.GetMethod("Add").Invoke(value, new[] { record });
}
return output;
}
Original file line number Diff line number Diff line change
@@ -14,14 +14,9 @@ namespace StatementParser.Parsers.Brokers.Lynx
internal class LynxCsvParser : ITransactionParser
{
private bool CanParse(string statementFilePath)
{
if (!File.Exists(statementFilePath) || Path.GetExtension(statementFilePath).ToLowerInvariant() != ".csv")
{
return false;
}

return true;
}
{
return File.Exists(statementFilePath) && Path.GetExtension(statementFilePath).ToLowerInvariant() == ".csv";
}

public IList<Transaction> Parse(string statementFilePath)
{
@@ -52,9 +47,9 @@ public IList<Transaction> Parse(string statementFilePath)

private decimal SearchForTax(DividendsRowModel dividend, List<WithholdingTaxRowModel> withholdingTaxes)
{
var tax = withholdingTaxes.Where(i => dividend.Description.Contains(Regex.Replace(i.Description, " - [^ ]+ Tax", "", RegexOptions.Compiled)) && i.Date == dividend.Date).FirstOrDefault();
var tax = withholdingTaxes.FirstOrDefault(i => dividend.Description.Contains(Regex.Replace(i.Description, " - [^ ]+ Tax", "", RegexOptions.Compiled)) && i.Date == dividend.Date);

return (tax == null) ? 0 : tax.Amount;
return tax?.Amount ?? 0;
}

private StatementModel LoadStatementModel(string statementFilePath)
@@ -67,22 +62,21 @@ private StatementModel LoadStatementModel(string statementFilePath)
ShortDatePattern = "yyyy-MM-dd"
};

using (var reader = new StreamReader(statementFilePath))
using (var csv = new CsvReader(reader, ci))
{
var statement = csv.ReadObject<StatementModel>(0, () => csv.GetField(1) == "Header");
using var reader = new StreamReader(statementFilePath);
using var csv = new CsvReader(reader, ci);

if (statement == null)
{
return null;
}
var statement = csv.ReadObject<StatementModel>(0, () => csv.GetField(1) == "Header");

statement.Dividends = statement.Dividends.Where(i => i.Date != null).ToList();
statement.WithholdingTaxes = statement.WithholdingTaxes.Where(i => i.Date != null).ToList();
if (statement == null)
{
return null;
}

return statement;
}
}
statement.Dividends = statement.Dividends.Where(i => i.Date != null).ToList();
statement.WithholdingTaxes = statement.WithholdingTaxes.Where(i => i.Date != null).ToList();

return statement;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Loading

0 comments on commit a8034f7

Please sign in to comment.