# rostilos/sonarqube-magento2-rules architecture

> Architecture diagram of the GitHub repository rostilos/sonarqube-magento2-rules, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rostilos/sonarqube-magento2-rules
- Repository: https://github.com/rostilos/sonarqube-magento2-rules

## Overview

##### 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).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Components
    A["Magento 2 Code Base (External)"]:::external
    B["SonarQube Server"]:::external
    C["Postgres Database"]:::external

    %% Build Process
    D["Maven Build (pom.xml)"]:::build
    click D "https://github.com/rostilos/sonarqube-magento2-rules/blob/main/pom.xml"

    %% Plugin Artifact
    E["sonarqube-magento2-rules Plugin"]:::plugin

    %% Plugin Internal Components
    subgraph "Plugin Structure"
        F["Core Plugin Classes"]:::module
        click F "https://github.com/rostilos/sonarqube-magento2-rules/tree/main/src/main/java/org/perspectiveteam/sonarrules/php"

        G["PHP Rule Checks"]:::module
        click G "https://github.com/rostilos/sonarqube-magento2-rules/tree/main/src/main/java/org/perspectiveteam/sonarrules/php/checks"

        H["Utility Classes"]:::module
        click H "https://github.com/rostilos/sonarqube-magento2-rules/blob/main/src/main/java/org/perspectiveteam/sonarrules/php/utils/CheckUtils.java"

        I["Localization Resources"]:::module
        click I "https://github.com/rostilos/sonarqube-magento2-rules/tree/main/src/main/resources/org/sonar/l10n/php/rules/magento2"
    end

    %% Test Modules
    J["Test Modules"]:::test
    click J "https://github.com/rostilos/sonarqube-magento2-rules/tree/main/src/test/java/org/perspectiveteam/sonarrules/php"

    %% Relationships and Data Flow
    %% SonarQube Ecosystem
    B -->|"depends_on"| C
    B -->|"loads"| E
    A -->|"analyzed_by"| G
    G -->|"reports_violations_to"| B

    %% Build to Plugin Artifact
    D -->|"builds"| E

    %% Plugin Artifact Composition
    E -->|"contains"| F
    E -->|"contains"| G
    E -->|"contains"| H
    E -->|"contains"| I

    %% Testing interaction
    J -->|"validates"| G

    %% Styles
    classDef external fill:#FFE6CC,stroke:#D2691E,stroke-width:2px;
    classDef build fill:#FFF2CC,stroke:#B8860B,stroke-width:2px;
    classDef plugin fill:#CCE5FF,stroke:#1E90FF,stroke-width:2px;
    classDef module fill:#E6FFCC,stroke:#32CD32,stroke-width:2px;
    classDef test fill:#FFD1DC,stroke:#FF1493,stroke-width:2px;
```
