Skip to content

Add CodeFixer for NoProtectedFieldsAnalyzer (PH2070) - #947

Draft
Brian Collamore (bcollamore) with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-946
Draft

Brian Collamore (bcollamore) with Copilot wants to merge 3 commits into
mainfrom
copilot/fix-946

Conversation

Copilot AI commented Aug 25, 2025

Copy link
Copy Markdown
Contributor

This PR implements an automatic code fix for the NoProtectedFieldsAnalyzer (PH2070) that converts protected fields to protected properties with private setters, improving code encapsulation.

What's Changed

New CodeFixProvider

  • NoProtectedFieldsCodeFixProvider: Automatically converts protected fields to properties
  • MEF Integration: Properly exported for IDE integration with [ExportCodeFixProvider] and [Shared] attributes
  • Batch Support: Uses WellKnownFixAllProviders.BatchFixer for fixing multiple violations at once

Smart Field-to-Property Conversion

The code fix transforms protected fields following C# naming conventions:

// Before
class Example 
{
    protected string _name;
    protected int _counter;
    protected List<string> _items;
}

// After (automatic fix)
class Example 
{
    protected string Name { get; private set; }
    protected int Counter { get; private set; }
    protected List<string> Items { get; private set; }
}

Key Features

  • Naming Convention Handling: Removes underscore prefix and capitalizes first letter
  • Multiple Field Support: Handles comma-separated field declarations like protected string _a, _b;
  • Type Preservation: Works with simple types, generics, and complex type expressions
  • Encapsulation: Creates properties with get; private set; to maintain proper encapsulation

Enhanced Test Coverage

  • Updated NoProtectedFieldsAnalyzerTest to extend CodeFixVerifier
  • Added 6 new test methods covering various scenarios:
    • Single field conversion
    • Multiple field conversion
    • Fields without underscore prefix
    • Type preservation
    • Complex generic types
    • Fix provider validation

Documentation Update

  • Updated Documentation/Diagnostics/PH2070.md to reflect CodeFix availability (changed "No" to "Yes")

Benefits for Developers

  • One-click fix: Developers can automatically resolve PH2070 violations with a single action
  • Consistency: Ensures uniform application of encapsulation patterns
  • Time savings: Eliminates manual refactoring for field-to-property conversions
  • IDE Integration: Works seamlessly in Visual Studio, VS Code, and other IDEs

Testing

  • ✅ All 10 NoProtectedFieldsAnalyzer tests passing
  • ✅ Full test suite: 2006/2006 tests passing
  • ✅ Zero formatting violations
  • ✅ Zero build warnings or errors

The implementation follows the established patterns in the codebase and maintains backward compatibility while adding the requested automatic fix functionality.

Fixes #946.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits August 25, 2025 21:43
Co-authored-by: bcollamore <57269455+bcollamore@users.noreply.github.com>
Co-authored-by: bcollamore <57269455+bcollamore@users.noreply.github.com>
Copilot AI changed the title [WIP] Create CodeFixer for NoProtectedFieldsAnalyzer Add CodeFixer for NoProtectedFieldsAnalyzer (PH2070) Aug 25, 2025
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create CodeFixer for NoProtectedFieldsAnalyzer

2 participants