Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/pmdPublic

[apex] New rule: Repeated primitives#5819

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
mitchspano wants to merge8 commits intopmd:main
base:main
Choose a base branch
Loading
frommitchspano:repeated_primitives

Conversation

mitchspano
Copy link
Contributor

@mitchspanomitchspano commentedJun 9, 2025
edited
Loading

Describe the PR

This PR introduces a new PMD ruleRepeatedPrimitiveValueRule for Apex that detects repeated primitive values (strings and numbers) within a class that could be extracted into named constants to improve code maintainability and reduce magic numbers/strings.

The rule identifies primitive literals that appear multiple times in the same class and flags them as potential candidates for constant extraction. It provides intelligent handling for common scenarios:

  • Excludes acceptable repeated values: Common values like0,1,-1, and empty strings
  • Ignores boolean literals:true andfalse values are acceptable to repeat
  • Special Date/DateTime handling: Numbers used inDate.newInstance() andDateTime.newInstance() calls are not flagged
  • Constant detection: Already definedstatic final constants are not flagged again
  • Null safety: Null values are ignored

Example violations:

publicclassExample {publicvoidmethod1() {Integertimeout=30;// violation: 30 appears multiple timesStringmessage='Hello World';// violation if repeated    }publicvoidmethod2() {Integerdelay=30;// violation: same value repeatedStringgreeting='Hello World';// violation: same string repeated    }}

The rule helps developers identify opportunities to extract magic numbers and strings into well-named constants, improving code readability and maintainability.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

mitchspanoand others added8 commitsJanuary 12, 2025 21:02
This  introduces a new Apex PMD rule, `RepeatedPrimitiveValue`, which identifies repeated literal primitive values (integers, decimals, strings, etc.) within an Apex class that are not defined as constants.The rule has been added to the `design.xml` rule set and includes comprehensive unit tests.
…s in Apex classes Added detailed documentation and examples for better understanding. Updated logic to exclude boolean literals and added test cases for various scenarios including blobs and objects.
Changed the version from 7.10.0 to 7.15.0 and updated the priority from 2 to 5 for the RepeatedPrimitiveValueRule to reflect its importance in code quality enforcement.
@github-actionsGitHub Actions
Copy link

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 723 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 34 errors and 8 configuration errors.

Regression Tester Report

(comment created at 2025-06-09 17:07:58+00:00 for0057c1b)

@mitchspanomitchspano marked this pull request as draftJune 9, 2025 17:22
@adangeladangel changed the titleRepeated primitives[apex] Repeated primitivesJun 9, 2025
@adangeladangel changed the title[apex] Repeated primitives[apex] New rule: Repeated primitivesJun 9, 2025
@adangeladangel added the a:new-ruleProposal to add a new built-in rule labelJun 9, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
a:new-ruleProposal to add a new built-in rule
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

[apex] New Rule: Repeated Primitive Value
2 participants
@mitchspano@adangel

[8]ページ先頭

©2009-2025 Movatter.jp