|
1 | 1 | /* |
2 | 2 | * creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs |
3 | | - * Copyright © 2023 Green Code Initiative (https://green-code-initiative.org) |
| 3 | + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org) |
4 | 4 | * |
5 | 5 | * This program is free software: you can redistribute it and/or modify |
6 | 6 | * it under the terms of the GNU General Public License as published by |
|
15 | 15 | * You should have received a copy of the GNU General Public License |
16 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | */ |
18 | | -package fr.greencodeinitiative.python.checks; |
| 18 | +package org.greencodeinitiative.creedengo.python.checks; |
19 | 19 |
|
20 | 20 | import java.util.List; |
21 | 21 | import java.util.stream.Collectors; |
|
28 | 28 | import org.sonar.plugins.python.api.tree.FunctionDef; |
29 | 29 | import org.sonar.plugins.python.api.tree.ParameterList; |
30 | 30 | import org.sonar.plugins.python.api.tree.QualifiedExpression; |
31 | | -import org.sonar.plugins.python.api.tree.ReturnStatement; |
32 | 31 | import org.sonar.plugins.python.api.tree.Statement; |
33 | 32 | import org.sonar.plugins.python.api.tree.StatementList; |
34 | 33 | import org.sonar.plugins.python.api.tree.Tree; |
35 | 34 | import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; |
36 | 35 |
|
37 | | -@Rule(key = "EC7") |
| 36 | +@Rule(key = "GCI7") |
| 37 | +@DeprecatedRuleKey(repositoryKey = "ecocode-python", ruleKey = "EC7") |
38 | 38 | @DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "D7") |
39 | 39 | public class AvoidGettersAndSetters extends PythonSubscriptionCheck { |
40 | 40 |
|
@@ -62,36 +62,36 @@ private boolean isConstructorMethod(FunctionDef functionDef) { |
62 | 62 | return functionDef.name() != null && "__init__".equals(functionDef.name().name()); |
63 | 63 | } |
64 | 64 |
|
65 | | - public void checkAllSetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) { |
| 65 | + private void checkAllSetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) { |
66 | 66 | if (statements.size() == 1 && statements.get(0).is(Tree.Kind.ASSIGNMENT_STMT)) { |
67 | 67 | AssignmentStatement assignmentStatement = (AssignmentStatement) statements.get(0); |
68 | 68 | if (checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot((QualifiedExpression) assignmentStatement.children().get(0).children().get(0))) { |
69 | 69 | // Check if assignedValue is a parameter of the function |
70 | 70 | ParameterList parameters = functionDef.parameters(); |
71 | | - if (parameters != null && !parameters.all().stream().filter(p -> checkAssignementFromParameter(assignmentStatement, p)).collect(Collectors.toList()).isEmpty()) { |
| 71 | + if (parameters != null && !parameters.all().stream().filter(p -> checkAssignmentFromParameter(assignmentStatement, p)).collect(Collectors.toList()).isEmpty()) { |
72 | 72 | ctx.addIssue(functionDef.defKeyword(), AvoidGettersAndSetters.DESCRIPTION); |
73 | 73 | } |
74 | 74 | } |
75 | 75 | } |
76 | 76 | } |
77 | 77 |
|
78 | | - public void checkAllGetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) { |
| 78 | + private void checkAllGetters(List<Statement> statements, FunctionDef functionDef, SubscriptionContext ctx) { |
79 | 79 | Statement lastStatement = statements.get(statements.size() - 1); |
80 | 80 | if (lastStatement.is(Tree.Kind.RETURN_STMT)) { |
81 | | - List<Tree> returnStatementChildren = ((ReturnStatement) lastStatement).children(); |
| 81 | + List<Tree> returnStatementChildren = lastStatement.children(); |
82 | 82 | if (returnStatementChildren.get(1).is(Tree.Kind.QUALIFIED_EXPR) && |
83 | 83 | checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot((QualifiedExpression) returnStatementChildren.get(1))) { |
84 | 84 | ctx.addIssue(functionDef.defKeyword(), AvoidGettersAndSetters.DESCRIPTION); |
85 | 85 | } |
86 | 86 | } |
87 | 87 | } |
88 | 88 |
|
89 | | - public boolean checkAssignementFromParameter(AssignmentStatement assignmentStatement, AnyParameter parameter) { |
| 89 | + private boolean checkAssignmentFromParameter(AssignmentStatement assignmentStatement, AnyParameter parameter) { |
90 | 90 | String parameterToString = parameter.firstToken().value(); |
91 | 91 | return assignmentStatement.assignedValue().firstToken().value().equalsIgnoreCase(parameterToString); |
92 | 92 | } |
93 | 93 |
|
94 | | - public boolean checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot(QualifiedExpression qualifiedExpression) { |
| 94 | + private boolean checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot(QualifiedExpression qualifiedExpression) { |
95 | 95 | List<Tree> qualifedExpressionChildren = qualifiedExpression.children(); |
96 | 96 | return qualifedExpressionChildren.size() == 3 && |
97 | 97 | qualifedExpressionChildren.get(0).firstToken().value().equalsIgnoreCase("self") && |
|
0 commit comments