private function detectIndent(Tokens $tokens, $index) { static $goBackTokens = array(T_ABSTRACT, T_FINAL, T_PUBLIC, T_PROTECTED, T_PRIVATE, T_STATIC); $token = $tokens[$index]; if ($token->isGivenKind($goBackTokens) || $token->isClassy() || $token->isGivenKind(T_FUNCTION)) { $prevIndex = null; $prevToken = $tokens->getPrevNonWhitespace($index, array(), $prevIndex); if ($prevToken->isGivenKind($goBackTokens)) { return $this->detectIndent($tokens, $prevIndex); } } $prevIndex = $index - 1; $prevToken = $tokens[$prevIndex]; if ($prevToken->equals('}')) { return $this->detectIndent($tokens, $prevIndex); } // if can not detect indent: if (!$prevToken->isWhitespace()) { return ''; } $explodedContent = explode("\n", $prevToken->content); // proper decect indent for code: ` } else {` if (1 === count($explodedContent)) { if ($tokens[$index - 2]->equals('}')) { return $this->detectIndent($tokens, $index - 2); } } return end($explodedContent); }