Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. RequireNumericLiteralSeparatorSniff.php

class RequireNumericLiteralSeparatorSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Numbers\RequireNumericLiteralSeparatorSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of RequireNumericLiteralSeparatorSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Numbers/RequireNumericLiteralSeparatorSniff.php, line 13

Namespace

SlevomatCodingStandard\Sniffs\Numbers
View source
class RequireNumericLiteralSeparatorSniff implements Sniff {
    public const CODE_REQUIRED_NUMERIC_LITERAL_SEPARATOR = 'RequiredNumericLiteralSeparator';
    
    /** @var bool|null */
    public $enable = null;
    
    /** @var int */
    public $minDigitsBeforeDecimalPoint = 4;
    
    /** @var int */
    public $minDigitsAfterDecimalPoint = 4;
    
    /** @var bool */
    public $ignoreOctalNumbers = true;
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_LNUMBER,
            T_DNUMBER,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $numberPointer
     */
    public function process(File $phpcsFile, $numberPointer) : void {
        $this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 70400);
        $this->minDigitsBeforeDecimalPoint = SniffSettingsHelper::normalizeInteger($this->minDigitsBeforeDecimalPoint);
        $this->minDigitsAfterDecimalPoint = SniffSettingsHelper::normalizeInteger($this->minDigitsAfterDecimalPoint);
        if (!$this->enable) {
            return;
        }
        $tokens = $phpcsFile->getTokens();
        $number = $tokens[$numberPointer]['content'];
        if (strpos($tokens[$numberPointer]['content'], '_') !== false) {
            return;
        }
        if ($this->ignoreOctalNumbers && preg_match('~^0[0-7]+$~', $number) === 1) {
            return;
        }
        $regexp = '~(?:^\\d{' . $this->minDigitsBeforeDecimalPoint . '}|\\.\\d{' . $this->minDigitsAfterDecimalPoint . '})~';
        if (preg_match($regexp, $number) === 0) {
            return;
        }
        $phpcsFile->addError('Use of numeric literal separator is required.', $numberPointer, self::CODE_REQUIRED_NUMERIC_LITERAL_SEPARATOR);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
RequireNumericLiteralSeparatorSniff::$enable public property @var bool|null
RequireNumericLiteralSeparatorSniff::$ignoreOctalNumbers public property @var bool
RequireNumericLiteralSeparatorSniff::$minDigitsAfterDecimalPoint public property @var int
RequireNumericLiteralSeparatorSniff::$minDigitsBeforeDecimalPoint public property @var int
RequireNumericLiteralSeparatorSniff::CODE_REQUIRED_NUMERIC_LITERAL_SEPARATOR public constant
RequireNumericLiteralSeparatorSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
RequireNumericLiteralSeparatorSniff::register public function * Overrides Sniff::register
RSS feed
Powered by Drupal