Skip to content

Commit

Permalink
feat: add rules for trailing commas in function calls and closure use
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Jul 8, 2022
1 parent cd23eb3 commit 33cca2c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
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
1 change: 1 addition & 0 deletions phpcs.xml.dist
Expand Up @@ -12,6 +12,7 @@

<!-- Show progress of the run -->
<arg value="p"/>
<arg value="s"/>

<rule ref="Doctrine"/>

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
);
34 changes: 34 additions & 0 deletions tests/php73-compatibility.patch
@@ -0,0 +1,34 @@
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/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'];

0 comments on commit 33cca2c

Please sign in to comment.