Skip to content

Commit 6023ed6

Browse files
committed
PHP 8.0 | PSR2/PSR12/ControlStructureSpacing: check match expressions
This adds support for checking the spacing inside single line conditions for `match` expressions to the PSR2 sniff and by extension to the PSR12 sniff, as well as explicitly for multiline conditions to the PSR12 sniff. Includes unit tests.
1 parent b52a018 commit 6023ed6

File tree

8 files changed

+61
-0
lines changed

8 files changed

+61
-0
lines changed

src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function register()
4141
T_ELSE,
4242
T_ELSEIF,
4343
T_CATCH,
44+
T_MATCH,
4445
];
4546

4647
}//end register()

src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ EOD
8484
) {
8585
break;
8686
}
87+
88+
match (
89+
$expr1 &&
90+
$expr2 &&
91+
$expr3
92+
) {
93+
// structure body
94+
};
95+
96+
match ($expr1 &&
97+
$expr2 &&
98+
$expr3) {
99+
// structure body
100+
};

src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,19 @@ EOD
8585
) {
8686
break;
8787
}
88+
89+
match (
90+
$expr1 &&
91+
$expr2 &&
92+
$expr3
93+
) {
94+
// structure body
95+
};
96+
97+
match (
98+
$expr1 &&
99+
$expr2 &&
100+
$expr3
101+
) {
102+
// structure body
103+
};

src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public function getErrorList()
4141
48 => 2,
4242
58 => 1,
4343
59 => 1,
44+
92 => 1,
45+
96 => 1,
46+
97 => 1,
47+
98 => 2,
4448
];
4549

4650
}//end getErrorList()

src/Standards/PSR2/Sniffs/ControlStructures/ControlStructureSpacingSniff.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function register()
4747
T_ELSE,
4848
T_ELSEIF,
4949
T_CATCH,
50+
T_MATCH,
5051
];
5152

5253
}//end register()

src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,14 @@ if ($expr1
6868
&& $expr2
6969
/* comment */ ) {
7070
}
71+
72+
$r = match ($x) {};
73+
$r = match ( $x ) {};
74+
75+
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesAfterOpen 1
76+
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 1
77+
$r = match ($x) {};
78+
$r = match ( $x ) {};
79+
$r = match ( $x ) {};
80+
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesAfterOpen 0
81+
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 0

src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,14 @@ if ($expr1
6767
&& $expr2
6868
/* comment */) {
6969
}
70+
71+
$r = match ($x) {};
72+
$r = match ($x) {};
73+
74+
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesAfterOpen 1
75+
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 1
76+
$r = match ( $x ) {};
77+
$r = match ( $x ) {};
78+
$r = match ( $x ) {};
79+
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesAfterOpen 0
80+
// phpcs:set PSR2.ControlStructures.ControlStructureSpacing requiredSpacesBeforeClose 0

src/Standards/PSR2/Tests/ControlStructures/ControlStructureSpacingUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public function getErrorList()
3636
60 => 1,
3737
64 => 1,
3838
69 => 1,
39+
73 => 2,
40+
77 => 2,
41+
79 => 2,
3942
];
4043

4144
}//end getErrorList()

0 commit comments

Comments
 (0)