-
Notifications
You must be signed in to change notification settings - Fork 1
fix: resolve SonarCloud issues in remaining renderers #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using System; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using SkiaSharp; | ||
|
|
||
| using System.IO; | ||
|
|
@@ -63,7 +64,7 @@ public sealed class Base64QRCodeGraphicOptions | |
| /// </summary> | ||
| public class Base64QRCode : AbstractQRCode | ||
| { | ||
| private QRCode qr; | ||
| private readonly QRCode qr; | ||
|
|
||
| /// <summary> | ||
| /// Constructor without params to be used in COM Objects connections | ||
|
|
@@ -130,25 +131,6 @@ public string GetGraphic(Base64QRCodeGraphicOptions options) | |
| : RenderGraphic(options, options.ImageType); | ||
| } | ||
|
|
||
| private string RenderGraphic(Base64QRCodeGraphicOptions options, ImageType imgType) | ||
| { | ||
| var base64 = string.Empty; | ||
| using (SKBitmap bmp = qr.GetGraphic(new QRCodeGraphicOptions | ||
| { | ||
| PixelsPerModule = options.PixelsPerModule, | ||
| DarkSKColor = options.DarkSKColor, | ||
| LightSKColor = options.LightSKColor, | ||
| DrawQuietZones = options.DrawQuietZones, | ||
| Icon = options.Icon, | ||
| IconSizePercent = options.IconSizePercent, | ||
| IconBorderWidth = options.IconBorderWidth | ||
| })) | ||
| { | ||
| base64 = SKBitmapToBase64(bmp, imgType); | ||
| } | ||
| return base64; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the graphic representation of the QR code. | ||
| /// </summary> | ||
|
|
@@ -181,6 +163,8 @@ public string GetGraphic(int pixelsPerModule, SKColor darkSKColor, SKColor light | |
| /// <param name="imgType">The img type.</param> | ||
| /// <returns>The string result.</returns> | ||
| [Obsolete("Use GetGraphic(Base64QRCodeGraphicOptions) instead.")] | ||
| [SuppressMessage("SonarAnalyzer.CSharp", "S107", Justification = "Legacy public API overload")] | ||
| [SuppressMessage("SonarAnalyzer.CSharp", "S1133", Justification = "Public API; retained for backward compatibility")] | ||
| public string GetGraphic(int pixelsPerModule, SKColor darkSKColor, SKColor lightSKColor, SKBitmap icon, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true, ImageType imgType = ImageType.Png) | ||
| { | ||
| var base64 = string.Empty; | ||
|
|
@@ -200,7 +184,26 @@ public string GetGraphic(int pixelsPerModule, SKColor darkSKColor, SKColor light | |
| return base64; | ||
| } | ||
|
|
||
| private string SKBitmapToBase64(SKBitmap bmp, ImageType imgType) | ||
| private string RenderGraphic(Base64QRCodeGraphicOptions options, ImageType imgType) | ||
| { | ||
| var base64 = string.Empty; | ||
| using (SKBitmap bmp = qr.GetGraphic(new QRCodeGraphicOptions | ||
| { | ||
| PixelsPerModule = options.PixelsPerModule, | ||
| DarkSKColor = options.DarkSKColor, | ||
| LightSKColor = options.LightSKColor, | ||
| DrawQuietZones = options.DrawQuietZones, | ||
| Icon = options.Icon, | ||
| IconSizePercent = options.IconSizePercent, | ||
| IconBorderWidth = options.IconBorderWidth | ||
| })) | ||
| { | ||
| base64 = SKBitmapToBase64(bmp, imgType); | ||
| } | ||
| return base64; | ||
| } | ||
|
Comment on lines
+187
to
+204
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: RenderGraphic method was only relocated, not modified The Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
| private static string SKBitmapToBase64(SKBitmap bmp, ImageType imgType) | ||
| { | ||
| var base64 = string.Empty; | ||
| SKEncodedImageFormat encodedFormat = imgType switch | ||
|
|
@@ -258,6 +261,7 @@ public static class Base64QRCodeHelper | |
| /// <param name="drawQuietZones">The draw quiet zones.</param> | ||
| /// <param name="imgType">The img type.</param> | ||
| /// <returns>The string result.</returns> | ||
| [SuppressMessage("SonarAnalyzer.CSharp", "S107", Justification = "Convenience helper with many optional parameters")] | ||
| public static string GetQRCode(string plainText, int pixelsPerModule, string darkSKColorHtmlHex, string lightSKColorHtmlHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true, ImageType imgType = ImageType.Png) | ||
| { | ||
| using (var qrGenerator = new QRCodeGenerator()) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| using SkiaSharp; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| using System.Globalization; | ||
| using System.IO; | ||
|
|
@@ -58,6 +59,7 @@ | |
| /// <param name="dpi"></param> | ||
| /// <param name="jpgQuality"></param> | ||
| /// <returns></returns> | ||
| [SuppressMessage("SonarAnalyzer.CSharp", "S1192", Justification = "PDF literal fragments reused in PDF generation")] | ||
| public byte[] GetGraphic(int pixelsPerModule, string darkSKColorHtmlHex, string lightSKColorHtmlHex, int dpi = 150, long jpgQuality = 85) | ||
| { | ||
| byte[] jpgArray = null, pngArray = null; | ||
|
|
@@ -211,11 +213,11 @@ | |
| /// </summary> | ||
| /// <param name="colorString">SKColor in HEX format like #ffffff</param> | ||
| /// <returns></returns> | ||
| private byte[] HexSKColorToByteArray(string colorString) | ||
| private static byte[] HexSKColorToByteArray(string colorString) | ||
| { | ||
| if (colorString.StartsWith("#")) | ||
| if (!string.IsNullOrEmpty(colorString) && colorString[0] == '#') | ||
| colorString = colorString.Substring(1); | ||
|
Comment on lines
+218
to
219
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: HexSKColorToByteArray condition refactor is behaviorally equivalent The condition was changed from Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| byte[] byteSKColor = new byte[colorString.Length / 2]; | ||
|
Check warning on line 220 in QRCoder.Core/Renderers/PdfByteQRCode.cs
|
||
| for (int i = 0; i < byteSKColor.Length; i++) | ||
| byteSKColor[i] = byte.Parse(colorString.Substring(i * 2, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture); | ||
| return byteSKColor; | ||
|
|
@@ -231,6 +233,7 @@ | |
| /// <summary> | ||
| /// Generates a QR code from the given data and returns the rendered output. | ||
| /// </summary> | ||
| [SuppressMessage("SonarAnalyzer.CSharp", "S107", Justification = "Convenience helper with many optional parameters")] | ||
| public static byte[] GetQRCode(string plainText, int pixelsPerModule, string darkSKColorHtmlHex, | ||
| string lightSKColorHtmlHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, | ||
| EciMode eciMode = EciMode.Default, int requestedVersion = -1) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: Making
qrreadonly cements a pre-existing inconsistency in SetQRCodeDataThe
Base64QRCode.SetQRCodeDataoverride atQRCoder.Core/Renderers/Base64QRCode.cs:90-93delegates tothis.qr.SetQRCodeData(data)but never callsbase.SetQRCodeData(data). This means the parent class'sQrCodeDataproperty is never updated when using this method. This is a pre-existing issue (not introduced by this PR), but addingreadonlyto theqrfield makes it slightly harder to refactor in the future — e.g., you can't reassignqrto a newQRCode(data)instance. In practice this doesn't matter becauseBase64QRCodeonly usesqr(notQrCodeDatadirectly) for rendering, but it's worth being aware of the asymmetry if the class is ever extended.Was this helpful? React with 👍 or 👎 to provide feedback.