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

Breadcrumb

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

class FileLengthSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Files\FileLengthSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of FileLengthSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Files/FileLengthSniff.php, line 15

Namespace

SlevomatCodingStandard\Sniffs\Files
View source
class FileLengthSniff implements Sniff {
    public const CODE_FILE_TOO_LONG = 'FileTooLong';
    
    /** @var int */
    public $maxLinesLength = 250;
    
    /** @var bool */
    public $includeComments = false;
    
    /** @var bool */
    public $includeWhitespace = false;
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_OPEN_TAG,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $pointer
     */
    public function process(File $phpcsFile, $pointer) : void {
        $this->maxLinesLength = SniffSettingsHelper::normalizeInteger($this->maxLinesLength);
        $flags = array_keys(array_filter([
            FunctionHelper::LINE_INCLUDE_COMMENT => $this->includeComments,
            FunctionHelper::LINE_INCLUDE_WHITESPACE => $this->includeWhitespace,
        ]));
        $flags = array_reduce($flags, static function ($carry, $flag) : int {
            return $carry | $flag;
        }, 0);
        $length = FunctionHelper::getLineCount($phpcsFile, $pointer, $flags);
        if ($length <= $this->maxLinesLength) {
            return;
        }
        $errorMessage = sprintf('Your file is too long. Currently using %d lines. Can be up to %d lines.', $length, $this->maxLinesLength);
        $phpcsFile->addError($errorMessage, $pointer, self::CODE_FILE_TOO_LONG);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FileLengthSniff::$includeComments public property @var bool
FileLengthSniff::$includeWhitespace public property @var bool
FileLengthSniff::$maxLinesLength public property @var int
FileLengthSniff::CODE_FILE_TOO_LONG public constant
FileLengthSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
FileLengthSniff::register public function * Overrides Sniff::register
RSS feed
Powered by Drupal