Skip to content

Commit d8a60ec

Browse files
authored
Merge pull request #146 from simPod/do-not-require-space-after-case
Do not require space after case
2 parents 9056d24 + e91edf0 commit d8a60ec

File tree

7 files changed

+247
-127
lines changed

7 files changed

+247
-127
lines changed

lib/Doctrine/ruleset.xml

+12
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@
209209
<rule ref="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing">
210210
<exclude name="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountBeforeControlStructure" />
211211
<exclude name="SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing.IncorrectLinesCountBeforeFirstControlStructure" />
212+
<properties>
213+
<property name="tokensToCheck" type="array">
214+
<element value="T_IF" />
215+
<element value="T_DO" />
216+
<element value="T_WHILE" />
217+
<element value="T_FOR" />
218+
<element value="T_FOREACH" />
219+
<element value="T_SWITCH" />
220+
<element value="T_TRY" />
221+
<element value="T_DEFAULT" />
222+
</property>
223+
</properties>
212224
</rule>
213225
<!-- Forbid fancy yoda conditions -->
214226
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowYodaComparison"/>

tests/expected_report.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ tests/input/class-references.php 10 0
99
tests/input/concatenation_spacing.php 24 0
1010
tests/input/constants-no-lsb.php 2 0
1111
tests/input/constants-var.php 4 0
12+
tests/input/ControlStructures.php 13 0
1213
tests/input/doc-comment-spacing.php 10 0
1314
tests/input/duplicate-assignment-variable.php 1 0
1415
tests/input/EarlyReturn.php 6 0
15-
tests/input/example-class.php 45 0
16+
tests/input/example-class.php 34 0
1617
tests/input/forbidden-comments.php 8 0
1718
tests/input/forbidden-functions.php 6 0
1819
tests/input/inline_type_hint_assertions.php 7 0
@@ -38,9 +39,9 @@ tests/input/use-ordering.php 1 0
3839
tests/input/useless-semicolon.php 2 0
3940
tests/input/UselessConditions.php 20 0
4041
----------------------------------------------------------------------
41-
A TOTAL OF 290 ERRORS AND 0 WARNINGS WERE FOUND IN 34 FILES
42+
A TOTAL OF 292 ERRORS AND 0 WARNINGS WERE FOUND IN 35 FILES
4243
----------------------------------------------------------------------
43-
PHPCBF CAN FIX 229 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
44+
PHPCBF CAN FIX 231 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
4445
----------------------------------------------------------------------
4546

4647

tests/fixed/ControlStructures.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ControlStructures;
6+
7+
use InvalidArgumentException;
8+
use Throwable;
9+
use const PHP_VERSION;
10+
11+
class ControlStructures
12+
{
13+
private const VERSION = PHP_VERSION;
14+
15+
/**
16+
* @return iterable<int>
17+
*/
18+
public function varAndIfNoSpaceBetween() : iterable
19+
{
20+
$var = 1;
21+
if (self::VERSION === 0) {
22+
yield 0;
23+
}
24+
}
25+
26+
/**
27+
* @return iterable<int>
28+
*/
29+
public function ifAndYieldSpaceBetween() : iterable
30+
{
31+
if (self::VERSION === 0) {
32+
yield 0;
33+
}
34+
35+
yield 1;
36+
}
37+
38+
/**
39+
* @return iterable<int>
40+
*/
41+
public function ifAndYieldFromSpaceBetween() : iterable
42+
{
43+
if (self::VERSION === 0) {
44+
yield 0;
45+
}
46+
47+
yield from [];
48+
}
49+
50+
public function ifAndThrowSpaceBetween() : void
51+
{
52+
if (self::VERSION === 0) {
53+
return;
54+
}
55+
56+
throw new InvalidArgumentException();
57+
}
58+
59+
public function ifAndReturnSpaceBetween() : int
60+
{
61+
if (self::VERSION === 0) {
62+
return 0;
63+
}
64+
65+
return 1;
66+
}
67+
68+
public function noSpaceAroundCase() : void
69+
{
70+
switch (self::VERSION) {
71+
case 1:
72+
case 2:
73+
// do something
74+
break;
75+
case 3:
76+
// do something else
77+
break;
78+
default:
79+
}
80+
}
81+
82+
public function spaceBelowBlocks() : void
83+
{
84+
if (true) {
85+
echo 1;
86+
}
87+
88+
do {
89+
echo 2;
90+
} while (true);
91+
92+
while (true) {
93+
echo 3;
94+
}
95+
96+
for ($i = 0; $i < 1; $i++) {
97+
echo $i;
98+
}
99+
100+
foreach ([] as $item) {
101+
echo $item;
102+
}
103+
104+
switch (true) {
105+
default:
106+
}
107+
108+
try {
109+
echo 4;
110+
} catch (Throwable $throwable) {
111+
}
112+
113+
echo 5;
114+
}
115+
}

tests/fixed/example-class.php

-61
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Fancy\TestCase;
1010
use InvalidArgumentException;
1111
use IteratorAggregate;
12-
use Throwable;
1312
use function assert;
1413
use function strlen as stringLength;
1514
use function substr;
@@ -90,64 +89,4 @@ public static function getTestCase() : TestCase
9089
{
9190
return new TestCase();
9291
}
93-
94-
/**
95-
* @return iterable<int>
96-
*/
97-
public function yieldSomething() : iterable
98-
{
99-
if (self::VERSION === 0) {
100-
yield 0;
101-
}
102-
103-
yield 1;
104-
}
105-
106-
/**
107-
* @return iterable<int>
108-
*/
109-
public function yieldFromSomething() : iterable
110-
{
111-
if (self::VERSION === 0) {
112-
yield 0;
113-
}
114-
115-
yield from [];
116-
}
117-
118-
public function throwWhenInvalid() : void
119-
{
120-
if (self::VERSION === 0) {
121-
return;
122-
}
123-
124-
throw new InvalidArgumentException();
125-
}
126-
127-
public function trySwitchSpace() : void
128-
{
129-
try {
130-
$var = 1;
131-
switch (self::VERSION) {
132-
default:
133-
}
134-
135-
foreach ([] as $item) {
136-
echo $item;
137-
}
138-
139-
while (true) {
140-
echo 2;
141-
}
142-
143-
if (true) {
144-
echo 3;
145-
}
146-
147-
echo 1;
148-
} catch (Throwable $throwable) {
149-
}
150-
151-
echo 2;
152-
}
15392
}

tests/input/ControlStructures.php

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ControlStructures;
6+
7+
use InvalidArgumentException;
8+
use Throwable;
9+
use const PHP_VERSION;
10+
11+
class ControlStructures
12+
{
13+
private const VERSION = PHP_VERSION;
14+
15+
/**
16+
* @return iterable<int>
17+
*/
18+
public function varAndIfNoSpaceBetween() : iterable
19+
{
20+
$var = 1;
21+
if (self::VERSION === 0) {
22+
yield 0;
23+
}
24+
}
25+
26+
/**
27+
* @return iterable<int>
28+
*/
29+
public function ifAndYieldSpaceBetween() : iterable
30+
{
31+
if (self::VERSION === 0) {
32+
yield 0;
33+
}
34+
yield 1;
35+
}
36+
37+
/**
38+
* @return iterable<int>
39+
*/
40+
public function ifAndYieldFromSpaceBetween() : iterable
41+
{
42+
if (self::VERSION === 0) {
43+
yield 0;
44+
}
45+
yield from [];
46+
}
47+
48+
public function ifAndThrowSpaceBetween() : void
49+
{
50+
if (self::VERSION === 0) {
51+
return;
52+
}
53+
throw new InvalidArgumentException();
54+
}
55+
56+
public function ifAndReturnSpaceBetween() : int
57+
{
58+
if (self::VERSION === 0) {
59+
return 0;
60+
}
61+
62+
return 1;
63+
}
64+
65+
public function noSpaceAroundCase() : void
66+
{
67+
switch (self::VERSION) {
68+
case 1:
69+
case 2:
70+
// do something
71+
break;
72+
case 3:
73+
// do something else
74+
break;
75+
default:
76+
}
77+
}
78+
79+
public function spaceBelowBlocks() : void
80+
{
81+
if (true) {
82+
echo 1;
83+
}
84+
do {
85+
echo 2;
86+
} while (true);
87+
while (true) {
88+
echo 3;
89+
}
90+
for ($i = 0; $i < 1; $i++) {
91+
echo $i;
92+
}
93+
foreach ([] as $item) {
94+
echo $item;
95+
}
96+
switch (true) {
97+
default:
98+
}
99+
try {
100+
echo 4;
101+
} catch (Throwable $throwable) {
102+
}
103+
echo 5;
104+
}
105+
}

tests/input/example-class.php

-52
Original file line numberDiff line numberDiff line change
@@ -89,56 +89,4 @@ public static function getTestCase() : TestCase
8989
return new TestCase();
9090
}
9191

92-
/**
93-
* @return iterable<int>
94-
*/
95-
public function yieldSomething() : iterable
96-
{
97-
if (self::VERSION === 0) {
98-
yield 0;
99-
}
100-
yield 1;
101-
}
102-
103-
/**
104-
* @return iterable<int>
105-
*/
106-
public function yieldFromSomething() : iterable
107-
{
108-
if (self::VERSION === 0) {
109-
yield 0;
110-
}
111-
yield from [];
112-
}
113-
114-
public function throwWhenInvalid() : void
115-
{
116-
if (self::VERSION === 0) {
117-
return;
118-
}
119-
throw new \InvalidArgumentException();
120-
}
121-
122-
public function trySwitchSpace() : void
123-
{
124-
try {
125-
$var = 1;
126-
switch (self::VERSION) {
127-
default:
128-
}
129-
foreach ([] as $item) {
130-
echo $item;
131-
}
132-
while (true) {
133-
echo 2;
134-
}
135-
if (true) {
136-
echo 3;
137-
}
138-
echo 1;
139-
} catch (Throwable $throwable) {
140-
}
141-
echo 2;
142-
}
143-
14492
}

0 commit comments

Comments
 (0)