When evaluating a switch statement, if a case branch is selected, regardless of whether or not "fallthrough" is true, the default case branch, if present, will be selected as well, resulting in the selected case and the default case being evaluated when only the selected case should be evaluated.
Let's say you have a string and it's value is "foo". When switching on this string, we have two cases and one default case:
case "foo"
case "bar"
default case
The current behavior results in both case "foo" and the default case both evaulating, when we would expect only case "foo" to execute. Fallthrough is false on both case "foo" and case "bar".
Comments
Comment #1
heddnThis seems to resolve the issue.
Comment #2
micromegas commentedPatch from #1 caused the default case to always get removed, even if the previous case was set to fall through.
New patch overrides canEvaluate() in RulesConditionalDefaultCase to only return TRUE if there are no other valid siblings, or if the previous valid sibling was set to fall through--these are the only two conditions that we would want the default case to apply.
Comment #3
Anonymous (not verified) commentedThe patch from #2 didn't work for me... One of my conditions (the one directly above the default case) wasn't evaluated (or if it was it was then overridden by the default). 'Fall through' wasn't set anywhere.
Comment #4
micromegas commentedFixed typos in getAllSibling()
Comment #5
micromegas commentedDid anyone have a chance to test patch #3? I've been using it in production for about 3 months without any issues.
Comment #6
kliban commentedI encountered the "default" problem today. Patch #3 solved it beautifully, on a very simple switch though (one case, one default). I'll test it further on as soon as I have the opportunity. Thx for the great job.
Comment #7
rodpal commentedPatch #3 is working perfectly. Thanks @micromegas!
Comment #8
micromegas commentedThanks for testing; I also have been using this heavily for about 9 months with no issues. Setting to RTBC.
Comment #9
cattmote commentedI can confirm that Patch #3 resolved the issue for me as well. Thanks for the contribution @micromegas :)
Comment #10
johnorourke commentedThanks @micromegas , worked perfectly for me too
Comment #11
granticusiv commentedIt looks like both 7.x-1.0-beta2 and 7.x-1.x-dev have 'function canEvaluate(RulesState $state)' declaration, so I adapted the patch and it worked for me on 7.x-1.0-beta2
Edit: Silly me :S I was looking in the wrong Class/file. I've applied the patch to the correct Classes in 7.x-1.0-beta2 and it's breaking when a Case is evaluated TRUE, rather than always returning the Default case.
Comment #12
alansaviolobo commented+1 RTBC
Comment #13
heddnTo be clear here, we want patch from #4 merged? There were some comments about #3 too.
Comment #14
alansaviolobo commentedI believe the confusion is because the patch number 3 is actually uploaded in comment #4.
But yes, that was the one I tested.
Comment #15
JMTorres commented+1 Works for me in production for over a year.
Comment #16
lelivero commented#4 works perfect for me in a "multiple cases" situation.
Conditional Rules 7.x-1.0-beta2+2-dev (2013-okt-01) and Rules 7.x-2.10
Comment #18
alansaviolobo commented