Skip to content

Commit 5d8e168

Browse files
committed
fix for Codacy error: "Consider refactoring this method in order to remove the need for this 'out' modifier."
1 parent 6628888 commit 5d8e168

File tree

3 files changed

+66
-72
lines changed

3 files changed

+66
-72
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockFileVersionInfo.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,12 @@ public MockFileVersionInfo(
6060
FilePrivatePart = version.Revision;
6161
}
6262

63-
ProductVersionParser.Parse(
64-
productVersion,
65-
out int productMajor,
66-
out int productMinor,
67-
out int productBuild,
68-
out int productPrivate);
69-
70-
ProductMajorPart = productMajor;
71-
ProductMinorPart = productMinor;
72-
ProductBuildPart = productBuild;
73-
ProductPrivatePart = productPrivate;
63+
var parsedProductVersion = ProductVersionParser.Parse(productVersion);
64+
65+
ProductMajorPart = parsedProductVersion.Major;
66+
ProductMinorPart = parsedProductVersion.Minor;
67+
ProductBuildPart = parsedProductVersion.Build;
68+
ProductPrivatePart = parsedProductVersion.PrivatePart;
7469
}
7570

7671
/// <inheritdoc/>

src/TestableIO.System.IO.Abstractions.TestingHelpers/ProductVersionParser.cs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,28 @@ public static class ProductVersionParser
1313
/// mimicking the behavior of the <see cref="AssemblyInformationalVersionAttribute"/> attribute.
1414
/// </summary>
1515
/// <param name="productVersion">The product version string to parse.</param>
16-
/// <param name="productMajorPart">Outputs the major part of the version. Defaults to 0 if not found.</param>
17-
/// <param name="productMinorPart">Outputs the minor part of the version. Defaults to 0 if not found.</param>
18-
/// <param name="productBuildPart">Outputs the build part of the version. Defaults to 0 if not found.</param>
19-
/// <param name="productPrivatePart">Outputs the private part of the version. Defaults to 0 if not found.</param>
16+
/// <returns>
17+
/// A <see cref="ProductVersion"/> object containing the parsed major, minor, build, and private parts.
18+
/// If the input is invalid, returns a <see cref="ProductVersion"/> with all parts set to 0.
19+
/// </returns>
2020
/// <remarks>
2121
/// The method splits the input string into segments separated by dots ('.') and attempts to extract
2222
/// the leading numeric value from each segment. A maximum of 4 segments are processed; if more than
2323
/// 4 segments are present, all segments are ignored. Additionally, if a segment does not contain
2424
/// a valid numeric part at its start or it contains more than just a number, the rest of the segments are ignored.
2525
/// </remarks>
26-
public static void Parse(
27-
string productVersion,
28-
out int productMajorPart,
29-
out int productMinorPart,
30-
out int productBuildPart,
31-
out int productPrivatePart)
26+
public static ProductVersion Parse(string productVersion)
3227
{
33-
productMajorPart = 0;
34-
productMinorPart = 0;
35-
productBuildPart = 0;
36-
productPrivatePart = 0;
37-
3828
if (string.IsNullOrWhiteSpace(productVersion))
3929
{
40-
return;
30+
return new();
4131
}
4232

4333
var segments = productVersion.Split('.');
4434
if (segments.Length > 4)
4535
{
4636
// if more than 4 segments are present, all segments are ignored
47-
return;
37+
return new();
4838
}
4939

5040
var regex = new Regex(@"^\d+");
@@ -71,10 +61,39 @@ public static void Parse(
7161
}
7262
}
7363

74-
productMajorPart = parts[0];
75-
productMinorPart = parts[1];
76-
productBuildPart = parts[2];
77-
productPrivatePart = parts[3];
64+
return new()
65+
{
66+
Major = parts[0],
67+
Minor = parts[1],
68+
Build = parts[2],
69+
PrivatePart = parts[3]
70+
};
71+
}
72+
73+
/// <summary>
74+
/// Represents a product version with numeric parts for major, minor, build, and private versions.
75+
/// </summary>
76+
public class ProductVersion
77+
{
78+
/// <summary>
79+
/// Gets the major part of the version number
80+
/// </summary>
81+
public int Major { get; init; }
82+
83+
/// <summary>
84+
/// Gets the minor part of the version number
85+
/// </summary>
86+
public int Minor { get; init; }
87+
88+
/// <summary>
89+
/// Gets the build part of the version number
90+
/// </summary>
91+
public int Build { get; init; }
92+
93+
/// <summary>
94+
/// Gets the private part of the version number
95+
/// </summary>
96+
public int PrivatePart { get; init; }
7897
}
7998
}
8099
}

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/ProductVersionParserTests.cs

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,15 @@ public void ProductVersionParser_Parse_ShouldIgnoreTheSegmentsWhenThereAreMoreTh
1212
string productVersion = "1.2.3.4.5";
1313

1414
// Act
15-
ProductVersionParser.Parse(
16-
productVersion,
17-
out int productMajor,
18-
out int productMinor,
19-
out int productBuild,
20-
out int productPrivate);
15+
var parsedProductVersion = ProductVersionParser.Parse(productVersion);
2116

2217
// Assert
2318
Assert.Multiple(() =>
2419
{
25-
Assert.That(productMajor, Is.Zero);
26-
Assert.That(productMinor, Is.Zero);
27-
Assert.That(productBuild, Is.Zero);
28-
Assert.That(productPrivate, Is.Zero);
20+
Assert.That(parsedProductVersion.Major, Is.Zero);
21+
Assert.That(parsedProductVersion.Minor, Is.Zero);
22+
Assert.That(parsedProductVersion.Build, Is.Zero);
23+
Assert.That(parsedProductVersion.PrivatePart, Is.Zero);
2924
});
3025
}
3126

@@ -42,20 +37,15 @@ public void ProductVersionParser_Parse_ShouldSkipTheRestOfTheSegmentsWhenOneIsNo
4237
int expectedRevision)
4338
{
4439
// Act
45-
ProductVersionParser.Parse(
46-
productVersion,
47-
out int productMajor,
48-
out int productMinor,
49-
out int productBuild,
50-
out int productPrivate);
40+
var parsedProductVersion = ProductVersionParser.Parse(productVersion);
5141

5242
// Assert
5343
Assert.Multiple(() =>
5444
{
55-
Assert.That(productMajor, Is.EqualTo(expectedMajor));
56-
Assert.That(productMinor, Is.EqualTo(expectedMinor));
57-
Assert.That(productBuild, Is.EqualTo(expectedBuild));
58-
Assert.That(productPrivate, Is.EqualTo(expectedRevision));
45+
Assert.That(parsedProductVersion.Major, Is.EqualTo(expectedMajor));
46+
Assert.That(parsedProductVersion.Minor, Is.EqualTo(expectedMinor));
47+
Assert.That(parsedProductVersion.Build, Is.EqualTo(expectedBuild));
48+
Assert.That(parsedProductVersion.PrivatePart, Is.EqualTo(expectedRevision));
5949
});
6050
}
6151

@@ -76,20 +66,15 @@ public void ProductVersionParser_Parse_ShouldSkipTheRestOfTheSegmentsWhenOneCont
7666
int expectedRevision)
7767
{
7868
// Act
79-
ProductVersionParser.Parse(
80-
productVersion,
81-
out int productMajor,
82-
out int productMinor,
83-
out int productBuild,
84-
out int productPrivate);
69+
var parsedProductVersion = ProductVersionParser.Parse(productVersion);
8570

8671
// Assert
8772
Assert.Multiple(() =>
8873
{
89-
Assert.That(productMajor, Is.EqualTo(expectedMajor));
90-
Assert.That(productMinor, Is.EqualTo(expectedMinor));
91-
Assert.That(productBuild, Is.EqualTo(expectedBuild));
92-
Assert.That(productPrivate, Is.EqualTo(expectedRevision));
74+
Assert.That(parsedProductVersion.Major, Is.EqualTo(expectedMajor));
75+
Assert.That(parsedProductVersion.Minor, Is.EqualTo(expectedMinor));
76+
Assert.That(parsedProductVersion.Build, Is.EqualTo(expectedBuild));
77+
Assert.That(parsedProductVersion.PrivatePart, Is.EqualTo(expectedRevision));
9378
});
9479
}
9580

@@ -107,20 +92,15 @@ public void ProductVersionParser_Parse_ShouldParseEachProvidedSegment(
10792
int expectedRevision)
10893
{
10994
// Act
110-
ProductVersionParser.Parse(
111-
productVersion,
112-
out int productMajor,
113-
out int productMinor,
114-
out int productBuild,
115-
out int productPrivate);
95+
var parsedProductVersion = ProductVersionParser.Parse(productVersion);
11696

11797
// Assert
11898
Assert.Multiple(() =>
11999
{
120-
Assert.That(productMajor, Is.EqualTo(expectedMajor));
121-
Assert.That(productMinor, Is.EqualTo(expectedMinor));
122-
Assert.That(productBuild, Is.EqualTo(expectedBuild));
123-
Assert.That(productPrivate, Is.EqualTo(expectedRevision));
100+
Assert.That(parsedProductVersion.Major, Is.EqualTo(expectedMajor));
101+
Assert.That(parsedProductVersion.Minor, Is.EqualTo(expectedMinor));
102+
Assert.That(parsedProductVersion.Build, Is.EqualTo(expectedBuild));
103+
Assert.That(parsedProductVersion.PrivatePart, Is.EqualTo(expectedRevision));
124104
});
125105
}
126106
}

0 commit comments

Comments
 (0)