Skip to content

Commit

Permalink
RequireNullCoalesceEqualOperatorSniff requires PHP 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
carusogabriel authored and kukulich committed Mar 22, 2020
1 parent 725678e commit e5b070b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ Requires use of null coalesce operator when possible.

Requires use of null coalesce equal operator when possible.

This sniff provides the following setting:

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

#### SlevomatCodingStandard.ControlStructures.EarlyExit 🔧

Requires use of early exit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use SlevomatCodingStandard\Helpers\IdentificatorHelper;
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
use SlevomatCodingStandard\Helpers\TokenHelper;
use const T_COALESCE;
use const T_EQUAL;
Expand All @@ -16,6 +17,9 @@ class RequireNullCoalesceEqualOperatorSniff implements Sniff

public const CODE_REQUIRED_NULL_COALESCE_EQUAL_OPERATOR = 'RequiredNullCoalesceEqualOperator';

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

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

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

/** @var int $variableStartPointer */
$variableStartPointer = TokenHelper::findNextEffective($phpcsFile, $equalPointer + 1);
$variableEndPointer = IdentificatorHelper::findEndPointer($phpcsFile, $variableStartPointer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ class RequireNullCoalesceEqualOperatorSniffTest extends TestCase

public function testNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireNullCoalesceEqualOperatorNoErrors.php');
$report = self::checkFile(__DIR__ . '/data/requireNullCoalesceEqualOperatorNoErrors.php', [
'enable' => true,
]);
self::assertNoSniffErrorInFile($report);
}

public function testErrors(): void
{
$report = self::checkFile(
__DIR__ . '/data/requireNullCoalesceEqualOperatorErrors.php',
[],
['enable' => true],
[RequireNullCoalesceEqualOperatorSniff::CODE_REQUIRED_NULL_COALESCE_EQUAL_OPERATOR]
);

Expand All @@ -30,4 +32,16 @@ public function testErrors(): void
self::assertAllFixedInFile($report);
}

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

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

}

0 comments on commit e5b070b

Please sign in to comment.