Skip to content

Commit

Permalink
✨ Add SlevomatCodingStandard.Functions.StaticClosure
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
dpi committed Jun 4, 2024
1 parent 8fd73ba commit 894b009
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions PreviousNextDrupal/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<!-- SlevomatCodingStandard.Functions -->
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall" />
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration" />
<rule ref="SlevomatCodingStandard.Functions.StaticClosure" />
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure" />

<!-- SlevomatCodingStandard.Namespaces -->
Expand Down
27 changes: 27 additions & 0 deletions tests/Sniffs/FunctionsStaticClosureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types = 1);

namespace PreviousNext\CodingStandard\Tests\Sniffs;

/**
* @covers \SlevomatCodingStandard\Sniffs\Functions\StaticClosureSniff
*/
final class FunctionsStaticClosureTest extends Base {

public function testNoError(): void {
$report = self::checkFile(__DIR__ . '/fixtures/FunctionsStaticClosureNoError.php');
self::assertNoSniffErrorInFile($report);
}

public function testError(): void {
$report = self::checkFile(__DIR__ . '/fixtures/FunctionsStaticClosureError.php');
self::assertSame(1, $report->getErrorCount());

Check failure on line 19 in tests/Sniffs/FunctionsStaticClosureTest.php

View workflow job for this annotation

GitHub Actions / tests (8.1, highest)

Failed asserting that 2 is identical to 1.
self::assertSniffError($report, 16, sniffName: 'SlevomatCodingStandard.Functions.StaticClosure', code: 'ClosureNotStatic');
}

protected static function getSniffName(): string {
return 'SlevomatCodingStandard.Functions.StaticClosure';
}

}
21 changes: 21 additions & 0 deletions tests/Sniffs/fixtures/FunctionsStaticClosureError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types = 1);

namespace PreviousNext\CodingStandard\Tests\Sniffs\fixtures;

/**
* The class.
*/
final class FunctionsStaticClosureError {

/**
* Foo.
*/
public function foo(): void {
$a = function (): void {
$a = 1;
};
}

}
21 changes: 21 additions & 0 deletions tests/Sniffs/fixtures/FunctionsStaticClosureNoError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types = 1);

namespace Sniffs\fixtures;

/**
* The class.
*/
final class FunctionsStaticClosureNoError {

/**
* Foo.
*/
public function foo(): void {
$a = static function (): void {
$a = 1;
};
}

}

0 comments on commit 894b009

Please sign in to comment.