Skip to content

Commit 93aaec9

Browse files
chore(deps): update dependency sonaranalyzer.csharp to 10.25.0.139117 (#664)
* chore(deps): update dependency sonaranalyzer.csharp to 10.25.0.139117 * refactor: Refactor argument validation polyfills for .NET 8+ Reorganize and update ThrowIfInPast/ThrowIfInFuture overloads for DateOnly, DateTime, and DateTimeOffset. Move ThrowIfNullOrWhiteSpace under !NET8_0_OR_GREATER. Update XML docs to match new signatures and behaviors. --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Martin Stühmer <me@samtrion.net>
1 parent c82c693 commit 93aaec9

3 files changed

Lines changed: 55 additions & 55 deletions

File tree

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<GlobalPackageReference Include="Roslynator.CodeAnalysis.Analyzers" Version="4.15.0" />
1616
<GlobalPackageReference Include="Roslynator.CodeFixes" Version="4.15.0" />
1717
<GlobalPackageReference Include="Roslynator.Refactorings" Version="4.15.0" />
18-
<GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="10.23.0.137933" />
18+
<GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="10.25.0.139117" />
1919
</ItemGroup>
2020
<ItemGroup>
2121
<PackageVersion Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.6.2" />

src/NetEvolve.Arguments/ArgumentExceptionPolyfill.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,6 @@ public static void ThrowIfNullOrEmpty(
3434
throw new ArgumentException("The value cannot be an empty string.", paramName);
3535
}
3636
}
37-
38-
/// <summary>Throws an exception if <paramref name="argument"/> is <see langword="null"/>, empty, or consists only of white-space characters.</summary>
39-
/// <param name="argument">The string argument to validate.</param>
40-
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
41-
/// <exception cref="ArgumentNullException"><paramref name="argument"/> is <see langword="null"/>.</exception>
42-
/// <exception cref="ArgumentException"><paramref name="argument"/> is empty or consists only of white-space characters.</exception>
43-
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorwhitespace?view=net-10.0#system-argumentexception-throwifnullorwhitespace(system-string-system-string)" />
44-
public static void ThrowIfNullOrWhiteSpace(
45-
[NotNull] string? argument,
46-
[CallerArgumentExpression(nameof(argument))] string? paramName = null
47-
)
48-
{
49-
if (argument is null)
50-
{
51-
throw new ArgumentNullException(paramName);
52-
}
53-
54-
if (string.IsNullOrWhiteSpace(argument))
55-
{
56-
throw new ArgumentException(
57-
"The value cannot be an empty string or composed entirely of whitespace.",
58-
paramName
59-
);
60-
}
61-
}
6237
#endif
6338

6439
/// <summary>Throws an exception if <paramref name="argument"/> is <see langword="null"/> or empty.</summary>
@@ -149,6 +124,33 @@ public static void ThrowIfNullOrEmpty<T>(
149124
}
150125
}
151126

127+
#if !NET8_0_OR_GREATER
128+
/// <summary>Throws an exception if <paramref name="argument"/> is <see langword="null"/>, empty, or consists only of white-space characters.</summary>
129+
/// <param name="argument">The string argument to validate.</param>
130+
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
131+
/// <exception cref="ArgumentNullException"><paramref name="argument"/> is <see langword="null"/>.</exception>
132+
/// <exception cref="ArgumentException"><paramref name="argument"/> is empty or consists only of white-space characters.</exception>
133+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorwhitespace?view=net-10.0#system-argumentexception-throwifnullorwhitespace(system-string-system-string)" />
134+
public static void ThrowIfNullOrWhiteSpace(
135+
[NotNull] string? argument,
136+
[CallerArgumentExpression(nameof(argument))] string? paramName = null
137+
)
138+
{
139+
if (argument is null)
140+
{
141+
throw new ArgumentNullException(paramName);
142+
}
143+
144+
if (string.IsNullOrWhiteSpace(argument))
145+
{
146+
throw new ArgumentException(
147+
"The value cannot be an empty string or composed entirely of whitespace.",
148+
paramName
149+
);
150+
}
151+
}
152+
#endif
153+
152154
/// <summary>Throws an exception if <paramref name="argument"/> exceeds the specified maximum length.</summary>
153155
/// <param name="argument">The string argument to validate.</param>
154156
/// <param name="maxLength">The maximum allowed length.</param>

src/NetEvolve.Arguments/ArgumentOutOfRangeExceptionPolyfills.cs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -358,62 +358,62 @@ public static void ThrowIfOutOfRange<T>(
358358
}
359359

360360
#if NET8_0_OR_GREATER
361-
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the past relative to the current time.</summary>
362-
/// <param name="value">The <see cref="DateTimeOffset"/> argument to validate as not in the past.</param>
363-
/// <param name="timeProvider">The time provider to use for getting the current time. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
361+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the past relative to the current date.</summary>
362+
/// <param name="value">The <see cref="DateOnly"/> argument to validate as not in the past.</param>
363+
/// <param name="timeProvider">The time provider to use for getting the current date. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
364364
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
365365
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the past.</exception>
366366
public static void ThrowIfInPast(
367-
DateTimeOffset value,
367+
DateOnly value,
368368
TimeProvider? timeProvider = null,
369369
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
370370
)
371371
{
372-
var now = (timeProvider ?? TimeProvider.System).GetUtcNow();
373-
if (value < now)
372+
var today = DateOnly.FromDateTime((timeProvider ?? TimeProvider.System).GetUtcNow().UtcDateTime);
373+
if (value < today)
374374
{
375375
throw new ArgumentOutOfRangeException(
376376
paramName,
377377
value,
378-
$"Value must not be in the past. Current time: {now}."
378+
$"Value must not be in the past. Current date: {today}."
379379
);
380380
}
381381
}
382382

383-
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the future relative to the current time.</summary>
384-
/// <param name="value">The <see cref="DateTimeOffset"/> argument to validate as not in the future.</param>
383+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the past relative to the current time.</summary>
384+
/// <param name="value">The <see cref="DateTime"/> argument to validate as not in the past.</param>
385385
/// <param name="timeProvider">The time provider to use for getting the current time. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
386386
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
387-
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the future.</exception>
388-
public static void ThrowIfInFuture(
389-
DateTimeOffset value,
387+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the past.</exception>
388+
public static void ThrowIfInPast(
389+
DateTime value,
390390
TimeProvider? timeProvider = null,
391391
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
392392
)
393393
{
394-
var now = (timeProvider ?? TimeProvider.System).GetUtcNow();
395-
if (value > now)
394+
var now = (timeProvider ?? TimeProvider.System).GetUtcNow().UtcDateTime;
395+
if (value < now)
396396
{
397397
throw new ArgumentOutOfRangeException(
398398
paramName,
399399
value,
400-
$"Value must not be in the future. Current time: {now}."
400+
$"Value must not be in the past. Current time: {now}."
401401
);
402402
}
403403
}
404404

405405
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the past relative to the current time.</summary>
406-
/// <param name="value">The <see cref="DateTime"/> argument to validate as not in the past.</param>
406+
/// <param name="value">The <see cref="DateTimeOffset"/> argument to validate as not in the past.</param>
407407
/// <param name="timeProvider">The time provider to use for getting the current time. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
408408
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
409409
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the past.</exception>
410410
public static void ThrowIfInPast(
411-
DateTime value,
411+
DateTimeOffset value,
412412
TimeProvider? timeProvider = null,
413413
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
414414
)
415415
{
416-
var now = (timeProvider ?? TimeProvider.System).GetUtcNow().UtcDateTime;
416+
var now = (timeProvider ?? TimeProvider.System).GetUtcNow();
417417
if (value < now)
418418
{
419419
throw new ArgumentOutOfRangeException(
@@ -445,27 +445,25 @@ public static void ThrowIfInFuture(
445445
);
446446
}
447447
}
448-
#endif
449448

450-
#if NET8_0_OR_GREATER
451-
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the past relative to the current date.</summary>
452-
/// <param name="value">The <see cref="DateOnly"/> argument to validate as not in the past.</param>
453-
/// <param name="timeProvider">The time provider to use for getting the current date. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
449+
/// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is in the future relative to the current time.</summary>
450+
/// <param name="value">The <see cref="DateTimeOffset"/> argument to validate as not in the future.</param>
451+
/// <param name="timeProvider">The time provider to use for getting the current time. If <see langword="null"/>, <see cref="TimeProvider.System"/> is used.</param>
454452
/// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
455-
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the past.</exception>
456-
public static void ThrowIfInPast(
457-
DateOnly value,
453+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is in the future.</exception>
454+
public static void ThrowIfInFuture(
455+
DateTimeOffset value,
458456
TimeProvider? timeProvider = null,
459457
[Runtime.CompilerServices.CallerArgumentExpression(nameof(value))] string? paramName = null
460458
)
461459
{
462-
var today = DateOnly.FromDateTime((timeProvider ?? TimeProvider.System).GetUtcNow().UtcDateTime);
463-
if (value < today)
460+
var now = (timeProvider ?? TimeProvider.System).GetUtcNow();
461+
if (value > now)
464462
{
465463
throw new ArgumentOutOfRangeException(
466464
paramName,
467465
value,
468-
$"Value must not be in the past. Current date: {today}."
466+
$"Value must not be in the future. Current time: {now}."
469467
);
470468
}
471469
}

0 commit comments

Comments
 (0)