# Inspekcje IntelliJ

Dokument opisuje zestaw inspekcji IntelliJ dla projektu Apollo.

## Cel

Zapewnienie spójnych zasad jakości kodu w całym zespole. Konfiguracja stanowi również podstawę do późniejszej automatycznej analizy statycznej kodu (SAST) w TeamCity.

---

## Profil inspekcji

Profil nazywa się **Apollo** i jest przechowywany w:

```
.idea/inspectionProfiles/Apollo.xml
```

Plik `profiles_settings.xml` w tym samym katalogu wskazuje profil `Apollo` jako domyślny dla projektu. Dzięki temu po pobraniu repozytorium IntelliJ automatycznie go aktywuje.

---

## Włączone inspekcje

Profil jest dostosowany do Grails/Groovy + JavaScript + SQL.

### Złożoność kodu (Groovy)

| Inspekcja | Poziom | Limit | Opis |
| --- | --- | --- | --- |
| `AnonymousClassComplexity` | WARNING | 3 | Złożoność klas anonimowych |
| `ClassComplexity` | WARNING | 80 | Złożoność całej klasy |
| `CyclomaticComplexity` | WARNING | 10 | Złożoność cyklomatyczna metody |
| `GroovyOverlyComplexMethod` | WARNING | 10 | Zbyt złożona metoda |
| `GroovyOverlyLongMethod` | WARNING | 30 | Zbyt długa metoda (liczba instrukcji) |
| `GroovyOverlyNestedMethod` | WARNING | 5 | Zbyt głębokie zagnieżdżenie w metodzie |
| `GroovyMethodParameterCount` | WARNING | 5 | Zbyt wiele parametrów metody |

### Jakość kodu

| Inspekcja | Poziom | Opis |
| --- | --- | --- |
| `GroovyPointlessArithmetic` | WARNING | Bezcelowe operacje arytmetyczne (np. `x + 0`) |
| `GroovyRangeTypeCheck` | WARNING | Nieprawidłowe typy w zakresach |
| `GroovySwitchStatementWithNoDefault` | WARNING | Brak klauzuli `default` w `switch` |

### JavaScript

| Inspekcja | Poziom | Limit | Opis |
| --- | --- | --- | --- |
| `CyclomaticComplexityJS` | WARNING | 10 | Złożoność cyklomatyczna funkcji JS |
| `NestingDepthJS` | WARNING | 5 | Głębokość zagnieżdżenia w JS |
| `ParametersPerFunctionJS` | WARNING | 5 | Zbyt wiele parametrów funkcji JS |
| `StatementsPerFunctionJS` | WARNING | 30 | Zbyt wiele instrukcji w funkcji JS |
| `FunctionWithMultipleLoopsJS` | WARNING | — | Wiele pętli w jednej funkcji JS |
| `JSDuplicatedDeclaration` | ERROR | — | Zduplikowane deklaracje zmiennych/funkcji JS |
| `Eslint` | WARNING | — | Reguły ESLint (konfiguracja z `.eslintrc`) |

### SQL

| Inspekcja | Poziom | Opis |
| --- | --- | --- |
| `SqlGotoInspection` | WARNING | Użycie `GOTO` w SQL |
| `SqlMissingColumnAliasesInspection` | WARNING | Brak aliasów kolumn w zapytaniach SQL |
| `SqlNamedArgumentsInspection` | WARNING | Niepoprawne nazwane argumenty w SQL |

---

## Przykłady kodu

### `AnonymousClassComplexity` — złożoność klas anonimowych (limit: 3)

```
def comparator = new Comparator<Transport>() {
    int compare(Transport a, Transport b) {
        if (a.status == 'NEW') return -1
        if (b.status == 'NEW') return 1
        if (a.priority > b.priority) return -1
        if (b.priority > a.priority) return 1
        return a.createdAt <=> b.createdAt
    }
}
```

### `ClassComplexity` - złożoność klasy (limit: 80)

Zbyt wiele rozgałęzień (if/else, switch, pętli, catch) zsumowanych po wszystkich metodach klasy

### `CyclomaticComplexity` / `GroovyOverlyComplexMethod` - złożoność cyklomatyczna metody (limit: 10)

```
def processTransport(Transport t) {
    if (!t) return                           // +1
    if (t.status == 'NEW') {                 // +1
        if (t.priority > 5) {               // +1
            if (t.hasDocuments()) {         // +1
                for (doc in t.documents) {  // +1
                    if (doc.valid) {        // +1
                        save(doc)
                    } else if (doc.expired) { // +1
                        archive(doc)
                    }
                }
            }
        }
    } else if (t.status == 'IN_PROGRESS') { // +1
        ...
    }
}
```

### `GroovyOverlyLongMethod` - zbyt długa metoda (limit: 30 instrukcji)

```
def generateReport(params) {
    def transport = transportService.find(params.id)
    def driver = transport.driver
    def vehicle = transport.vehicle
    // ... 35 kolejnych linii logiki
}
```

### `GroovyOverlyNestedMethod` - zbyt głębokie zagnieżdżenie (limit: 5)

```
def process(orders) {
    for (order in orders) {                    // poziom 1
        if (order.active) {                    // poziom 2
            for (item in order.items) {        // poziom 3
                if (item.valid) {              // poziom 4
                    for (doc in item.docs) {   // poziom 5
                        if (doc.signed) {      // poziom 6
                            save(doc)
                        }
                    }
                }
            }
        }
    }
}
```

### `GroovyMethodParameterCount` - zbyt wiele parametrów (limit: 5)

```
def createTransport(String origin, String destination, Date date,
                    String driverName, String vehiclePlate, Integer priority) { ... }
```

### `GroovyPointlessArithmetic` - bezcelowe operacje arytmetyczne

```
def total = subtotal + 0
def value = amount * 1
def result = price - 0
```

### `GroovyRangeTypeCheck` - nieprawidłowe typy w zakresach

```
def range = "a".."z"
if (someInt in "1".."9") { ... }  // powinno być 1..9
```

### `GroovySwitchStatementWithNoDefault` - brak `default` w `switch`

```
switch (transport.status) {
    case 'NEW': handleNew(); break
    case 'DONE': handleDone(); break
}
```

### `CyclomaticComplexityJS` / `NestingDepthJS` - złożoność i zagnieżdżenie w JS

```
// PROBLEM — zagnieżdżenie 6 poziomów, złożoność > 10
function processData(data) {
    if (data) {                              // poziom 1
        data.items.forEach(item => {         // poziom 2
            if (item.active) {               // poziom 3
                item.documents.forEach(doc => { // poziom 4
                    if (doc.valid) {         // poziom 5
                        if (doc.signed) {   // poziom 6
                            save(doc)
                        }
                    }
                })
            }
        })
    }
}
```

### `ParametersPerFunctionJS` - zbyt wiele parametrów funkcji JS (limit: 5)

```
function createOrder(id, name, date, status, priority, driverId) { ... }
```

### `StatementsPerFunctionJS` - zbyt wiele instrukcji w funkcji JS (limit: 30)

Analogicznie do `GroovyOverlyLongMethod`

### `FunctionWithMultipleLoopsJS` - wiele pętli w jednej funkcji JS

```
function process(items) {
    const valid = []
    for (const item of items) {     // pętla 1
        if (item.valid) valid.push(item)
    }
    const result = []
    for (const item of valid) {     // pętla 2
        result.push(transform(item))
    }
    return result
}
```

### `JSDuplicatedDeclaration` - zduplikowane deklaracje JS

```
const status = 'NEW'
// ...
const status = 'DONE'
```

### `SqlMissingColumnAliasesInspection` — brak aliasów kolumn w SQL

```
SELECT t.id, d.id, t.status
FROM transport t
JOIN driver d ON d.id = t.driver_id
```
