2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2020-06-30 10:16:44 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2020-06-30 10:16:44 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,php]
|
2020-06-30 10:16:44 +02:00
|
|
|
----
|
|
|
|
switch ($myVariable) {
|
|
|
|
case 1:
|
|
|
|
foo();
|
|
|
|
break;
|
|
|
|
case 2: // Both 'doSomething()' and 'doSomethingElse()' will be executed. Is it on purpose ?
|
|
|
|
do_something();
|
|
|
|
default:
|
|
|
|
do_something_else();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2020-06-30 10:16:44 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,php]
|
2020-06-30 10:16:44 +02:00
|
|
|
----
|
|
|
|
switch ($myVariable) {
|
|
|
|
case 1:
|
|
|
|
foo();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
do_something();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
do_something_else();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Exceptions
|
2020-06-30 10:16:44 +02:00
|
|
|
|
|
|
|
This rule is relaxed in following cases:
|
|
|
|
|
2021-02-02 15:02:10 +01:00
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
[source,php]
|
2020-06-30 10:16:44 +02:00
|
|
|
----
|
|
|
|
switch ($myVariable) {
|
|
|
|
case 0: // Empty case used to specify the same behavior for a group of cases.
|
|
|
|
case 1:
|
|
|
|
do_something();
|
|
|
|
break;
|
|
|
|
case 2: // Use of continue statement
|
|
|
|
continue;
|
|
|
|
case 3: // Case includes a jump statement (exit, return, break &etc)
|
|
|
|
exit(0);
|
|
|
|
case 4:
|
|
|
|
echo 'Second case, which falls through';
|
|
|
|
// no break <- comment is used when fall-through is intentional in a non-empty case body
|
|
|
|
default: // For the last case, use of break statement is optional
|
|
|
|
doSomethingElse();
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../see.adoc[]
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../message.adoc[]
|
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2023-06-22 10:38:01 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|