ObjectCalisthenics\Helper\Structure\StructureMetrics::getStructureLengthInLines PHP Method

getStructureLengthInLines() public static method

public static getStructureLengthInLines ( PHP_CodeSniffer_File $phpcsFile, integer $stackPtr ) : integer
$phpcsFile PHP_CodeSniffer_File
$stackPtr integer
return integer
    public static function getStructureLengthInLines(PHP_CodeSniffer_File $phpcsFile, int $stackPtr) : int
    {
        $tokens = $phpcsFile->getTokens();
        $token = $tokens[$stackPtr];
        // Skip function without body.
        if (isset($token['scope_opener']) === false) {
            return 0;
        }
        $firstToken = $tokens[$token['scope_opener']];
        $lastToken = $tokens[$token['scope_closer']];
        return $lastToken['line'] - $firstToken['line'];
    }

Usage Example

 /**
  * @param PHP_CodeSniffer_File $phpcsFile
  * @param int                  $stackPtr
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $token = $tokens[$stackPtr];
     $length = StructureMetrics::getStructureLengthInLines($phpcsFile, $stackPtr);
     if ($length > $this->maxLength) {
         $tokenType = strtolower(substr($token['type'], 2));
         $error = sprintf('Keep your %s small (currently using %d lines, must be less or equals than %d lines)', $tokenType, $length, $this->maxLength);
         $phpcsFile->addError($error, $stackPtr, sprintf('%sTooBig', ucfirst($tokenType)));
     }
 }