Skip to content

Refactor TagImporter to avoid Optional parameters (Sonar S3553) #545

@redcatbear

Description

@redcatbear

Situation

Sonar complains about the use of Optional as a parameter in TagImporter.java (rule S3553: "Optional" should not be used for parameters).

Affected code in TagImporter.java:

static TagImporter create(final Optional<PathConfig> config, final InputFile file,
        final ImportEventListener listener)
{
    final LineConsumer lineConsumer = createLineConsumer(config, file, listener);
    return new TagImporter(lineConsumer, file);
}

private static LineConsumer createLineConsumer(final Optional<PathConfig> config,
        final InputFile file, final ImportEventListener listener)
{
    // ...
}

Using Optional as a parameter is considered a code smell because it forces callers to wrap arguments in an Optional even if they already have the object, and it doesn't provide the safety benefits that Optional returns provide.

Implementation Hints

  1. Modify TagImporter.create and TagImporter.createLineConsumer to accept PathConfig directly instead of Optional<PathConfig>.
  2. The PathConfig parameter should be nullable. Update the logic to check for null instead of config.isPresent().
  3. Alternatively, provide overloaded versions of these methods: one with PathConfig and one without.
  4. Update TagImporterFactory.createImporter to pass the value from the Optional or null:
    final PathConfig config = findConfig(path).orElse(null);
    return TagImporter.create(config, path, listener);
  5. Check for other usages of these methods in tests and update them accordingly.

Acceptance Criteria

  1. TagImporter methods no longer use Optional as a parameter.
  2. Sonar S3553 warning for this location is resolved.
  3. mvn verify passes, ensuring that the tag importer still works correctly with and without configuration.

Metadata

Metadata

Assignees

Labels

refactoringCode improvement without behavior change
No fields configured for Refactoring.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions