Skip to content

Commit 5fcbb3a

Browse files
committed
[ISSUE 142] cleaning + tests minor corrections
1 parent 32b62c9 commit 5fcbb3a

File tree

3 files changed

+27
-53
lines changed

3 files changed

+27
-53
lines changed

src/main/java/fr/greencodeinitiative/python/checks/AvoidMultipleIfElseStatementCheck.java

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ public void visitNode(SubscriptionContext context, Tree pTree) {
6767

6868
FunctionDef method = (FunctionDef)pTree;
6969

70-
// if (!method.body().is(Tree.Kind.)) {
71-
// return;
72-
// }
73-
7470
// reinit data structure before each method analysis
7571
variablesStruct = new VariablesPerLevelDataStructure();
7672

@@ -91,10 +87,6 @@ private void visitNodeContent(SubscriptionContext context, List<Statement> pLstS
9187
}
9288

9389
for (Statement statement : pLstStatements) {
94-
// if (statement.is(Kind.BLOCK)) {
95-
// // the cirrent node is a block : visit block content
96-
// visitNodeContent(((BlockTree)statement).statements(), pLevel);
97-
// } else
9890
if (statement.is(IF_STMT)) {
9991
visitIfNode(context, (IfStatement)statement, pLevel);
10092
}
@@ -166,25 +158,12 @@ private void computeConditionVariables(SubscriptionContext context, BinaryExpres
166158
computeConditionVariables(context, (BinaryExpression) pBinExprTree.rightOperand(), pLevel);
167159
}
168160
} else if (pBinExprTree.is(COMPARISON)) {
169-
// else if (pBinExprTree.is(Tree.Kind.EQUAL_TO)
170-
// || pBinExprTree.is(Tree.Kind.NOT_EQUAL_TO)
171-
// || pBinExprTree.is(Tree.Kind.GREATER_THAN)
172-
// || pBinExprTree.is(Tree.Kind.GREATER_THAN_OR_EQUAL_TO)
173-
// || pBinExprTree.is(Tree.Kind.LESS_THAN_OR_EQUAL_TO)
174-
// || pBinExprTree.is(Tree.Kind.LESS_THAN)
175-
// ) {
176-
177-
// continue analyze with variables if some key-words are found
161+
162+
// continue to analyze with variables if some key-words are found
178163
if (pBinExprTree.leftOperand().is(NAME)) {
179-
// if (pBinExprTree.leftOperand().is(Tree.Kind.VARIABLE_IDENTIFIER)) {
180-
// if ("IDENTIFIER".equals(pBinExprTree.leftOperand().firstToken().type().getName())) {
181-
// computeVariables(context, (VariableIdentifierTree) pBinExprTree.leftOperand(), pLevel);
182164
computeVariables(context, pBinExprTree.leftOperand(), pLevel);
183165
}
184166
if (pBinExprTree.rightOperand().is(NAME)) {
185-
// if ("IDENTIFIER".equals(pBinExprTree.rightOperand().firstToken().type().getName())) {
186-
// if (pBinExprTree.rightOperand().is(Tree.Kind.VARIABLE_IDENTIFIER)) {
187-
// computeVariables(context, (VariableIdentifierTree) pBinExprTree.rightOperand(), pLevel);
188167
computeVariables(context, pBinExprTree.rightOperand(), pLevel);
189168
}
190169
}
@@ -195,22 +174,17 @@ private void computeConditionVariables(SubscriptionContext context, BinaryExpres
195174
* @param pVarIdTree The Variable AST structure
196175
* @param pLevel the level of structure
197176
*/
198-
// private void computeVariables(SubscriptionContext context, VariableIdentifierTree pVarIdTree, int pLevel) {
199177
private void computeVariables(SubscriptionContext context, Expression pVarIdTree, int pLevel) {
200-
// if (pVarIdTree.variableExpression().is(Kind.VARIABLE_IDENTIFIER)) {
201-
// increment the variable counter to list of all variables
202-
// int nbUsed = variablesStruct.incrementVariableUsageForLevel(pVarIdTree.text(), pLevel);
178+
// increment the variable counter to list of all variables
203179
int nbUsed = variablesStruct.incrementVariableUsageForLevel(pVarIdTree.firstToken().value(), pLevel);
204180

205-
// increment variable counter to list of variables already declared for current if or elseif struture
206-
// variablesStruct.incrementVariableUsageForLevelForCurrentIfStruct(pVarIdTree.text(), pLevel);
181+
// increment variable counter to list of variables already declared for current if or elseif struture
207182
variablesStruct.incrementVariableUsageForLevelForCurrentIfStruct(pVarIdTree.firstToken().value(), pLevel);
208183

209-
// raise an error if maximum
210-
if (nbUsed > 2) {
211-
context.addIssue(pVarIdTree.firstToken(), ERROR_MESSAGE);
212-
}
213-
// }
184+
// raise an error if maximum
185+
if (nbUsed > 2) {
186+
context.addIssue(pVarIdTree.firstToken(), ERROR_MESSAGE);
187+
}
214188
}
215189

216190
/**
@@ -306,7 +280,7 @@ private static class VariablesPerLevelDataStructure {
306280

307281
// map variable counters per level for current If / ElseIf structure
308282
// purpose : used by compute variables Else process (because Else structure is particular :
309-
// we don't know previous variables and we need previous If / ElseIf structure to know variables)
283+
// we don't know previous variables, and we need previous If / ElseIf structure to know variables)
310284
private final Map<Integer, Map<String, Integer>> mapVariablesPerLevelForCurrentIfStruct;
311285

312286
public VariablesPerLevelDataStructure() {

src/test/resources/checks/avoidMultipleIfElseStatementCompliant.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# COMPLIANT
1212
# USE CASE : compliant use case to check if a variable is used maximum twice on several IF / ELSE statements
1313
# at the same level AND no problem with several IF staments at the same level using different variables
14-
def shouldBeCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVariablesUsed():
14+
def compliant_variables_used_max_twice_and_differents_variables():
1515
nb1 = 0
1616
nb2 = 0
1717
nb3 = 0
@@ -28,7 +28,7 @@ def shouldBeCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVariablesUsed(
2828
# COMPLIANT
2929
# USE CASE : compliant use case to check if a variable is used maximum twice on several IF / ELSE statements
3030
# at the same level AND no problem with several IF staments at the same level using different variables
31-
def shouldBeCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVariablesUsedAtDiffLevels():
31+
def compliant_variables_used_max_twice_and_differents_variables_at_diff_levels():
3232
nb1 = 0
3333
nb2 = 0
3434
nb3 = 0
@@ -51,7 +51,7 @@ def shouldBeCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVariablesUsedA
5151
# COMPLIANT
5252
# USE CASE : compliant use case to check if a variable is used maximum twice on several IF / ELSE statements
5353
# at the same level AND no problem with several IF staments at the same level using different variables
54-
def shouldBeCompliantBecauseVariablesUsedMaximumTwiceAndDiffVariablesUsedAtDiffLevelsScenario2():
54+
def compliant_variables_used_max_twice_and_diff_variables_used_at_diff_levels_scenario_2():
5555
nb1 = 0
5656
nb2 = 0
5757
nb3 = 0
@@ -76,7 +76,7 @@ def shouldBeCompliantBecauseVariablesUsedMaximumTwiceAndDiffVariablesUsedAtDiffL
7676

7777
# COMPLIANT
7878
# USE CASE : compliant use case to check if one variable is used maximum twice in different IF statements
79-
def shouldBeCompliantBecauseVariableUsedMaximumTwiceInIfStatements():
79+
def compliant_variables_used_maximum_twice_in_if_statements():
8080
nb1 = 0
8181
if nb1 == 1:
8282
nb1 = 1
@@ -88,7 +88,7 @@ def shouldBeCompliantBecauseVariableUsedMaximumTwiceInIfStatements():
8888
# USE CASE : compliant use case to check if following is OK :
8989
# - two uses of the same variable
9090
# - usage of the same variable on different levels of IF statements
91-
def shouldBeCompliantBecauseSereralVariablesUsedMaximumTwiceInComposedElseStatements():
91+
def compliant_variables_used_max_twice_in_composed_else_statements():
9292
nb1 = 0
9393
nb2 = 0
9494
nb3 = 0
@@ -108,7 +108,7 @@ def shouldBeCompliantBecauseSereralVariablesUsedMaximumTwiceInComposedElseStatem
108108
# USE CASE : compliant use case to check if following is OK :
109109
# - two uses of the same variable
110110
# - usage of the same variable on different kind of test statements (IF and elif)
111-
def shouldBeCompliantBecauseVariableUsedMaximumTwiceInIfOrelifStatements():
111+
def compliant_variables_used_max_twice_in_if_orelif_statements():
112112
nb1 = 0
113113
nb2 = 10
114114
if nb1 == 1:
@@ -121,7 +121,7 @@ def shouldBeCompliantBecauseVariableUsedMaximumTwiceInIfOrelifStatements():
121121
# USE CASE : compliant use case to check if following is OK :
122122
# - two uses of the same variable
123123
# - usage of the same variable on different kind of test statements (IF and elif)
124-
def shouldBeCompliantBecauseSeveralVariablesUsedMaximumTwiceInIfOrelifStatements():
124+
def compliant_variables_used_max_twice_in_if_orelif_statements_scenario_2():
125125
nb1 = 0
126126
nb2 = 10
127127
nb3 = 3

src/test/resources/checks/avoidMultipleIfElseStatementNonCompliant.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# USE CASE : Non compliant use case to check if following is NON OK :
1313
# - two uses of the same variable
1414
# - usage of the same variable on different levels of IF statements
15-
def shouldBeCompliantBecauseVariableUsedMaximumTwiceInComposedElseStatements():
15+
def not_compliant_variables_used_max_twice_in_composed_else_statements():
1616
nb1 = 0
1717
if nb1 == 1:
1818
nb1 = 2
@@ -24,7 +24,7 @@ def shouldBeCompliantBecauseVariableUsedMaximumTwiceInComposedElseStatements():
2424
# NON COMPLIANT
2525
# USE CASE : non compliant use case to check if a variable is not used max twice on several IF / ELSE statements
2626
# at the same level
27-
def shouldBeNotCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVariablesUsed():
27+
def not_compliant_variables_used_max_twice_and_differents_variables_used():
2828
nb1 = 0
2929
nb2 = 0
3030
nb3 = 0
@@ -41,7 +41,7 @@ def shouldBeNotCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVariablesUs
4141
# NON COMPLIANT
4242
# USE CASE : NON compliant use case to check if following is NOT COMPLIANT :
4343
# one variable is used maximum in two IF / ELSE / elif statements
44-
def shouldBeNotCompliantBecauseVariablesIsUsedMoreThanTwice():
44+
def not_compliant_variable_used_more_than_twice():
4545
nb1 = 0
4646
if nb1 == 1:
4747
nb1 = 2
@@ -54,7 +54,7 @@ def shouldBeNotCompliantBecauseVariablesIsUsedMoreThanTwice():
5454
# NON COMPLIANT
5555
# USE CASE : NON compliant use case to check if following is NOT OK :
5656
# - same variable used maximum twice : no compliant because 2 IFs and 1 ELSE
57-
def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInIfStatementsAtDifferentsLevels():
57+
def not_compliant_variable_used_more_than_twice_in_if_statements_at_differents_levels():
5858
nb1 = 0
5959
if nb1 == 1:
6060
if nb1 == 2:
@@ -69,7 +69,7 @@ def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInIfStatementsAtDifferen
6969
# USE CASE : non compliant use case to check if following is NOT OK :
7070
# - two uses of the same variable : use thre times with 2 IFs and 1 ELSE
7171
# - usage of the same variable on different levels of IF statements
72-
def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatements():
72+
def not_compliant_variable_used_more_than_twice_in_composed_else_statements():
7373
nb1 = 0
7474
if nb1 == 1:
7575
nb1 = 2
@@ -84,7 +84,7 @@ def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatements
8484
# USE CASE : non compliant use case to check if following is NOT OK :
8585
# - two uses of the same variable : use thre times with 2 IFs and 1 ELSE
8686
# - usage of the same variable on different levels of IF statements
87-
def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatementsScenario2():
87+
def not_compliant_variable_used_more_than_twice_in_composed_else_statements_scenario_2():
8888
nb1 = 0
8989
if nb1 == 1:
9090
if nb1 == 3:
@@ -102,7 +102,7 @@ def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatements
102102
# USE CASE : non compliant use case to check if following is NOT OK :
103103
# - two uses of the same variable : use thre times with 2 IFs and 1 ELSE
104104
# - usage of the same variable on different levels of IF statements
105-
def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatementsScenario3():
105+
def not_compliant_variable_used_more_than_twice_in_composed_else_statements_scenario_3():
106106
nb1 = 0
107107
nb2 = 0
108108
if nb1 == 1:
@@ -121,7 +121,7 @@ def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatements
121121
# USE CASE : non compliant use case to check if following is NOT OK :
122122
# - two uses of the same variable : use thre times with 2 IFs and 1 ELSE
123123
# - usage of the same variable on different levels of IF statements
124-
def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatementsScenario4():
124+
def not_compliant_variable_used_more_than_twice_in_composed_else_statements_scenario_4():
125125
nb1 = 0
126126
nb2 = 0
127127
if nb1 == 1:
@@ -140,7 +140,7 @@ def shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatements
140140
# USE CASE : NON compliant use case to check if following is NOT OK :
141141
# - the same variable must used maximum twice
142142
# - usage of the same variable on different levels of IF / ELSE statements
143-
def shouldBeNotCompliantBecauseVariableUsedMaximumTwiceInComposedElseStatements():
143+
def not_compliant_variable_used_max_twice_in_composed_else_statements():
144144
nb1 = 0
145145
if nb1 == 1:
146146
nb1 = 2
@@ -158,7 +158,7 @@ def shouldBeNotCompliantBecauseVariableUsedMaximumTwiceInComposedElseStatements(
158158
# USE CASE : NON compliant use case to check if following is NOT OK :
159159
# - more than twice uses of the same variable
160160
# - usage of the same variable on different kind of test statements (IF and elif)
161-
def shouldBeNotCompliantBecauseTheSameVariableIsUsedMoreThanTwice():
161+
def not_compliant_the_same_variable_is_used_more_than_twice():
162162
nb1 = 0
163163
nb2 = 10
164164
if nb1 == 1:
@@ -173,7 +173,7 @@ def shouldBeNotCompliantBecauseTheSameVariableIsUsedMoreThanTwice():
173173
# USE CASE : NON compliant use case to check if following is NOT OK :
174174
# - more than twice uses of the same variable
175175
# - usage of the same variable on different kind of test statements (IF and elif)
176-
def shouldBeNotCompliantBecauseTheSameVariableIsUsedManyTimes():
176+
def not_compliant_the_same_variable_is_used_many_times():
177177
nb1 = 0
178178
nb2 = 10
179179
nb3 = 11

0 commit comments

Comments
 (0)