Skip to content

Remove duplicated reflection helpers in ReflectHelper - #9502

Merged
Amaury Levé (Evangelink) merged 1 commit into
mainfrom
dev/amauryleve/ubiquitous-guide
Jun 29, 2026
Merged

Amaury Levé (Evangelink) merged 1 commit into
mainfrom
dev/amauryleve/ubiquitous-guide

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Summary

Addresses part of #9499 (Code Duplication Analysis).

ReflectHelper.GetTestCategories and ReflectHelper.GetTestPropertiesAsTraits (static methods) duplicated the algorithm bodies of the IReflectionOperations extension methods of the same name in ReflectionHelper. The static ReflectHelper.GetCustomAttributesCached helper already delegates to PlatformServiceProvider.Instance.ReflectionOperations, so the two static methods are now thin delegates to the shared extension methods — removing ~70 lines of true duplication with zero behavior change.

The ReflectionHelper extension methods are the "live" implementations (used by TypeEnumerator); ReflectHelper.GetTestCategories (static) had no production callers and ReflectHelper.GetTestPropertiesAsTraits (static) is only referenced from a unit test, which keeps working via the delegate.

Scope notes

The dedup-analysis issue lists 21 candidates. The remaining items were intentionally left as-is:

  • Jsonite folder (4 high-priority items): vendored third-party BSD-licensed code (Alexandre Mutel, 2016) — must not be refactored.
  • ReflectHelper vs ReflectionOperations: intentional architectural split (singleton facade vs service); merging would break the ReflectHelper.Instance surface.
  • SynchronizationContextPreservingAsyncTaskMethodBuilder: generic/non-generic struct pair required by the compile-time async-builder pattern; cannot be unified.
  • Assert-condition analyzers / other analyzers: every analyzer in the project repeats the Initialize boilerplate by convention; extracting a base for just two would be locally inconsistent.
  • Report engines: the duplication is constructor forwarding across two assemblies, which can't be DRYed via inheritance.

Validation

  • MSTestAdapter.PlatformServices builds clean (0 warnings, 0 errors).
  • ReflectionOperationsTests (47) and TestPropertyAttributeTests (3) pass.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

ReflectHelper.GetTestCategories and ReflectHelper.GetTestPropertiesAsTraits duplicated the algorithm bodies of the IReflectionOperations extension methods in ReflectionHelper. Since the static GetCustomAttributesCached helper already delegates to PlatformServiceProvider.Instance.ReflectionOperations, the static methods are now thin delegates to the shared extension methods, removing ~70 lines of duplication (issue #9499).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 29, 2026 12:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes duplicated reflection metadata extraction logic in ReflectHelper by delegating to the existing “live” implementations in ReflectionHelper (extension methods on IReflectionOperations). This keeps behavior centralized in the same place that TypeEnumerator uses, while preserving the ReflectHelper surface.

Changes:

  • Replaced ReflectHelper.GetTestCategories body with a thin delegate to IReflectionOperations.GetTestCategories(...) (extension method).
  • Replaced ReflectHelper.GetTestPropertiesAsTraits body with a thin delegate to IReflectionOperations.GetTestPropertiesAsTraits(...) (extension method).
  • Added the required using to bring the extension methods into scope.
Show a summary per file
File Description
src/Adapter/MSTestAdapter.PlatformServices/Helpers/ReflectHelper.cs Removes duplicated implementations by delegating to ReflectionHelper extension methods on IReflectionOperations.

Review details

  • Files reviewed: 1/1 changed files
  • Comments generated: 0
  • Review effort level: Low

@Evangelink Amaury Levé (Evangelink) left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

🤖 Automated review by GitHub Copilot. Posted via a maintainer's GitHub token, so it appears under their account — the account owner did not write or approve this content personally. Generated by the Expert Code Review workflow. To request a follow-up action, reply by tagging @copilot directly.

✅ 22/22 dimensions clean — no findings.


Behavioral equivalence — verified ✔

GetTestCategories: The old body called ReflectHelper.GetCustomAttributesCached(method) / (owningType) / (owningType.Assembly), which is a static shim that already delegated to PlatformServiceProvider.Instance.ReflectionOperations.GetCustomAttributesCached(...). The new one-liner delegates directly to PlatformServiceProvider.Instance.ReflectionOperations.GetTestCategories(categoryAttributeProvider, owningType), which is the extension method in ReflectionHelper — that method calls reflectionOperations.GetCustomAttributesCached(...) on the same instance, hitting the same ConcurrentDictionary cache. Attribute order (method → type → assembly) and the lazy-List<string> / spread-to-array return pattern are identical.

GetTestPropertiesAsTraits: The old body used GetCustomAttributesCached(testPropertyProvider) for the method and testPropertyProvider.ReflectedType is { } testClass ? GetCustomAttributesCached(testClass) : [] for the class. The new one-liner delegates to ReflectionHelper.GetTestPropertiesAsTraits, which uses the identical null-guarded pattern via reflectionOperations.GetCustomAttributesCached. The two-pass count-then-fill algorithm and the empty-array fast path are preserved verbatim.

The PR's zero-behavior-change claim is correct.

@Evangelink Amaury Levé (Evangelink) added the state/needs-review Awaiting review from the team. label Jun 29, 2026
@Evangelink
Amaury Levé (Evangelink) enabled auto-merge (squash) June 29, 2026 12:55
@Evangelink
Amaury Levé (Evangelink) merged commit 2940b5e into main Jun 29, 2026
41 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the dev/amauryleve/ubiquitous-guide branch June 29, 2026 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state/needs-review Awaiting review from the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants