Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1.5k
[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:mainChoose a base branch frommitchspano:repeated_primitives
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Draft
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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.
Compared to main: (comment created at 2025-06-09 17:07:58+00:00 for0057c1b) |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading.Please reload this page.
Describe the PR
This PR introduces a new PMD rule
RepeatedPrimitiveValueRule
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:
0
,1
,-1
, and empty stringstrue
andfalse
values are acceptable to repeatDate.newInstance()
andDateTime.newInstance()
calls are not flaggedstatic final
constants are not flagged againExample violations:
The rule helps developers identify opportunities to extract magic numbers and strings into well-named constants, improving code readability and maintainability.
Related issues
Ready?
./mvnw clean verify
passes (checked automatically by github actions)