Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create rule S7134: Dependency constraints should not be violated #4458

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions rules/S7134/java/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"title": "Architectural constraints should not be violated",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "0min"
},
"tags": [
"architecture",
"design"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7134",
"sqKey": "S7134",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH"
},
"attribute": "MODULAR"
}
}
62 changes: 62 additions & 0 deletions rules/S7134/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
This rule reports when a class, package or other source code component violates the architectural constraints defined for a project.

== Why is this an issue?

There is a reference from one source code component to another that is prohibited by the architectural constraints defined for the project.
These definitions are made by the maintainers of the project in the architecture configuration file.

=== What is the potential impact?

Over time, codebases often drift from the intended project architecture,
gradually degrading the code structure and misaligning it with the original design.

This misalignment reduces visibility and control over daily decisions affecting the architecture.
As these small decisions accumulate, the codebase becomes harder to understand, and the architecture grows increasingly complex and unstructured.

== How to fix it

Refactor your source code to adhere to the architectural constraints.
The specific approach will depend on your project architecture and the code violating the constraints.
For example, you might replace a function call with an alternative function call from another software layer
or create a new function in that layer to maintain a clean architecture.

=== Code examples

Assuming that for source code components in directory `./src/main/com/example/panels`, access to
components in directory `./src/main/com/example/repos` is constrained:

==== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
package com.example.panels;

import com.example.repos.CustomerRepo;

class ShowCustomersPanel extends Panel {

CustomerRepo customerRepo = ...;

List<Customer> customers = customerRepo.findAll();
}
----

==== Compliant solution

[source,java,diff-id=1,diff-type=compliant]
----
package com.example.panels;

import com.example.services.CustomerService;

class ShowCustomersPanel extends Panel {

CustomerService customerService = ...;

List<Customer> customers = customerService.getAllCustomers();
}
----

=== Documentation

- Defining architectural constraints for SonarQube
2 changes: 2 additions & 0 deletions rules/S7134/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading