From 93e052d5dd8beb9e769711cda7e5fa1b1ceef7c4 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Fri, 26 Jul 2024 21:58:40 +0200 Subject: [PATCH 1/4] [ISSUE 34] format source --- pom.xml | 3 +- .../php/PhpRuleRepository.java | 73 +++++++++-------- .../php/PhpRuleRepositoryTest.java | 79 +++++++++---------- 3 files changed, 77 insertions(+), 78 deletions(-) diff --git a/pom.xml b/pom.xml index 59d56f9..630cd68 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 io.ecocode diff --git a/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java b/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java index 3cca60c..88986db 100644 --- a/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java +++ b/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java @@ -17,54 +17,53 @@ */ 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; +import java.util.List; + 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 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; + private final SonarRuntime sonarRuntime; - public PhpRuleRepository(SonarRuntime sonarRuntime) { - this.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 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 String repositoryKey() { + return REPOSITORY_KEY; + } - @Override - public List> checkClasses() { - return List.of( - AvoidGettingSizeCollectionInLoopCheck.class, - AvoidDoubleQuoteCheck.class, - AvoidFullSQLRequestCheck.class, - AvoidSQLRequestInLoopCheck.class, - AvoidTryCatchWithFileOpenedCheck.class, - AvoidUsingGlobalVariablesCheck.class, - IncrementCheck.class, - NoFunctionCallWhenDeclaringForLoop.class, - UseOfMethodsForBasicOperations.class, - AvoidMultipleIfElseStatementCheck.class - ); - } + @Override + public List> checkClasses() { + return List.of( + AvoidGettingSizeCollectionInLoopCheck.class, + AvoidDoubleQuoteCheck.class, + AvoidFullSQLRequestCheck.class, + AvoidSQLRequestInLoopCheck.class, + AvoidTryCatchWithFileOpenedCheck.class, + AvoidUsingGlobalVariablesCheck.class, + IncrementCheck.class, + NoFunctionCallWhenDeclaringForLoop.class, + UseOfMethodsForBasicOperations.class, + AvoidMultipleIfElseStatementCheck.class + ); + } } diff --git a/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java b/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java index 64f2181..0734ae3 100644 --- a/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java +++ b/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java @@ -26,55 +26,54 @@ import org.sonar.api.utils.Version; 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 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(); + PhpRuleRepository 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 + @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(repository.rules()).hasSize(10); - } + @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(); + } } From 094e9d18e1947e7b389ce8c7a8b41a6eaf41acf2 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Fri, 26 Jul 2024 22:14:43 +0200 Subject: [PATCH 2/4] [ISSUE 34] Add test to ensure all Rules are registered --- pom.xml | 7 +++++ .../php/PhpRuleRepository.java | 26 ++++++++++--------- .../php/PhpRuleRepositoryTest.java | 21 +++++++++++++-- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index 630cd68..226fd39 100644 --- a/pom.xml +++ b/pom.xml @@ -140,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 88986db..e6aec3b 100644 --- a/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java +++ b/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java @@ -27,6 +27,19 @@ public class PhpRuleRepository implements RulesDefinition, PHPCustomRuleRepository { + static final List> ANNOTATED_RULE_CLASSES = List.of( + AvoidGettingSizeCollectionInLoopCheck.class, + AvoidDoubleQuoteCheck.class, + AvoidFullSQLRequestCheck.class, + AvoidSQLRequestInLoopCheck.class, + AvoidTryCatchWithFileOpenedCheck.class, + AvoidUsingGlobalVariablesCheck.class, + IncrementCheck.class, + NoFunctionCallWhenDeclaringForLoop.class, + 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"; @@ -53,17 +66,6 @@ public String repositoryKey() { @Override public List> checkClasses() { - return List.of( - AvoidGettingSizeCollectionInLoopCheck.class, - AvoidDoubleQuoteCheck.class, - AvoidFullSQLRequestCheck.class, - AvoidSQLRequestInLoopCheck.class, - AvoidTryCatchWithFileOpenedCheck.class, - AvoidUsingGlobalVariablesCheck.class, - IncrementCheck.class, - NoFunctionCallWhenDeclaringForLoop.class, - UseOfMethodsForBasicOperations.class, - AvoidMultipleIfElseStatementCheck.class - ); + 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 0734ae3..8f82197 100644 --- a/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java +++ b/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java @@ -21,9 +21,13 @@ 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.mockito.Mockito.doReturn; @@ -31,13 +35,14 @@ class PhpRuleRepositoryTest { + 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 = new PhpRuleRepository(sonarRuntime); RulesDefinition.Context context = new RulesDefinition.Context(); rulesDefinition.define(context); repository = context.repository(rulesDefinition.repositoryKey()); @@ -53,7 +58,14 @@ void testMetadata() { @Test void testRegistredRules() { - assertThat(repository.rules()).hasSize(10); + assertThat(rulesDefinition.checkClasses()) + .describedAs("All implemented rules must be registered into " + PhpRuleRepository.class) + .containsExactlyInAnyOrder(getDefinedRules().toArray(new Class[0])); + } + + @Test + void checkNumberRules() { + assertThat(repository.rules()).hasSize(PhpRuleRepository.ANNOTATED_RULE_CLASSES.size()); } @Test @@ -76,4 +88,9 @@ void testAllRuleParametersHaveDescription() { .isNotEmpty()); assertions.assertAll(); } + + private static Set> getDefinedRules() { + Reflections r = new Reflections(PhpRuleRepository.class.getPackageName() + ".checks"); + return r.getTypesAnnotatedWith(Rule.class); + } } From f3d1b963e37a238d92b14e48180c176a2e81753e Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Fri, 26 Jul 2024 22:17:51 +0200 Subject: [PATCH 3/4] [ISSUE 34] reformat CHANGELOG --- CHANGELOG.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79e1c74..ab8e3e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,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 +38,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 +62,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 +76,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 From 3403732875f58c231c783c71a627afd5e89433cf Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Fri, 26 Jul 2024 22:18:31 +0200 Subject: [PATCH 4/4] [ISSUE 34] upgrade CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab8e3e8..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