Skip to content

Refactor duplicated formatter initialization (Clone C7) #2012

Backlog

Description

@nusaibaalam

PMD CPD identified a three-way Type 2 code clone involving:

  • httpie/output/formatters/headers.py — HeadersFormatter
  • httpie/output/formatters/json.py — JSONFormatter
  • httpie/output/formatters/xml.py — XMLFormatter

The three classes contain duplicated constructors that differ only in the format options key used to determine whether formatting is enabled.

Proposed solution

Apply the Pull Up Method technique with a Template Method element.

Move the common initialization logic into FormatterPlugin in httpie/plugins/base.py.

Introduce an optional class attribute:

format_options_key = None

Configure the subclasses with their respective keys:

  • HeadersFormatter: ('headers', 'sort')
  • JSONFormatter: ('json', 'format')
  • XMLFormatter: ('xml', 'format')

Remove the duplicated subclass constructors while preserving the existing behavior of ColorFormatter.

Validation criteria

  1. Run the relevant regression tests:
    python -m pytest tests/test_output.py tests/test_httpie.py tests/test_json.py tests/test_xml.py
  2. Run PMD CPD with minimum-token thresholds of 25, 50, and 100.
  3. Confirm that the C7 three-way clone is eliminated.
  4. Confirm that existing formatter behavior remains unchanged.

Measured results

  • Regression tests: 508 passed, 2 skipped, 3 xfailed.
  • CPD at 25 tokens: 12 groups before, 11 after.
  • CPD at 50 tokens: 1 group before and after.
  • CPD at 100 tokens: 0 groups before and after.

Implementation

The refactoring has been committed locally as 448d462 on branch cs3-c7-refactor.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions