Skip to content

Commit

Permalink
Merge branch 'main' into feature/ISSUE-208--better-build
Browse files Browse the repository at this point in the history
  • Loading branch information
dedece35 authored Aug 8, 2023
2 parents a7695a0 + 90667c4 commit f575288
Show file tree
Hide file tree
Showing 9 changed files with 999 additions and 53 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [#121](https://github.com/green-code-initiative/ecoCode/issues/121) new PHP rule : Multiple if-else statement + refactoring implementation

### Changed

### Deleted
Expand Down
86 changes: 43 additions & 43 deletions RULES.md

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions TODOs_DDC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
TODOS dev DDC
===

actions vues en séance weekly-meeting :

- ecoCode-android : nettoyer packages (suite au release)
- teams : nettoyer anciennes équipes

actions vues perso :

- nettoyer le MIGRATION_TODOs.md
- ménage dans les branches de dev (local et remote)
- JYC : suppression dépendance analyser-commons ==> check si bien tout nettoyé (version dans pom, référence dans code)
- créer issue sur la rule EC2 (`avoidMultipleIfElse`) sur JAVA :
- à refondre avec les uses cases du PHP + implem du PHP (en cours - PR_160_recup)
- JAVA : existe depuis longtemps !!! normal que l'implem PHP et python aient le même code minimaliste fonctionellement
- voir les rules désativées chez PJ, créer des issues et corriger (`avoidMultipleIfElse`) :
- voir pourquoi désactivées car ralaient trop
- retester le EC2
54 changes: 54 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC2/php/EC2.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
If we are using too many conditional IF, ELSEIF or ELSE statements it will impact performance.
We can think of using a switch statement instead of multiple if-else if possible, or refactor code
to reduce number of IF, ELSEIF and ELSE statements. Sometimes called "complexity cyclomatic".
Switch statement has a performance advantage over if – else.

## Functional rules
- one variable must be used maximum twice in IF / ELSEIF / ELSE statements at the same level - WARNINGs :
- IF and ELSEIF statements use explicitly variable names !
- ELSE statements use implicity variable names !
- one variable must be used maximum twice in IF / ELSEIF / ELSE statements at differents hierarchical levels
- we can assume that if one variable is used three times or more, we should :
- use a SWITCH statement instead
- or refactor the code if possible

## Non-compliant Code Example

NON compliant, because `$nb` is used 4 times :
- 2 explicit times in IF statements
- 2 implicit times in ELSE statements

```php
$index = 1;
$nb = 2;
...
if ($nb == 0) {
$nb = $index;
} elseif ($nb == 1) {
$nb = $index * 2;
} elseif ($nb == 2) {
$nb = $index * 3;
} else {
$nb = -1;
}
```

## Compliant Code Example

SWITCH statement solution + refactor solution

```php
$index = 1;
$nb = 2;
...
switch ($nb) {
case 0:
case 1:
case 2:
$nb = $index * ($nb + 1);
break;
default:
$nb = -1;
}
return $nb;
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@
import java.util.List;

import fr.greencodeinitiative.php.checks.AvoidGettingSizeCollectionInLoopCheck;
import fr.greencodeinitiative.php.checks.AvoidDoubleQuoteCheck;
import fr.greencodeinitiative.php.checks.AvoidFullSQLRequestCheck;
import fr.greencodeinitiative.php.checks.AvoidSQLRequestInLoopCheck;
import fr.greencodeinitiative.php.checks.AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements;
import fr.greencodeinitiative.php.checks.AvoidUsingGlobalVariablesCheck;
import fr.greencodeinitiative.php.checks.IncrementCheck;
import fr.greencodeinitiative.php.checks.NoFunctionCallWhenDeclaringForLoop;
import fr.greencodeinitiative.php.checks.UseOfMethodsForBasicOperations;
import fr.greencodeinitiative.php.checks.*;
import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.plugins.php.api.visitors.PHPCustomRuleRepository;
Expand Down Expand Up @@ -69,7 +62,8 @@ public List<Class<?>> checkClasses() {
AvoidUsingGlobalVariablesCheck.class,
IncrementCheck.class,
NoFunctionCallWhenDeclaringForLoop.class,
UseOfMethodsForBasicOperations.class
UseOfMethodsForBasicOperations.class,
AvoidMultipleIfElseStatementCheck.class
);
}
}
Loading

0 comments on commit f575288

Please sign in to comment.