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

Breadcrumb

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

function FunctionHelper::getLineCount

3 calls to FunctionHelper::getLineCount()
ClassLengthSniff::process in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/ClassLengthSniff.php
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
FileLengthSniff::process in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Files/FileLengthSniff.php
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint *
FunctionHelper::getFunctionLengthInLines in vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/FunctionHelper.php
*

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/FunctionHelper.php, line 480

Class

FunctionHelper
@internal

Namespace

SlevomatCodingStandard\Helpers

Code

public static function getLineCount(File $file, int $tokenPosition, int $flags = 0) : int {
    $includeWhitespace = ($flags & self::LINE_INCLUDE_WHITESPACE) === self::LINE_INCLUDE_WHITESPACE;
    $includeComments = ($flags & self::LINE_INCLUDE_COMMENT) === self::LINE_INCLUDE_COMMENT;
    $tokens = $file->getTokens();
    $token = $tokens[$tokenPosition];
    $tokenOpenerPosition = $token['scope_opener'] ?? $tokenPosition;
    $tokenCloserPosition = $token['scope_closer'] ?? $file->numTokens - 1;
    $tokenOpenerLine = $tokens[$tokenOpenerPosition]['line'];
    $tokenCloserLine = $tokens[$tokenCloserPosition]['line'];
    $lineCount = 0;
    $lastCommentLine = null;
    $previousIncludedPosition = null;
    for ($position = $tokenOpenerPosition; $position <= $tokenCloserPosition - 1; $position++) {
        $token = $tokens[$position];
        if ($includeComments === false) {
            if (in_array($token['code'], Tokens::$commentTokens, true)) {
                if ($previousIncludedPosition !== null && substr_count($token['content'], $file->eolChar) > 0 && $token['line'] === $tokens[$previousIncludedPosition]['line']) {
                    // Comment with linebreak starting on same line as included Token
                    $lineCount++;
                }
                // Don't include comment
                $lastCommentLine = $token['line'];
                continue;
            }
            if ($previousIncludedPosition !== null && $token['code'] === T_WHITESPACE && $token['line'] === $lastCommentLine && $token['line'] !== $tokens[$previousIncludedPosition]['line']) {
                // Whitespace after block comment... still on comment line...
                // Ignore along with the comment
                continue;
            }
        }
        if ($token['code'] === T_WHITESPACE) {
            $nextNonWhitespacePosition = $file->findNext(T_WHITESPACE, $position + 1, $tokenCloserPosition + 1, true);
            if ($includeWhitespace === false && $token['column'] === 1 && $nextNonWhitespacePosition !== false && $tokens[$nextNonWhitespacePosition]['line'] !== $token['line']) {
                // This line is nothing but whitepace
                $position = $nextNonWhitespacePosition - 1;
                continue;
            }
            if ($previousIncludedPosition === $tokenOpenerPosition && $token['line'] === $tokenOpenerLine) {
                // Don't linclude line break after opening "{"
                // Unless there was code or an (included) comment following the "{"
                continue;
            }
        }
        if ($token['code'] !== T_WHITESPACE) {
            $previousIncludedPosition = $position;
        }
        $newLineFoundCount = substr_count($token['content'], $file->eolChar);
        $lineCount += $newLineFoundCount;
    }
    if ($tokens[$previousIncludedPosition]['line'] === $tokenCloserLine) {
        // There is code or comment on the closing "}" line...
        $lineCount++;
    }
    return $lineCount;
}

API Navigation

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