Skip to content

Commit

Permalink
RequireNumericLiteralSeparatorSniff: New options "minDigitsBeforeDeci…
Browse files Browse the repository at this point in the history
…malPoint" and "minDigitsAfterDecimalPoint"
  • Loading branch information
kukulich committed Mar 27, 2020
1 parent 465bf4c commit 7323c48
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ Requires use of numeric literal separators.
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.
* `minDigitsBeforeDecimalPoint`: the mininum digits before decimal point to require separator.
* `minDigitsAfterDecimalPoint`: the mininum digits after decimal point to require separator.

#### SlevomatCodingStandard.PHP.ReferenceSpacing 🔧

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class RequireNumericLiteralSeparatorSniff implements Sniff
/** @var bool|null */
public $enable = null;

/** @var int */
public $minDigitsBeforeDecimalPoint = 4;

/** @var int */
public $minDigitsAfterDecimalPoint = 4;

/**
* @return array<int, (int|string)>
*/
Expand Down Expand Up @@ -48,7 +54,8 @@ public function process(File $phpcsFile, $numberPointer): void
return;
}

if (preg_match('~(?:^\\d{4}|\.\\d{4})~', $tokens[$numberPointer]['content']) === 0) {
$regexp = '~(?:^\\d{' . SniffSettingsHelper::normalizeInteger($this->minDigitsBeforeDecimalPoint) . '}|\.\\d{' . SniffSettingsHelper::normalizeInteger($this->minDigitsAfterDecimalPoint) . '})~';
if (preg_match($regexp, $tokens[$numberPointer]['content']) === 0) {
return;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Sniffs/Numbers/RequireNumericLiteralSeparatorSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ public function testErrors(): void
self::assertSniffError($report, 7, RequireNumericLiteralSeparatorSniff::CODE_REQUIRED_NUMERIC_LITERAL_SEPARATOR);
}

public function testModifiedSettingsNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorModifiedSettingsNoErrors.php', [
'enable' => true,
'minDigitsBeforeDecimalPoint' => 7,
'minDigitsAfterDecimalPoint' => 6,
]);
self::assertNoSniffErrorInFile($report);
}

public function testModifiedSettingsErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorModifiedSettingsErrors.php', [
'enable' => true,
'minDigitsBeforeDecimalPoint' => 7,
'minDigitsAfterDecimalPoint' => 6,
]);

self::assertSame(2, $report->getErrorCount());

self::assertSniffError($report, 3, RequireNumericLiteralSeparatorSniff::CODE_REQUIRED_NUMERIC_LITERAL_SEPARATOR);
self::assertSniffError($report, 4, RequireNumericLiteralSeparatorSniff::CODE_REQUIRED_NUMERIC_LITERAL_SEPARATOR);
}

public function testShouldNotReportIfSniffIsDisabled(): void
{
$report = self::checkFile(__DIR__ . '/data/requireNumericLiteralSeparatorErrors.php', [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php // lint >= 7.4

1000000;
1.000000;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php // lint >= 7.4

100000;
1.00000;

0 comments on commit 7323c48

Please sign in to comment.