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

Breadcrumb

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

class FunctionLengthSniff

Hierarchy

  • class \SlevomatCodingStandard\Sniffs\Functions\FunctionLengthSniff implements \PHP_CodeSniffer\Sniffs\Sniff

Expanded class hierarchy of FunctionLengthSniff

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/FunctionLengthSniff.php, line 15

Namespace

SlevomatCodingStandard\Sniffs\Functions
View source
class FunctionLengthSniff implements Sniff {
    public const CODE_FUNCTION_LENGTH = 'FunctionLength';
    
    /** @var int */
    public $maxLinesLength = 20;
    
    /** @var bool */
    public $includeComments = false;
    
    /** @var bool */
    public $includeWhitespace = false;
    
    /**
     * @return array<int, (int|string)>
     */
    public function register() : array {
        return [
            T_FUNCTION,
        ];
    }
    
    /**
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
     * @param int $functionPointer
     */
    public function process(File $file, $functionPointer) : 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::getFunctionLengthInLines($file, $functionPointer, $flags);
        if ($length <= $this->maxLinesLength) {
            return;
        }
        $errorMessage = sprintf('Your function is too long. Currently using %d lines. Can be up to %d lines.', $length, $this->maxLinesLength);
        $file->addError($errorMessage, $functionPointer, self::CODE_FUNCTION_LENGTH);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
FunctionLengthSniff::$includeComments public property @var bool
FunctionLengthSniff::$includeWhitespace public property @var bool
FunctionLengthSniff::$maxLinesLength public property @var int
FunctionLengthSniff::CODE_FUNCTION_LENGTH public constant
FunctionLengthSniff::process public function * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
Overrides Sniff::process
FunctionLengthSniff::register public function * Overrides Sniff::register

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal