Skip to content

Commit 4ce4394

Browse files
committed
Fix sonarcloud issues
1 parent 7294c72 commit 4ce4394

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,13 @@ public static XmlQualifiedName GetQualifiedName(this TypeModel typeModel)
312312
public static string GetUniqueTypeName(this NamespaceModel model, string name)
313313
{
314314
var n = name;
315+
var i = 2;
315316

316-
for (var i = 2; model.Types.ContainsKey(n) && model.Types[n] is not SimpleModel; i++)
317+
while (model.Types.ContainsKey(n) && model.Types[n] is not SimpleModel)
318+
{
317319
n = name + i;
320+
i++;
321+
}
318322

319323
return n;
320324
}

XmlSchemaClassGenerator/ModelBuilder.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace XmlSchemaClassGenerator
1111
{
1212
internal class ModelBuilder
1313
{
14+
private const string ItemName = "Item";
15+
private const string PropertyName = "Property";
16+
private const string ElementName = "Element";
1417
private readonly GeneratorConfiguration _configuration;
1518
private readonly XmlSchemaSet _set;
1619
private readonly Dictionary<XmlQualifiedName, HashSet<XmlSchemaAttributeGroup>> AttributeGroups = new();
@@ -868,12 +871,12 @@ private PropertyModel PropertyFromAttribute(TypeModel owningTypeModel, XmlSchema
868871
attributeQualifiedName = new XmlQualifiedName(typeName, owningTypeModel.XmlSchemaName.Namespace);
869872
// try to avoid name clashes
870873
if (NameExists(attributeQualifiedName))
871-
attributeQualifiedName = new[] { "Item", "Property", "Element" }.Select(s => new XmlQualifiedName(attributeQualifiedName.Name + s, attributeQualifiedName.Namespace)).First(n => !NameExists(n));
874+
attributeQualifiedName = new[] { ItemName, PropertyName, ElementName }.Select(s => new XmlQualifiedName(attributeQualifiedName.Name + s, attributeQualifiedName.Namespace)).First(n => !NameExists(n));
872875
}
873876
}
874877

875878
if (name == owningTypeModel.Name)
876-
name += "Property";
879+
name += PropertyName;
877880
}
878881

879882
name = owningTypeModel.GetUniquePropertyName(name, properties);
@@ -989,7 +992,7 @@ private PropertyModel PropertyFromElement(TypeModel owningTypeModel, XmlSchemaEl
989992
var name = _configuration.NamingProvider.ElementNameFromQualifiedName(effectiveElement.QualifiedName, effectiveElement);
990993
var originalName = name;
991994
if (name == owningTypeModel.Name)
992-
name += "Property"; // member names cannot be the same as their enclosing type
995+
name += PropertyName; // member names cannot be the same as their enclosing type
993996

994997
name = owningTypeModel.GetUniquePropertyName(name, properties);
995998

@@ -1023,7 +1026,7 @@ private XmlQualifiedName GetQualifiedName(TypeModel typeModel, XmlSchemaParticle
10231026
elementQualifiedName = new XmlQualifiedName(typeName, typeModel.XmlSchemaName.Namespace);
10241027
// try to avoid name clashes
10251028
if (NameExists(elementQualifiedName))
1026-
elementQualifiedName = new[] { "Item", "Property", "Element" }.Select(s => new XmlQualifiedName(elementQualifiedName.Name + s, elementQualifiedName.Namespace)).First(n => !NameExists(n));
1029+
elementQualifiedName = new[] { ItemName, PropertyName, ElementName }.Select(s => new XmlQualifiedName(elementQualifiedName.Name + s, elementQualifiedName.Namespace)).First(n => !NameExists(n));
10271030
}
10281031
}
10291032

XmlSchemaClassGenerator/TypeModel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,13 @@ public override CodeTypeDeclaration Generate()
385385
if (IsMixed && (BaseClass == null || (BaseClass is ClassModel && !AllBaseClasses.Any(b => b.IsMixed))))
386386
{
387387
var propName = "Text";
388+
var propertyIndex = 1;
388389

389390
// To not collide with any existing members
390-
for (var propertyIndex = 1; Properties.Exists(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal); propertyIndex++)
391+
while (Properties.Exists(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal))
391392
{
392393
propName = $"Text_{propertyIndex}";
394+
propertyIndex++;
393395
}
394396
// hack to generate automatic property
395397
var text = new CodeMemberField(typeof(string[]), propName + PropertyModel.GetAccessors()) { Attributes = MemberAttributes.Public };

0 commit comments

Comments
 (0)