Skip to content

Commit f3cd3a4

Browse files
committed
feat: add rules for trailing commas in function declarations
1 parent 05e593b commit f3cd3a4

File tree

5 files changed

+168
-3
lines changed

5 files changed

+168
-3
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ update-compatibility-patch-74:
2121
@git apply tests/php74-compatibility.patch
2222
@printf "Please open your editor and apply your changes\n"
2323
@until [ "$${compatibility_resolved}" == "y" ]; do read -p "Have finished your changes (y|n)? " compatibility_resolved; done && compatibility_resolved=
24-
@git diff -- tests/expected_report.txt tests/fixed > .tmp-patch && mv .tmp-patch tests/php74-compatibility.patch && git apply -R tests/php74-compatibility.patch
24+
@git diff -- tests/expected_report.txt tests/fixed tests/input > .tmp-patch && mv .tmp-patch tests/php74-compatibility.patch && git apply -R tests/php74-compatibility.patch
2525
@git commit -m 'Update compatibility patch' tests/php74-compatibility.patch
2626

2727
update-compatibility-patch-80:
2828
@git apply tests/php80-compatibility.patch
2929
@printf "Please open your editor and apply your changes\n"
3030
@until [ "$${compatibility_resolved}" == "y" ]; do read -p "Have finished your changes (y|n)? " compatibility_resolved; done && compatibility_resolved=
31-
@git diff -- tests/expected_report.txt tests/fixed > .tmp-patch-80 && mv .tmp-patch-80 tests/php80-compatibility.patch && git apply -R tests/php80-compatibility.patch
31+
@git diff -- tests/expected_report.txt tests/fixed tests/input > .tmp-patch-80 && mv .tmp-patch-80 tests/php80-compatibility.patch && git apply -R tests/php80-compatibility.patch
3232
@git commit -m 'Update compatibility patch' tests/php80-compatibility.patch
3333

3434
vendor: composer.json

lib/Doctrine/ruleset.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@
284284
<property name="spacesCountAfterArrow" value="1"/>
285285
</properties>
286286
</rule>
287+
<!-- Disallow trailing commas in single line function declarations -->
288+
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration">
289+
<properties>
290+
<property name="onlySingleLine" value="true" />
291+
</properties>
292+
</rule>
293+
<!-- Require trailing commas in multiline function declarations -->
294+
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration"/>
287295
<!-- Require closures not referencing $this be static -->
288296
<rule ref="SlevomatCodingStandard.Functions.StaticClosure"/>
289297
<!-- Forbid unused variables passed to closures via `use` -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine;
6+
7+
class TralingCommaOnFunctions
8+
{
9+
public function a(int $arg): void
10+
{
11+
}
12+
13+
public function b(
14+
int $arg
15+
): void {
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine;
6+
7+
class TralingCommaOnFunctions
8+
{
9+
public function a(int $arg): void
10+
{
11+
}
12+
13+
public function b(
14+
int $arg
15+
): void {
16+
}
17+
}

tests/php80-compatibility.patch

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ index 1421913..d52353c 100644
9797
- public ?string $nullableString = null;
9898
+ public string|null $nullableString = null;
9999
}
100+
diff --git a/tests/fixed/TralingCommaOnFunctions.php b/tests/fixed/TralingCommaOnFunctions.php
101+
index ed304c9..001650f 100644
102+
--- a/tests/fixed/TralingCommaOnFunctions.php
103+
+++ b/tests/fixed/TralingCommaOnFunctions.php
104+
@@ -11,7 +11,7 @@ class TralingCommaOnFunctions
105+
}
106+
107+
public function b(
108+
- int $arg
109+
+ int $arg,
110+
): void {
111+
}
112+
}
100113
diff --git a/tests/fixed/UselessConditions.php b/tests/fixed/UselessConditions.php
101114
index 2151b17..71e0cfb 100644
102115
--- a/tests/fixed/UselessConditions.php
@@ -205,8 +218,105 @@ index 5bbb636..7ce8a3d 100644
205218

206219
-$var = $object === null ? null : $object->property;
207220
+$var = $object?->property;
221+
diff --git a/tests/fixed/return_type_on_closures.php b/tests/fixed/return_type_on_closures.php
222+
index 134bade..80ad413 100644
223+
--- a/tests/fixed/return_type_on_closures.php
224+
+++ b/tests/fixed/return_type_on_closures.php
225+
@@ -22,7 +22,7 @@ static function (
226+
int $c,
227+
int $d,
228+
int $e,
229+
- int $b
230+
+ int $b,
231+
): void {
232+
}
233+
234+
@@ -31,7 +31,7 @@ static function (
235+
int $c,
236+
int $d,
237+
int $e,
238+
- int $b
239+
+ int $b,
240+
): void {
241+
}
242+
243+
@@ -40,7 +40,7 @@ static function (
244+
int $c,
245+
int $d,
246+
int $e,
247+
- int $b
248+
+ int $b,
249+
): void {
250+
}
251+
252+
@@ -49,7 +49,7 @@ static function (
253+
int $c,
254+
int $d,
255+
int $e,
256+
- int $b
257+
+ int $b,
258+
): void {
259+
}
260+
261+
@@ -58,6 +58,6 @@ static function (
262+
int $c,
263+
int $d,
264+
int $e,
265+
- int $b
266+
+ int $b,
267+
): void {
268+
}
269+
diff --git a/tests/fixed/return_type_on_methods.php b/tests/fixed/return_type_on_methods.php
270+
index 8e2c6f7..0c897ae 100644
271+
--- a/tests/fixed/return_type_on_methods.php
272+
+++ b/tests/fixed/return_type_on_methods.php
273+
@@ -31,7 +31,7 @@ class Test
274+
int $c,
275+
int $d,
276+
int $e,
277+
- int $b
278+
+ int $b,
279+
): void {
280+
}
281+
282+
@@ -40,7 +40,7 @@ class Test
283+
int $c,
284+
int $d,
285+
int $e,
286+
- int $b
287+
+ int $b,
288+
): void {
289+
}
290+
291+
@@ -49,7 +49,7 @@ class Test
292+
int $c,
293+
int $d,
294+
int $e,
295+
- int $b
296+
+ int $b,
297+
): void {
298+
}
299+
300+
@@ -58,7 +58,7 @@ class Test
301+
int $c,
302+
int $d,
303+
int $e,
304+
- int $b
305+
+ int $b,
306+
): void {
307+
}
308+
309+
@@ -67,7 +67,7 @@ class Test
310+
int $c,
311+
int $d,
312+
int $e,
313+
- int $b
314+
+ int $b,
315+
): void {
316+
}
317+
}
208318
diff --git a/tests/fixed/type-hints.php b/tests/fixed/type-hints.php
209-
index 6033eca..5e26ed8 100644
319+
index 10e6f34..5e26ed8 100644
210320
--- a/tests/fixed/type-hints.php
211321
+++ b/tests/fixed/type-hints.php
212322
@@ -10,7 +10,7 @@ use Traversable;
@@ -226,3 +336,16 @@ index 6033eca..5e26ed8 100644
226336
- private $x = 1;
227337
+ private int|string|null $x = 1;
228338
}
339+
diff --git a/tests/input/TralingCommaOnFunctions.php b/tests/input/TralingCommaOnFunctions.php
340+
index ed304c9..e383ee2 100644
341+
--- a/tests/input/TralingCommaOnFunctions.php
342+
+++ b/tests/input/TralingCommaOnFunctions.php
343+
@@ -6,7 +6,7 @@ namespace Doctrine;
344+
345+
class TralingCommaOnFunctions
346+
{
347+
- public function a(int $arg): void
348+
+ public function a(int $arg,): void
349+
{
350+
}
351+

0 commit comments

Comments
 (0)