Skip to content

Commit 1f1aa19

Browse files
carusogabrielostrolucky
authored andcommitted
Ensure that spacing around cases are consistent
As reported via slevomat/coding-standard#867, in case that a switch with cases returning or breaking, the `JumpStatementsSpacing` sniff wasn't handling it properly. These 3 new options added ensure that this sniff handles this case as well.
1 parent e098fd0 commit 1f1aa19

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

lib/Doctrine/ruleset.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@
240240
<!-- Require consistent spacing for jump statements -->
241241
<rule ref="SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing">
242242
<properties>
243+
<property name="linesCountBeforeWhenFirstInCaseOrDefault" value="0"/>
244+
<property name="linesCountAfterWhenLastInCaseOrDefault" value="1"/>
245+
<property name="linesCountAfterWhenLastInLastCaseOrDefault" value="0"/>
243246
<property name="tokensToCheck" type="array">
244247
<element value="T_RETURN" />
245248
<element value="T_THROW" />

tests/fixed/ControlStructures.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,21 @@ public function spaceAroundMultilineIfs(): void
128128
echo 2;
129129
}
130130
}
131+
132+
public function spacingAroundCasesWithBreakAndReturn(): void
133+
{
134+
switch (true) {
135+
case 1:
136+
throw new InvalidArgumentException();
137+
138+
case 2:
139+
return;
140+
141+
case 3:
142+
break;
143+
144+
case 4:
145+
echo 1;
146+
}
147+
}
131148
}

tests/input/ControlStructures.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,28 @@ public function spaceAroundMultilineIfs(): void
115115
echo 2;
116116
}
117117
}
118+
119+
public function spacingAroundCasesWithBreakAndReturn(): void
120+
{
121+
switch (true) {
122+
123+
case 1:
124+
125+
throw new InvalidArgumentException();
126+
127+
case 2:
128+
129+
return;
130+
131+
132+
case 3:
133+
134+
break;
135+
136+
case 4:
137+
138+
echo 1;
139+
140+
}
141+
}
118142
}

tests/php-compatibility.patch

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
2-
index 1e809f9..4776284 100644
2+
index 1e809f9..490fcbd 100644
33
--- a/tests/expected_report.txt
44
+++ b/tests/expected_report.txt
55
@@ -5,44 +5,48 @@ FILE ERRORS WARNINGS
@@ -12,7 +12,8 @@ index 1e809f9..4776284 100644
1212
+tests/input/concatenation_spacing.php 49 0
1313
tests/input/constants-no-lsb.php 2 0
1414
tests/input/constants-var.php 4 0
15-
tests/input/ControlStructures.php 17 0
15+
-tests/input/ControlStructures.php 17 0
16+
+tests/input/ControlStructures.php 27 0
1617
tests/input/doc-comment-spacing.php 10 0
1718
tests/input/duplicate-assignment-variable.php 1 0
1819
tests/input/EarlyReturn.php 6 0
@@ -52,10 +53,10 @@ index 1e809f9..4776284 100644
5253
tests/input/UselessConditions.php 20 0
5354
----------------------------------------------------------------------
5455
-A TOTAL OF 297 ERRORS AND 0 WARNINGS WERE FOUND IN 36 FILES
55-
+A TOTAL OF 361 ERRORS AND 0 WARNINGS WERE FOUND IN 40 FILES
56+
+A TOTAL OF 371 ERRORS AND 0 WARNINGS WERE FOUND IN 40 FILES
5657
----------------------------------------------------------------------
5758
-PHPCBF CAN FIX 236 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
58-
+PHPCBF CAN FIX 296 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
59+
+PHPCBF CAN FIX 306 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
5960
----------------------------------------------------------------------
6061

6162

0 commit comments

Comments
 (0)