Skip to content

Commit

Permalink
RequireArrowFunctionSniff requires PHP 7.4
Browse files Browse the repository at this point in the history
Fix #924
  • Loading branch information
carusogabriel authored and kukulich committed Mar 22, 2020
1 parent 81ad6e7 commit e27ab09
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ Requires arrow functions.
Sniff provides the following settings:

* `allowNested` (defaults to `true`)
* `enable`: either to enable or no this sniff. By default, it is enabled for PHP versions 7.4 or higher.

#### SlevomatCodingStandard.Functions.TrailingCommaInCall 🔧

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use SlevomatCodingStandard\Helpers\ScopeHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use function count;
use const T_BITWISE_AND;
Expand All @@ -24,6 +25,9 @@ class RequireArrowFunctionSniff implements Sniff
/** @var bool */
public $allowNested = true;

/** @var bool|null */
public $enable = null;

/**
* @return array<int, (int|string)>
*/
Expand All @@ -41,6 +45,12 @@ public function register(): array
*/
public function process(File $phpcsFile, $closurePointer): void
{
$this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 70400);

if (!$this->enable) {
return;
}

$tokens = $phpcsFile->getTokens();

$returnPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$closurePointer]['scope_opener'] + 1);
Expand Down
15 changes: 15 additions & 0 deletions tests/Sniffs/Functions/RequireArrowFunctionSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function testDisallowNestedNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionDisallowNestedNoErrors.php', [
'allowNested' => false,
'enable' => true,
]);
self::assertNoSniffErrorInFile($report);
}
Expand All @@ -19,6 +20,7 @@ public function testDisallowNestedErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionDisallowNestedErrors.php', [
'allowNested' => false,
'enable' => true,
]);

self::assertSame(4, $report->getErrorCount());
Expand All @@ -35,6 +37,7 @@ public function testAllowNestedNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionAllowNestedNoErrors.php', [
'allowNested' => true,
'enable' => true,
]);
self::assertNoSniffErrorInFile($report);
}
Expand All @@ -43,6 +46,7 @@ public function testAllowNestedErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionAllowNestedErrors.php', [
'allowNested' => true,
'enable' => true,
]);

self::assertSame(3, $report->getErrorCount());
Expand All @@ -54,4 +58,15 @@ public function testAllowNestedErrors(): void
self::assertAllFixedInFile($report);
}

public function testShouldNotReportIfSniffIsDisabled(): void
{
$report = self::checkFile(__DIR__ . '/data/requireArrowFunctionAllowNestedErrors.php', [
'allowNested' => true,
'enable' => false,
]);

self::assertSame(0, $report->getErrorCount());
self::assertNoSniffErrorInFile($report);
}

}

0 comments on commit e27ab09

Please sign in to comment.