diff --git a/CHANGELOG.md b/CHANGELOG.md index 79e1c74..5f87387 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- [#34](https://github.com/green-code-initiative/ecoCode-php/issues/34) Add test to ensure all Rules are registered + ### Deleted ## [1.5.1] - 2024-07-25 @@ -24,7 +26,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - upgrade docker system -- [#30](https://github.com/green-code-initiative/ecoCode-php/issues/30) Upgrade dependencies to latest ones - technical incompatibilities for SonarQube before 9.9 version +- [#30](https://github.com/green-code-initiative/ecoCode-php/issues/30) Upgrade dependencies to latest ones - technical + incompatibilities for SonarQube before 9.9 version - clean unit test files (to have less other issues) ## [1.4.4] - 2024-07-19 @@ -37,10 +40,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- [#20](https://github.com/green-code-initiative/ecoCode-php/issues/20) Add support for SonarQube 10.4 "DownloadOnlyWhenRequired" feature +- [#20](https://github.com/green-code-initiative/ecoCode-php/issues/20) Add support for SonarQube 10.4 " + DownloadOnlyWhenRequired" feature ### Changed -- [#23](https://github.com/green-code-initiative/ecoCode-php/issues/23) deprecation of EC22 rule for PHP (waiting for measurement to prove it) + +- [#23](https://github.com/green-code-initiative/ecoCode-php/issues/23) deprecation of EC22 rule for PHP (waiting for + measurement to prove it) - check Sonarqube 10.5.1 compatibility + update docker files and README.md ## [1.4.2] - 2024-01-12 @@ -58,8 +64,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- [#9](https://github.com/green-code-initiative/ecoCode-php/pull/9) Upgrade licence system and licence headers of Java files -- [#10](https://github.com/green-code-initiative/ecoCode-php/pull/10) Adding EC35 rule : EC35 rule replaces EC34 with a specific use case ("file not found" specific) +- [#9](https://github.com/green-code-initiative/ecoCode-php/pull/9) Upgrade licence system and licence headers of Java + files +- [#10](https://github.com/green-code-initiative/ecoCode-php/pull/10) Adding EC35 rule : EC35 rule replaces EC34 with a + specific use case ("file not found" specific) - [#13](https://github.com/green-code-initiative/ecoCode-php/issues/13) Add build number to manifest - [#12](https://github.com/green-code-initiative/ecoCode-php/issues/12) Fix unit tests execution with Maven - Update ecocode-rules-specifications to 1.4.6 @@ -70,7 +78,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - PHP rules moved from `ecoCode` repository to current repository -- [#121](https://github.com/green-code-initiative/ecoCode/issues/121) new PHP rule : Multiple if-else statement + refactoring implementation +- [#121](https://github.com/green-code-initiative/ecoCode/issues/121) new PHP rule : Multiple if-else statement + + refactoring implementation - [#205](https://github.com/green-code-initiative/ecoCode/issues/205) compatibility with SonarQube 10.1 ## Comparison list diff --git a/pom.xml b/pom.xml index 59d56f9..226fd39 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 io.ecocode @@ -139,6 +140,13 @@ test + + org.reflections + reflections + 0.10.2 + test + + diff --git a/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java b/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java index 3cca60c..e6aec3b 100644 --- a/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java +++ b/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java @@ -17,44 +17,17 @@ */ package fr.greencodeinitiative.php; -import java.util.List; - -import fr.greencodeinitiative.php.checks.AvoidGettingSizeCollectionInLoopCheck; 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; import org.sonarsource.analyzer.commons.RuleMetadataLoader; -public class PhpRuleRepository implements RulesDefinition, PHPCustomRuleRepository { - - private static final String LANGUAGE = "php"; - private static final String NAME = "ecoCode"; - private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/php"; - private static final String REPOSITORY_KEY = "ecocode-php"; - - private final SonarRuntime sonarRuntime; - - public PhpRuleRepository(SonarRuntime sonarRuntime) { - this.sonarRuntime = sonarRuntime; - } - - @Override - public void define(Context context) { - NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME); - RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, sonarRuntime); - ruleMetadataLoader.addRulesByAnnotatedClass(repository, checkClasses()); - repository.done(); - } +import java.util.List; - @Override - public String repositoryKey() { - return REPOSITORY_KEY; - } +public class PhpRuleRepository implements RulesDefinition, PHPCustomRuleRepository { - @Override - public List> checkClasses() { - return List.of( + static final List> ANNOTATED_RULE_CLASSES = List.of( AvoidGettingSizeCollectionInLoopCheck.class, AvoidDoubleQuoteCheck.class, AvoidFullSQLRequestCheck.class, @@ -66,5 +39,33 @@ public List> checkClasses() { UseOfMethodsForBasicOperations.class, AvoidMultipleIfElseStatementCheck.class ); - } + + private static final String LANGUAGE = "php"; + private static final String NAME = "ecoCode"; + private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/php"; + private static final String REPOSITORY_KEY = "ecocode-php"; + + private final SonarRuntime sonarRuntime; + + public PhpRuleRepository(SonarRuntime sonarRuntime) { + this.sonarRuntime = sonarRuntime; + } + + @Override + public void define(Context context) { + NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME); + RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, sonarRuntime); + ruleMetadataLoader.addRulesByAnnotatedClass(repository, checkClasses()); + repository.done(); + } + + @Override + public String repositoryKey() { + return REPOSITORY_KEY; + } + + @Override + public List> checkClasses() { + return ANNOTATED_RULE_CLASSES; + } } diff --git a/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java b/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java index 64f2181..8f82197 100644 --- a/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java +++ b/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java @@ -21,60 +21,76 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.reflections.Reflections; import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; import org.sonar.api.utils.Version; +import org.sonar.check.Rule; + +import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; class PhpRuleRepositoryTest { - private RulesDefinition.Repository repository; + private PhpRuleRepository rulesDefinition; + private RulesDefinition.Repository repository; @BeforeEach - void init() { - final SonarRuntime sonarRuntime = mock(SonarRuntime.class); - doReturn(Version.create(0, 0)).when(sonarRuntime).getApiVersion(); - PhpRuleRepository rulesDefinition = new PhpRuleRepository(sonarRuntime); - RulesDefinition.Context context = new RulesDefinition.Context(); - rulesDefinition.define(context); - repository = context.repository(rulesDefinition.repositoryKey()); - } + void init() { + final SonarRuntime sonarRuntime = mock(SonarRuntime.class); + doReturn(Version.create(0, 0)).when(sonarRuntime).getApiVersion(); + rulesDefinition = new PhpRuleRepository(sonarRuntime); + RulesDefinition.Context context = new RulesDefinition.Context(); + rulesDefinition.define(context); + repository = context.repository(rulesDefinition.repositoryKey()); + } + + @Test + @DisplayName("Test repository metadata") + void testMetadata() { + assertThat(repository.name()).isEqualTo("ecoCode"); + assertThat(repository.language()).isEqualTo("php"); + assertThat(repository.key()).isEqualTo("ecocode-php"); + } + + @Test + void testRegistredRules() { + assertThat(rulesDefinition.checkClasses()) + .describedAs("All implemented rules must be registered into " + PhpRuleRepository.class) + .containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0])); + } - @Test - @DisplayName("Test repository metadata") - void testMetadata() { - assertThat(repository.name()).isEqualTo("ecoCode"); - assertThat(repository.language()).isEqualTo("php"); - assertThat(repository.key()).isEqualTo("ecocode-php"); - } + @Test + void checkNumberRules() { + assertThat(repository.rules()).hasSize(PhpRuleRepository.ANNOTATED_RULE_CLASSES.size()); + } - @Test - void testRegistredRules() { - assertThat(repository.rules()).hasSize(10); - } + @Test + @DisplayName("All rule keys must be prefixed by 'EC'") + void testRuleKeyPrefix() { + SoftAssertions assertions = new SoftAssertions(); + repository.rules().forEach( + rule -> assertions.assertThat(rule.key()).startsWith("EC") + ); + assertions.assertAll(); + } - @Test - @DisplayName("All rule keys must be prefixed by 'EC'") - void testRuleKeyPrefix() { - SoftAssertions assertions = new SoftAssertions(); - repository.rules().forEach( - rule -> assertions.assertThat(rule.key()).startsWith("EC") - ); - assertions.assertAll(); - } + @Test + void testAllRuleParametersHaveDescription() { + SoftAssertions assertions = new SoftAssertions(); + repository.rules().stream() + .flatMap(rule -> rule.params().stream()) + .forEach(param -> assertions.assertThat(param.description()) + .as("description for " + param.key()) + .isNotEmpty()); + assertions.assertAll(); + } - @Test - void testAllRuleParametersHaveDescription() { - SoftAssertions assertions = new SoftAssertions(); - repository.rules().stream() - .flatMap(rule -> rule.params().stream()) - .forEach(param -> assertions.assertThat(param.description()) - .as("description for " + param.key()) - .isNotEmpty()); - assertions.assertAll(); - } + private static Set> getDefinedRules() { + Reflections r = new Reflections(PhpRuleRepository.class.getPackageName() + ".checks"); + return r.getTypesAnnotatedWith(Rule.class); + } }