Skip to content

Commit

Permalink
code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Jul 25, 2023
1 parent e91efd7 commit 60f7bc1
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private async Task RemoveUserFromCacheAsync(User user)
}

var users = (await AllUsersAsync()).Users;
return users.FirstOrDefault(p => p.Id == userTableId);
return users.Find(p => p.Id == userTableId);
}

/// <summary>
Expand Down Expand Up @@ -284,7 +284,7 @@ public async Task<SignUpResult> SignUpAsync(string name,
}

// Add a user role based on a user id
var roleToAdd = roles.FirstOrDefault(p => p.Code == GetRoleAddToUser(identifier,user));
var roleToAdd = roles.Find(p => p.Code == GetRoleAddToUser(identifier,user));
AddToRole(user, roleToAdd);

if (credentialType == null)
Expand Down Expand Up @@ -555,7 +555,7 @@ public async Task<ValidateResult> ValidateAsync(string credentialTypeCode,
return new ValidateResult(success: false, error: ValidateResultError.SecretNotValid);
}

var userData = (await AllUsersAsync()).Users.FirstOrDefault(p => p.Id == credential.UserId);
var userData = (await AllUsersAsync()).Users.Find(p => p.Id == credential.UserId);
if ( userData == null )
{
return new ValidateResult(success: false, error: ValidateResultError.UserNotFound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ public static List<string> Compare(FileIndexItem sourceIndexItem, FileIndexItem?
}

// Last Edited is not needed
if ( differenceList.Any(p => p == nameof(FileIndexItem.LastEdited).ToLowerInvariant()) )
if ( differenceList.Exists(p => p == nameof(FileIndexItem.LastEdited).ToLowerInvariant()) )
{
differenceList.Remove(nameof(FileIndexItem.LastEdited).ToLowerInvariant());
}

// Last Changed is a generated list
if ( differenceList.Any(p => p == nameof(FileIndexItem.LastChanged).ToLowerInvariant()) )
if ( differenceList.Exists(p => p == nameof(FileIndexItem.LastChanged).ToLowerInvariant()) )
{
differenceList.Remove(nameof(FileIndexItem.LastChanged).ToLowerInvariant());
}
Expand All @@ -134,7 +134,7 @@ public static FileIndexItem Set(FileIndexItem? sourceIndexItem, string fieldName

PropertyInfo[] propertiesA = new FileIndexItem().GetType().GetProperties(
BindingFlags.Public | BindingFlags.Instance);
var property = propertiesA.FirstOrDefault(p =>
var property = Array.Find(propertiesA,p =>
string.Equals(p.Name, fieldName, StringComparison.InvariantCultureIgnoreCase));

var fieldType = fieldContent.GetType();
Expand All @@ -158,7 +158,7 @@ public static FileIndexItem Set(FileIndexItem? sourceIndexItem, string fieldName
{
PropertyInfo[] propertiesA = new FileIndexItem().GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance);
return propertiesA.FirstOrDefault(p =>
return Array.Find(propertiesA,p =>
string.Equals(p.Name, fieldName, StringComparison.InvariantCultureIgnoreCase))?
.GetValue(sourceIndexItem, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public DateTime ParseDateTimeFromFileName()
{
// Depends on 'AppSettingsProvider.Structure'
// depends on SourceFullFilePath
if(string.IsNullOrEmpty(SourceFullFilePath)) {return new DateTime();}
if ( string.IsNullOrEmpty(SourceFullFilePath) )
{
return new DateTime(0, DateTimeKind.Utc);
}

var fileName = Path.GetFileNameWithoutExtension(SourceFullFilePath);

Expand Down
2 changes: 1 addition & 1 deletion starsky/starsky.foundation.database/Query/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public void CacheUpdateItem(List<FileIndexItem> updateStatusContent)
// make it a list to avoid enum errors
displayFileFolders = displayFileFolders.ToList();

var obj = displayFileFolders.FirstOrDefault(p => p.FilePath == item.FilePath);
var obj = displayFileFolders.Find(p => p.FilePath == item.FilePath);
if ( obj != null )
{
// remove add again
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async Task<List<FileIndexItem>> LocalQuery(ApplicationDbContext context)
fileHashesList.Contains(p.FileHash!)).ToListAsync();
foreach ( var fileHash in fileHashesList )
{
if ( result.FirstOrDefault(p => p.FileHash == fileHash) == null )
if ( result.Find(p => p.FileHash == fileHash) == null )
{
result.Add(new FileIndexItem(){FileHash = fileHash,
Status = FileIndexItem.ExifStatus.NotFoundNotInIndex});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public partial class Query
};
}

var currentFileIndexItem = fileIndexItemsList.FirstOrDefault(p => p.FileName == fileName);
var currentFileIndexItem = fileIndexItemsList.Find(p => p.FileName == fileName);

// Could be not found or not in directory cache
if ( currentFileIndexItem == null )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ThumbnailQuery(ApplicationDbContext context, IServiceScopeFactory? scopeF

public Task<List<ThumbnailItem>?> AddThumbnailRangeAsync(List<ThumbnailResultDataTransferModel> thumbnailItems)
{
if ( thumbnailItems.Any(p => string.IsNullOrEmpty(p.FileHash) ) )
if ( thumbnailItems.Exists(p => string.IsNullOrEmpty(p.FileHash) ) )
{
throw new ArgumentNullException(nameof(thumbnailItems), "[AddThumbnailRangeAsync] FileHash is null or empty");
}
Expand Down Expand Up @@ -208,8 +208,8 @@ private static async Task<bool> RenameInternalAsync(ApplicationDbContext dbConte
var beforeOrNewItems = await dbContext.Thumbnails.Where(p =>
p.FileHash == beforeFileHash || p.FileHash == newFileHash).ToListAsync();

var beforeItem = beforeOrNewItems.FirstOrDefault(p => p.FileHash == beforeFileHash);
var newItem = beforeOrNewItems.FirstOrDefault(p => p.FileHash == newFileHash);
var beforeItem = beforeOrNewItems.Find(p => p.FileHash == beforeFileHash);
var newItem = beforeOrNewItems.Find(p => p.FileHash == newFileHash);

if ( beforeItem == null) return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private static List<Assembly> GetEntryAssemblyReferencedAssemblies(List<Assembly
// ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
foreach ( var assemblyFilter in assemblyFilters )
{
var isThere = assemblies.Any(p => p.FullName == assemblyName.FullName);
var isThere = assemblies.Exists(p => p.FullName == assemblyName.FullName);
if (IsWildcardMatch(assemblyName.Name!, assemblyFilter) && !isThere )
{
assemblies.Add(AppDomain.CurrentDomain.Load(assemblyName));
Expand All @@ -165,7 +165,7 @@ private static Assembly[] GetReferencedAssemblies(List<Assembly> assemblies, IEn
foreach ( var referencedAssembly in assembly.GetReferencedAssemblies() )
{
if ( IsWildcardMatch(referencedAssembly.Name!, assemblyFilter)
&& assemblies.All(p => p.FullName != referencedAssembly.FullName) )
&& assemblies.TrueForAll(p => p.FullName != referencedAssembly.FullName) )
{
assemblies.Add(Assembly.Load(referencedAssembly));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -28,7 +29,7 @@ public static List<string> Compare(AppSettings sourceIndexItem, object? updateOb
foreach ( var propertyB in propertiesB )
{
// only for when TransferObject is Nullable<bool> and AppSettings is bool
var propertyInfoFromA = propertiesA.FirstOrDefault(p => p.Name == propertyB.Name);
var propertyInfoFromA = Array.Find(propertiesA, p => p.Name == propertyB.Name);
if ( propertyInfoFromA == null ) continue;
if (propertyInfoFromA.PropertyType == typeof(bool?) && propertyB.PropertyType == typeof(bool?))
{
Expand Down Expand Up @@ -277,8 +278,7 @@ internal static void CompareInt(string propertyName, AppSettings sourceIndexItem
/// <returns></returns>
private static object? GetPropertyValue(object car, string propertyName)
{
return car.GetType().GetProperties()
.FirstOrDefault(pi => pi.Name == propertyName)?
return Array.Find(car.GetType().GetProperties(), pi => pi.Name == propertyName)?
.GetValue(car, null);
}

Expand Down
11 changes: 9 additions & 2 deletions starsky/starsky.foundation.platform/Helpers/DateAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public static DateTime GetBuildDate(Assembly assembly)
// <SourceRevisionId>build$([System.DateTime]::UtcNow.ToString("yyyyMMddHHmmss"))</SourceRevisionId>
// </PropertyGroup>
var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if ( attribute?.InformationalVersion == null ) return new DateTime();
if ( attribute?.InformationalVersion == null )
{
return new DateTime(0, DateTimeKind.Utc);

Check warning on line 25 in starsky/starsky.foundation.platform/Helpers/DateAssembly.cs

View check run for this annotation

Codecov / codecov/patch

starsky/starsky.foundation.platform/Helpers/DateAssembly.cs#L25

Added line #L25 was not covered by tests
}

var value = attribute.InformationalVersion;
return ParseBuildTime(value);
}
Expand All @@ -29,7 +33,10 @@ internal static DateTime ParseBuildTime(string value)
{
const string buildVersionMetadataPrefix = "+build";
var index = value.IndexOf(buildVersionMetadataPrefix, StringComparison.Ordinal);
if ( index <= 0 ) return new DateTime();
if ( index <= 0 )
{
return new DateTime(0, DateTimeKind.Utc);
}
value = value.Substring(index + buildVersionMetadataPrefix.Length);
return DateTime.TryParseExact(value, "yyyyMMddHHmmss", CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal, out var result) ? result : new DateTime();
Expand Down
6 changes: 3 additions & 3 deletions starsky/starsky.foundation.platform/Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public bool IsReadOnly(string f)
{
if (!ReadOnlyFolders.Any() ) return false;

var result = ReadOnlyFolders.FirstOrDefault(f.Contains);
var result = ReadOnlyFolders.Find(f.Contains);
return result != null;
}

Expand Down Expand Up @@ -1038,9 +1038,9 @@ public string DatabasePathToFilePath(string databaseFilePath, bool checkIfExist
/// </summary>
/// <param name="input">the input, the env should start with a $</param>
/// <returns>the value or the input when nothing is found</returns>
internal string ReplaceEnvironmentVariable(string input)
internal static string ReplaceEnvironmentVariable(string input)
{
if ( string.IsNullOrEmpty(input) || !input.StartsWith("$") ) return input;
if ( string.IsNullOrEmpty(input) || !input.StartsWith('$') ) return input;
var value = Environment.GetEnvironmentVariable(input.Remove(0, 1));
return string.IsNullOrEmpty(value) ? input : value;
}
Expand Down
2 changes: 1 addition & 1 deletion starsky/starsky.foundation.readmeta/Helpers/GeoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static GeoListItem ParseIsoString(string isoStr)

// Check for minimum length
// Check for trailing slash
if ( isoStr.Length < 18 || !isoStr.EndsWith("/") )
if ( isoStr.Length < 18 || !isoStr.EndsWith('/') )
{
return geoListItem;
}
Expand Down
2 changes: 1 addition & 1 deletion starsky/starskytest/FakeMocks/FakeIQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public Task<List<FileIndexItem>> GetObjectsByFileHashAsync(List<string> fileHash

foreach ( var fileHash in fileHashesList )
{
if ( result.FirstOrDefault(p => p.FileHash == fileHash) == null )
if ( result.Find(p => p.FileHash == fileHash) == null )
{
result.Add(new FileIndexItem(){FileHash = fileHash,
Status = FileIndexItem.ExifStatus.NotFoundNotInIndex});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ public void AppSettingsProviderTest_SqLiteFullPathTest()
[TestMethod]
public void ReplaceEnvironmentVariable_Nothing()
{
var value = _appSettings.ReplaceEnvironmentVariable(string.Empty);
var value = AppSettings.ReplaceEnvironmentVariable(string.Empty);
Assert.AreEqual(string.Empty, value);
}

[TestMethod]
public void ReplaceEnvironmentVariable_SomethingThatShouldBeIgnored()
{
var value = _appSettings.ReplaceEnvironmentVariable("/test");
var value = AppSettings.ReplaceEnvironmentVariable("/test");
Assert.AreEqual("/test", value);
}

[TestMethod]
public void ReplaceEnvironmentVariable_Non_Existing_EnvVariable()
{
var value = _appSettings.ReplaceEnvironmentVariable("$sdhfdskfbndsfjb38");
var value = AppSettings.ReplaceEnvironmentVariable("$sdhfdskfbndsfjb38");
Assert.AreEqual("$sdhfdskfbndsfjb38", value);
}

Expand All @@ -86,7 +86,7 @@ public void ReplaceEnvironmentVariable_Existing_EnvVariable()
{
Environment.SetEnvironmentVariable("test123456789","123456789");
// should start with a dollar sign
var value = _appSettings.ReplaceEnvironmentVariable("$test123456789");
var value = AppSettings.ReplaceEnvironmentVariable("$test123456789");
Assert.AreEqual("123456789", value);
}

Expand Down

0 comments on commit 60f7bc1

Please sign in to comment.