Commit 4910a210 authored by Gerson SUNYE's avatar Gerson SUNYE
Browse files

Merge branch 'improvement-on-closed-issue/905-TooFewBranchesForASwitchStatement' into 'master'

Simplification of helpers on already closed issue #905 TooFewBranchesForASwitchStatement

Closes #905

See merge request naomod/mde/projet-2019!33
parents fd3b3d8b e00ecfbf
......@@ -224,18 +224,18 @@ rule numberOfClasses() {
}
-- Rule that creates an instance of Measure for the switch statement with too few branches passed in parameter
rule createMeasureForTooFewBranchesForASwitchStatement(switchStatement: java!SwitchStatement) {
rule MeasureTooFewBranchesForASwitchStatement(switchStatement: java!SwitchStatement) {
to
om: smm!ObservedMeasure (
measure <- tooFewBranchesForASwitchStatement,
measurements <- measurement
),
tooFewBranchesForASwitchStatement: smm!DimensionalMeasure (
name <- 'Violated rule : Too few branches for a switch statement',
shortDescription <- 'minimumNumberCaseForASwitch : 3'
name <- 'TooFewBranchesForASwitchStatement',
shortDescription <- 'Switch statements are intended to be used to support complex branching behaviour. Using a switch for only a few cases is ill-advised, since switches are not as easy to understand as if-then statements. In these cases use the if-then statement to increase code readability.'
),
measurement: smm!DirectMeasurement (
error <- 'Too few branches for SwitchStatement in Class : ' + switchStatement.originalCompilationUnit.name + '',
error <- 'Too few branches for SwitchStatement in Class : ' + switchStatement.originalCompilationUnit.name,
value <- thisModule.nbBranchesOfASwitchStatement(switchStatement)
)
do {
......@@ -245,12 +245,9 @@ rule createMeasureForTooFewBranchesForASwitchStatement(switchStatement: java!Swi
-- Returns the number of branches in the switch statement passed in parameter
helper def: nbBranchesOfASwitchStatement(switchStatement:java!SwitchStatement) : Integer =
switchStatement.statements->iterate(current; nbSwitchCases: Integer = 0 |
if (current.oclIsTypeOf(java!SwitchCase))
then nbSwitchCases+1
else nbSwitchCases
endif
);
switchStatement.statements->select(each | each.oclIsTypeOf(java!SwitchCase)).size()
;
-- Returns the number of breaks of the switch statement passed in parameter
helper def: nbBreakStatementOfASwitchStatement(ss:java!SwitchStatement) : Integer =
......
......@@ -28,18 +28,16 @@ helper def:isUselessStringValueOf(s:java!MethodInvocation): Boolean =
endif
;
------------------------------------ TooFewBranchesForASwitchStatement--------------------------------------
-- Returns a set of measures for each switch statement that violates the rule : number of switch cases < 3
-- To test, use input model : tooFewBranchesForASwitchStatement.xmi
helper def: tooFewBranchesForASwitchStatement(): Set(smm!Measure) =
-- Iterate through all SwitchStatement
java!SwitchStatement.allInstances()->iterate(current; measures: Set(smm!Measure) = Set{} |
-- If the number of switch cases for the current switch statement is < 3 then add a new measure to the set
if (thisModule.nbBranchesOfASwitchStatement(current) < 3)
then measures->union(Set{thisModule.createMeasureForTooFewBranchesForASwitchStatement(current)})
else measures
endif
);
-- If the number of switch cases for the current switch statement is < 3 then add a new measure to the set
java!SwitchStatement.allInstances()
-> select (switchStatement | thisModule.nbBranchesOfASwitchStatement(switchStatement) < 3)
-> collect (switchStatement | thisModule.MeasureTooFewBranchesForASwitchStatement(switchStatement))
;
--------------------------------------------- UseIndexOfChar---------------------------------------------
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment