How rostilos/sonarqube-magento2-rules works
Main Components
• Magento 2 Code Base – Although not a part of the plugin code itself, the rules are designed to analyze Magento 2 PHP projects. Including an “Analyzed Code” component might be useful to show how rules are applied.
• Plugin Artifact – The core of this project is the plugin jar that is built via Maven (pom.xml exists) and contains all the rules implementations.
• Rule Implementations – Within the plugin jar, the architecture is organized into packages:
• Base Packages: “org/perspectiveteam/sonarrules” and its subpackages.
• Utility and Localization Resources:
• Utility Classes: e.g., CheckUtils.java in “php/utils”
• Localization / rule metadata: located in “src/main/resources/org/sonar/l10n/php/rules/magento2” which hold HTML descriptions for each rule.
Relationships and Interactions
• The SonarQube server dynamically loads the plugin once deployed. At startup, the plugin registers its custom PHP rules with the SonarQube engine, exposing them for later use during code analysis.
• During a code scan, SonarQube’s scanning engine passes the Magento 2 PHP source code to the plugin, which then applies its rule implementations.
• Each rule implementation (sensor or check) inspects code elements and reports violations back to SonarQube. The localization resources provide human-readable descriptions and messages for each rule violation.
• The testing framework interacts with the rule implementations to verify their correctness (unit tests) and simulate scanning scenarios (using sample PHP files under “src/test/resources”).
Important Architectural Patterns / Design Principles
• Plugin Registration: The project utilizes extension and sensor mechanisms typical for SonarQube plugins.
• Modularity: Rules are separated into individual components for clarity, with each rule class encapsulated in its respective file, facilitating maintainability and extensibility.
• Localization and Documentation: The use of resource files for rule metadata ensures that the rules are well documented in multiple languages or formats (in this case HTML descriptions).
• Maven Build Structure: The use of Maven for dependency management and build lifecycle indicates a standard Java project layout.
Final Recommendations
• Be as detailed as possible when mapping out each component, ensuring no major aspect is omitted, particularly:
• The Maven build process (i.e., developing and packaging via pom.xml).
• The directory grouping between rule implementations and resources.
• The integration/deployment aspects (copying jar to SonarQube’s extensions/plugins directory).