Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rules for trailing commas in function calls and closure use #270

Merged
merged 1 commit into from Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/continuous-integration.yml
Expand Up @@ -130,6 +130,7 @@ jobs:
matrix:
php-version:
- "7.2"
- "7.3"
Comment on lines 132 to +133
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need these? They are EOL and abandoned

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintenance is kind of burden. I basically have to patch test inputs and outputs for each php version so I can add tests for new sniffs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, I was suggesting to drop support overall

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that was an agreement from my side :D

but as discussed #268 (comment)

- "7.4"
- "8.0"
- "8.1"
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Expand Up @@ -8,6 +8,13 @@ test-report: vendor
test-fix: vendor
./bin/test-fix

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

update-compatibility-patch-74:
@git apply tests/php74-compatibility.patch
@printf "Please open your editor and apply your changes\n"
Expand Down
16 changes: 16 additions & 0 deletions lib/Doctrine/ruleset.xml
Expand Up @@ -291,12 +291,28 @@
<property name="spacesCountAfterArrow" value="1"/>
</properties>
</rule>
<!-- Disallow trailing commas in single line function calls -->
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall">
<properties>
<property name="onlySingleLine" value="true" />
</properties>
</rule>
<!-- Disallow trailing commas in single line closure use -->
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse">
<properties>
<property name="onlySingleLine" value="true" />
</properties>
</rule>
<!-- Disallow trailing commas in single line function declarations -->
<rule ref="SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration">
<properties>
<property name="onlySingleLine" value="true" />
</properties>
</rule>
<!-- Require trailing commas in multiline function calls -->
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall"/>
<!-- Require trailing commas in multiline closure use -->
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInClosureUse"/>
<!-- Require trailing commas in multiline function declarations -->
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration"/>
<!-- Require closures not referencing $this be static -->
Expand Down
27 changes: 27 additions & 0 deletions tests/fixed/TrailingCommaOnFunctions.php
Expand Up @@ -4,6 +4,10 @@

namespace Doctrine;

use function var_dump;

// phpcs:disable PSR1.Files.SideEffects

class TrailingCommaOnFunctions
{
public function a(int $arg): void
Expand All @@ -14,4 +18,27 @@ public function b(
int $arg
): void {
}

public function uses(): void
{
$var = null;

$singleLine = static function (int $arg) use ($var): void {
var_dump($var);
};

$multiLine = static function (int $arg) use (
$var
): void {
var_dump($var);
};
}
}

$class = new TrailingCommaOnFunctions();

$class->a(1);

$class->a(
1
);
27 changes: 27 additions & 0 deletions tests/input/TrailingCommaOnFunctions.php
Expand Up @@ -4,6 +4,10 @@

namespace Doctrine;

use function var_dump;

// phpcs:disable PSR1.Files.SideEffects

class TrailingCommaOnFunctions
{
public function a(int $arg): void
Expand All @@ -14,4 +18,27 @@ public function b(
int $arg
): void {
}

public function uses(): void
{
$var = null;

$singleLine = static function (int $arg) use ($var): void {
var_dump($var);
};

$multiLine = static function (int $arg) use (
$var
): void {
var_dump($var);
};
}
}

$class = new TrailingCommaOnFunctions();

$class->a(1);

$class->a(
1
);
109 changes: 109 additions & 0 deletions tests/php73-compatibility.patch
@@ -0,0 +1,109 @@
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
index 1d5a7d3..fae9e70 100644
--- a/tests/expected_report.txt
+++ b/tests/expected_report.txt
@@ -39,6 +39,7 @@ tests/input/strings.php 1 0
tests/input/superfluous-naming.php 11 0
tests/input/test-case.php 8 0
tests/input/trailing_comma_on_array.php 1 0
+tests/input/TrailingCommaOnFunctions.php 2 0
tests/input/traits-uses.php 11 0
tests/input/type-hints.php 7 0
tests/input/UnusedVariables.php 1 0
@@ -46,9 +47,9 @@ tests/input/use-ordering.php 1 0
tests/input/useless-semicolon.php 2 0
tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
-A TOTAL OF 381 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
+A TOTAL OF 383 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
----------------------------------------------------------------------
-PHPCBF CAN FIX 315 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 317 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


diff --git a/tests/fixed/TrailingCommaOnFunctions.php b/tests/fixed/TrailingCommaOnFunctions.php
index f3ffa91..67173b3 100644
--- a/tests/fixed/TrailingCommaOnFunctions.php
+++ b/tests/fixed/TrailingCommaOnFunctions.php
@@ -37,8 +37,9 @@ class TrailingCommaOnFunctions

$class = new TrailingCommaOnFunctions();

+// phpcs:ignore Generic.Functions.FunctionCallArgumentSpacing.NoSpaceAfterComma
$class->a(1);

$class->a(
- 1
+ 1,
);
diff --git a/tests/fixed/arrow-functions-format.php b/tests/fixed/arrow-functions-format.php
index a45074f..4da39b8 100644
--- a/tests/fixed/arrow-functions-format.php
+++ b/tests/fixed/arrow-functions-format.php
@@ -18,10 +18,10 @@ $returningObject = static fn () => new stdClass();

$multiLineArrowFunctions = Collection::from([1, 2])
->map(
- static fn (int $v): int => $v * 2
+ static fn (int $v): int => $v * 2,
)
->reduce(
- static fn (int $tmp, int $v): int => $tmp + $v
+ static fn (int $tmp, int $v): int => $tmp + $v,
);

$thisIsNotAnArrowFunction = [$this->fn => 'value'];
diff --git a/tests/fixed/namespaces-spacing.php b/tests/fixed/namespaces-spacing.php
index d42bbfe..36cbae2 100644
--- a/tests/fixed/namespaces-spacing.php
+++ b/tests/fixed/namespaces-spacing.php
@@ -16,5 +16,5 @@ use const DATE_RFC3339;
strrev(
(new DateTimeImmutable('@' . time(), new DateTimeZone('UTC')))
->sub(new DateInterval('P1D'))
- ->format(DATE_RFC3339)
+ ->format(DATE_RFC3339),
);
diff --git a/tests/input/TrailingCommaOnFunctions.php b/tests/input/TrailingCommaOnFunctions.php
index f3ffa91..8adcedf 100644
--- a/tests/input/TrailingCommaOnFunctions.php
+++ b/tests/input/TrailingCommaOnFunctions.php
@@ -37,7 +37,8 @@ class TrailingCommaOnFunctions

$class = new TrailingCommaOnFunctions();

-$class->a(1);
+// phpcs:ignore Generic.Functions.FunctionCallArgumentSpacing.NoSpaceAfterComma
+$class->a(1,);

$class->a(
1
diff --git a/tests/input/arrow-functions-format.php b/tests/input/arrow-functions-format.php
index 8a358e8..d3903ff 100644
--- a/tests/input/arrow-functions-format.php
+++ b/tests/input/arrow-functions-format.php
@@ -18,10 +18,10 @@ $returningObject = static fn () => new stdClass();

$multiLineArrowFunctions = Collection::from([1, 2])
->map(
- static fn (int $v): int => $v * 2
+ static fn (int $v): int => $v * 2,
)
->reduce(
- static fn (int $tmp, int $v): int => $tmp + $v
+ static fn (int $tmp, int $v): int => $tmp + $v,
);

$thisIsNotAnArrowFunction = [$this->fn => 'value'];
diff --git a/tests/input/namespaces-spacing.php b/tests/input/namespaces-spacing.php
index e1ab639..e7be018 100644
--- a/tests/input/namespaces-spacing.php
+++ b/tests/input/namespaces-spacing.php
@@ -11,5 +11,5 @@ use const DATE_RFC3339;
strrev(
(new DateTimeImmutable('@' . time(), new DateTimeZone('UTC')))
->sub(new DateInterval('P1D'))
- ->format(DATE_RFC3339)
+ ->format(DATE_RFC3339),
);
98 changes: 90 additions & 8 deletions tests/php74-compatibility.patch
@@ -1,5 +1,5 @@
diff --git a/tests/expected_report.txt b/tests/expected_report.txt
index c644926..7d122d2 100644
index 1d5a7d3..91e97e7 100644
--- a/tests/expected_report.txt
+++ b/tests/expected_report.txt
@@ -15,7 +15,7 @@ tests/input/ControlStructures.php 28 0
Expand All @@ -23,14 +23,11 @@ index c644926..7d122d2 100644
tests/input/null_coalesce_operator.php 3 0
tests/input/optimized-functions.php 1 0
tests/input/PropertyDeclaration.php 6 0
@@ -35,20 +35,20 @@ tests/input/semicolon_spacing.php 3 0
tests/input/single-line-array-spacing.php 5 0
tests/input/spread-operator.php 6 0
tests/input/static-closures.php 1 0
tests/input/strings.php 1 0
@@ -39,16 +39,17 @@ tests/input/strings.php 1 0
tests/input/superfluous-naming.php 11 0
tests/input/test-case.php 8 0
tests/input/trailing_comma_on_array.php 1 0
+tests/input/TrailingCommaOnFunctions.php 2 0
tests/input/traits-uses.php 11 0
-tests/input/type-hints.php 7 0
+tests/input/type-hints.php 8 0
Expand All @@ -40,13 +37,45 @@ index c644926..7d122d2 100644
tests/input/UselessConditions.php 20 0
----------------------------------------------------------------------
-A TOTAL OF 381 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
+A TOTAL OF 390 ERRORS AND 0 WARNINGS WERE FOUND IN 42 FILES
+A TOTAL OF 392 ERRORS AND 0 WARNINGS WERE FOUND IN 43 FILES
----------------------------------------------------------------------
-PHPCBF CAN FIX 315 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 324 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX 326 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


diff --git a/tests/fixed/TrailingCommaOnFunctions.php b/tests/fixed/TrailingCommaOnFunctions.php
index f3ffa91..67173b3 100644
--- a/tests/fixed/TrailingCommaOnFunctions.php
+++ b/tests/fixed/TrailingCommaOnFunctions.php
@@ -37,8 +37,9 @@ class TrailingCommaOnFunctions

$class = new TrailingCommaOnFunctions();

+// phpcs:ignore Generic.Functions.FunctionCallArgumentSpacing.NoSpaceAfterComma
$class->a(1);

$class->a(
- 1
+ 1,
);
diff --git a/tests/fixed/arrow-functions-format.php b/tests/fixed/arrow-functions-format.php
index a45074f..4da39b8 100644
--- a/tests/fixed/arrow-functions-format.php
+++ b/tests/fixed/arrow-functions-format.php
@@ -18,10 +18,10 @@ $returningObject = static fn () => new stdClass();

$multiLineArrowFunctions = Collection::from([1, 2])
->map(
- static fn (int $v): int => $v * 2
+ static fn (int $v): int => $v * 2,
)
->reduce(
- static fn (int $tmp, int $v): int => $tmp + $v
+ static fn (int $tmp, int $v): int => $tmp + $v,
);

$thisIsNotAnArrowFunction = [$this->fn => 'value'];
diff --git a/tests/fixed/example-class.php b/tests/fixed/example-class.php
index 998e51d..7866379 100644
--- a/tests/fixed/example-class.php
Expand All @@ -69,6 +98,17 @@ index 998e51d..7866379 100644

/** @var ControlStructureSniff|int|string|null */
private $baxBax;
diff --git a/tests/fixed/namespaces-spacing.php b/tests/fixed/namespaces-spacing.php
index d42bbfe..36cbae2 100644
--- a/tests/fixed/namespaces-spacing.php
+++ b/tests/fixed/namespaces-spacing.php
@@ -16,5 +16,5 @@ use const DATE_RFC3339;
strrev(
(new DateTimeImmutable('@' . time(), new DateTimeZone('UTC')))
->sub(new DateInterval('P1D'))
- ->format(DATE_RFC3339)
+ ->format(DATE_RFC3339),
);
diff --git a/tests/fixed/new_with_parentheses.php b/tests/fixed/new_with_parentheses.php
index 6e81bbe..47a06ec 100644
--- a/tests/fixed/new_with_parentheses.php
Expand Down Expand Up @@ -129,3 +169,45 @@ index 10e6f34..bfa6d4f 100644

/**
* @param Iterator $iterator
diff --git a/tests/input/TrailingCommaOnFunctions.php b/tests/input/TrailingCommaOnFunctions.php
index f3ffa91..8adcedf 100644
--- a/tests/input/TrailingCommaOnFunctions.php
+++ b/tests/input/TrailingCommaOnFunctions.php
@@ -37,7 +37,8 @@ class TrailingCommaOnFunctions

$class = new TrailingCommaOnFunctions();

-$class->a(1);
+// phpcs:ignore Generic.Functions.FunctionCallArgumentSpacing.NoSpaceAfterComma
+$class->a(1,);

$class->a(
1
diff --git a/tests/input/arrow-functions-format.php b/tests/input/arrow-functions-format.php
index 8a358e8..d3903ff 100644
--- a/tests/input/arrow-functions-format.php
+++ b/tests/input/arrow-functions-format.php
@@ -18,10 +18,10 @@ $returningObject = static fn () => new stdClass();

$multiLineArrowFunctions = Collection::from([1, 2])
->map(
- static fn (int $v): int => $v * 2
+ static fn (int $v): int => $v * 2,
)
->reduce(
- static fn (int $tmp, int $v): int => $tmp + $v
+ static fn (int $tmp, int $v): int => $tmp + $v,
);

$thisIsNotAnArrowFunction = [$this->fn => 'value'];
diff --git a/tests/input/namespaces-spacing.php b/tests/input/namespaces-spacing.php
index e1ab639..e7be018 100644
--- a/tests/input/namespaces-spacing.php
+++ b/tests/input/namespaces-spacing.php
@@ -11,5 +11,5 @@ use const DATE_RFC3339;
strrev(
(new DateTimeImmutable('@' . time(), new DateTimeZone('UTC')))
->sub(new DateInterval('P1D'))
- ->format(DATE_RFC3339)
+ ->format(DATE_RFC3339),
);