Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 24, 2025

This PR implements a new refactoring tool that converts method calls from passing individual arguments to passing a parameter object (struct/record/class), addressing a common refactoring pattern needed for improved code maintainability.

Features

New Command: parameters-to-object

Converts methods like this:

void MyFunction(bool arg1, string arg2, CustomObject arg3) {
    if (arg1) {
        Console.WriteLine(arg2);
        return arg3.Value;
    }
}

MyFunction(true, "Hello World!", new CustomObject());

Into this:

void MyFunction(MyFunctionArgs args) {
    if (args.arg1) {
        Console.WriteLine(args.arg2);
        return args.arg3.Value;
    }
}

public class MyFunctionArgs {
    public bool arg1 { get; set; }
    public string arg2 { get; set; }
    public CustomObject arg3 { get; set; }
    
    public MyFunctionArgs(bool arg1, string arg2, CustomObject arg3) {
        this.arg1 = arg1;
        this.arg2 = arg2;
        this.arg3 = arg3;
    }
}

Usage

# Convert to class (default)
cast parameters-to-object MyFile.cs --line 10 --parameter-object-name "MyParams"

# Convert to struct  
cast parameters-to-object MyFile.cs --line 10 --parameter-object-type struct

# Convert to record (modern C# syntax)
cast parameters-to-object MyFile.cs --line 10 --parameter-object-type record

# Preview changes
cast parameters-to-object MyFile.cs --line 10 --dry-run

Implementation Details

  • Method Signature Transformation: Updates method to accept single parameter object
  • Method Body Updates: Replaces parameter references with args.parameterName syntax
  • Parameter Object Generation: Creates class/struct/record with properties and constructor
  • Type Support: Handles complex types including generics and custom objects
  • Validation: Blocks unsupported parameter types (ref/out/in/params)

Testing

Added comprehensive test suite with 5 test cases covering:

  • Basic parameter conversion for all object types
  • Complex parameter types (generics, collections)
  • Error handling for methods without parameters
  • Real-world business logic scenarios

All existing tests (78) continue to pass.

Current Limitations

  • Call site updates require manual intervention (noted for future enhancement)
  • Some edge cases in complex parameter reference replacement may need manual review

Fixes #19.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

…hensive tests

Co-authored-by: AdamFrisby <114041+AdamFrisby@users.noreply.github.com>
Copilot AI changed the title [WIP] Parameters to Parameter Object Add Parameters to Parameter Object refactoring command Jul 24, 2025
Copilot AI requested a review from AdamFrisby July 24, 2025 19:13
Copilot finished work on behalf of AdamFrisby July 24, 2025 19:13
@AdamFrisby
Copy link
Owner

@copilot we need the update callers functionality as well.

…actoring

Co-authored-by: AdamFrisby <114041+AdamFrisby@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 24, 2025

@copilot we need the update callers functionality as well.

I've implemented the update callers functionality. The refactoring command now automatically finds and updates all call sites to use the new parameter object.

Key features added:

  • Automatic call site conversion: MyFunction(arg1, arg2)MyFunction(new MyFunctionArgs(arg1, arg2))
  • --update-callers option (defaults to true) to control this behavior
  • Support for all parameter object types (class/struct/record)
  • Comprehensive test coverage including multiple call sites

The implementation includes proper constructor assignments using this.property = parameter syntax and maintains backward compatibility for users who prefer manual call site updates.

Commit: b4ead92

Copilot finished work on behalf of AdamFrisby July 24, 2025 19:51
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.

Parameters to Parameter Object

2 participants