Skip to content

Commit

Permalink
Use static closure if possible (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
asispts committed Nov 3, 2023
1 parent c7f260e commit adc9b6f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions ptscs/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
<rule ref="SlevomatCodingStandard.Variables.UnusedVariable" />
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure" />
<rule ref="SlevomatCodingStandard.Functions.DisallowEmptyFunction" />
<rule ref="SlevomatCodingStandard.Functions.StaticClosure" />
<rule ref="SlevomatCodingStandard.Commenting.DisallowOneLinePropertyDocComment" />
<rule ref="SlevomatCodingStandard.Commenting.EmptyComment" />

Expand Down
23 changes: 23 additions & 0 deletions tests/Sniffs/Slevomat/Functions/StaticClosureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Ptscs\Tests\Sniffs\Slevomat\Functions;

use Iterator;
use Ptscs\Tests\SniffTestCase;
use Ptscs\Tests\Utils\ErrorData;

final class StaticClosureTest extends SniffTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->appendExclude('Squiz.Classes.ClassFileName.NoMatch');
}

public static function provideTestData(): Iterator
{
yield[
[new ErrorData(9, 'SlevomatCodingStandard.Functions.StaticClosure.ClosureNotStatic')],
];
}
}
13 changes: 13 additions & 0 deletions tests/Sniffs/Slevomat/Functions/_data/StaticClosure.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace App;

final class Foobar
{
public function callback()
{
return static function () {
return 'Test';
};
}
}
13 changes: 13 additions & 0 deletions tests/Sniffs/Slevomat/Functions/_data/StaticClosure.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace App;

final class Foobar
{
public function callback()
{
return function () {
return 'Test';
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ final class Foobar
{
public function __invoke(): callable
{
return function () {
return static function () {
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ final class Foobar
{
public function __invoke(): callable
{
return function () use ($name) {
return static function () use ($name) {
};
}
}
2 changes: 1 addition & 1 deletion tests/Sniffs/Unsolved/_data/ScopeIndent.php.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

$this->callback(function () {
$this->callback(static function () {
if (true) {
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Sniffs/Unsolved/_data/ScopeIndent.php.inc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

$this->callback(function () {
$this->callback(static function () {
if (true) {
}

Expand Down

0 comments on commit adc9b6f

Please sign in to comment.