-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ PHP plugin ] refactoring and corrections #82
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
decf87d
refactoring + correction PHP rules system
dedece35 d1e770a
[ISSUE 64] correction of classCastException
dedece35 b699020
[ISSUE 64] correction PHP plugin review feedback
dedece35 31848f1
[ISSUE 64] correction Java plugin rule formatting
dedece35 ef8145c
[ISSUE 64] add 'ecocode' tag on PHP rules
dedece35 b29b997
Merge branch 'main' into ISSUE_64_PHP
dedece35 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* SonarQube Python Plugin | ||
* Copyright (C) 2012-2019 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package fr.greencodeinitiative.php; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
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 org.sonar.api.server.rule.RulesDefinition; | ||
import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader; | ||
import org.sonar.plugins.php.api.visitors.PHPCustomRuleRepository; | ||
|
||
public class PhpRuleRepository implements RulesDefinition, PHPCustomRuleRepository { | ||
|
||
public static final String LANGUAGE = "php"; | ||
public static final String NAME = "Green Code Initiative"; | ||
public static final String RESOURCE_BASE_PATH = "/fr/greencodeinitiative/l10n/php/rules/custom/"; | ||
public static final String REPOSITORY_KEY = "gci-php"; | ||
|
||
@Override | ||
public void define(Context context) { | ||
NewRepository repository = context.createRepository(repositoryKey(), LANGUAGE).setName(NAME); | ||
|
||
new RulesDefinitionAnnotationLoader().load(repository, checkClasses().toArray(new Class[] {})); | ||
|
||
// technical debt | ||
Map<String, String> remediationCosts = new HashMap<>(); | ||
remediationCosts.put(AvoidSQLRequestInLoopCheck.RULE_KEY, "10min"); | ||
remediationCosts.put(AvoidFullSQLRequestCheck.RULE_KEY, "20min"); | ||
repository.rules().forEach(rule -> { | ||
String debt = remediationCosts.get(rule.key()); | ||
|
||
// TODO DDC : create support to use org.apache.commons.lang.StringUtils | ||
// if (StringUtils.isBlank(debt)) { | ||
if (debt == null || debt.trim().equals("")) { | ||
// default debt to 5min for issue correction | ||
rule.setDebtRemediationFunction( | ||
rule.debtRemediationFunctions().constantPerIssue("5min")); | ||
} else { | ||
rule.setDebtRemediationFunction( | ||
rule.debtRemediationFunctions().constantPerIssue(debt)); | ||
} | ||
}); | ||
|
||
// HTML description | ||
repository.rules().forEach(rule -> | ||
rule.setHtmlDescription(loadResource(RESOURCE_BASE_PATH + rule.key() + ".html"))); | ||
|
||
repository.done(); | ||
} | ||
|
||
@Override | ||
public String repositoryKey() { | ||
return REPOSITORY_KEY; | ||
} | ||
|
||
@Override | ||
public List<Class<?>> checkClasses() { | ||
return ImmutableList.of( | ||
utarwyn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AvoidDoubleQuoteCheck.class, | ||
AvoidFullSQLRequestCheck.class, | ||
AvoidSQLRequestInLoopCheck.class, | ||
AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.class, | ||
AvoidUsingGlobalVariablesCheck.class, | ||
IncrementCheck.class, | ||
NoFunctionCallWhenDeclaringForLoop.class, | ||
UseOfMethodsForBasicOperations.class | ||
); | ||
} | ||
|
||
private String loadResource(String path) { | ||
URL resource = getClass().getResource(path); | ||
if (resource == null) { | ||
throw new IllegalStateException("Resource not found: " + path); | ||
} | ||
ByteArrayOutputStream result = new ByteArrayOutputStream(); | ||
try (InputStream in = resource.openStream()) { | ||
byte[] buffer = new byte[1024]; | ||
for (int len = in.read(buffer); len != -1; len = in.read(buffer)) { | ||
result.write(buffer, 0, len); | ||
} | ||
return new String(result.toByteArray(), StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Failed to read resource: " + path, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will have to specify here if the remediation cost of a rule is different from 5 minutes? Maybe create a more modular and readable list outside the algorithm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@utarwyn,
yes you are right.
indeed, I spend a lot of time to analyse why python and php plugin didn't work since upgrade libraries version in december 2022.
I wanted find a quick solution which turn on again these two plugins for the hackathon in order to have all plugins working for developers.
But the real solution, for me, must be found after the hackathon with more time to keep a solution like done in java plugin. For example, rollback JSON files which describe this kind of information.
I created an issue for that : #80
I'm waiting for @jhertout review on next monday, to merge this branch and to have all plugins working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the detailed answer, I understand that we have to make the plugin work for the hackaton. My comments are only open remarks and proposals, even to be applied later. I don't have yet the hindsight to validate this code, I let Julien look at it 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @dedece35,
I agree with @utarwyn but as you say for the moment the important thing is to have the project to work for the hackaton. I suppose it is for the same reason you leave comments in the pom files?
The review is OK for me. I tested with the php test project and I have no problems.
Some minor things you may want to fix here (or in an other PR):
// NOK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jhertout
thank you for feedback.
Problems corrected on rule "Prefer local variables to globals". I corrected also the exact same problem on java plugin.
On the other hand, I don't find / understand your problem on rule "Use of methods for basic operations".
See my capture : 2 lines with the problem and with "NOK" on each.
If there was a problem, unit tests wouldn't pass. No ?
I merge this PR.
We can correct your second point on other PR.