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

Breadcrumb

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

class IndentationHelper

@internal

Hierarchy

  • class \SlevomatCodingStandard\Helpers\IndentationHelper

Expanded class hierarchy of IndentationHelper

19 files declare their use of IndentationHelper
AbstractLineCall.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Functions/AbstractLineCall.php
AbstractLineCondition.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/ControlStructures/AbstractLineCondition.php
AbstractMethodSignature.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Classes/AbstractMethodSignature.php
AttributeAndTargetSpacingSniff.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Attributes/AttributeAndTargetSpacingSniff.php
AttributesOrderSniff.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Attributes/AttributesOrderSniff.php

... See full list

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/IndentationHelper.php, line 21

Namespace

SlevomatCodingStandard\Helpers
View source
class IndentationHelper {
    public const DEFAULT_INDENTATION_WIDTH = 4;
    public const TAB_INDENT = "\t";
    public const SPACES_INDENT = '    ';
    public static function getIndentation(File $phpcsFile, int $pointer) : string {
        $firstPointerOnLine = TokenHelper::findFirstTokenOnLine($phpcsFile, $pointer);
        return TokenHelper::getContent($phpcsFile, $firstPointerOnLine, $pointer - 1);
    }
    public static function addIndentation(string $indentation, int $level = 1) : string {
        $whitespace = self::getOneIndentationLevel($indentation);
        return $indentation . str_repeat($whitespace, $level);
    }
    public static function getOneIndentationLevel(string $indentation) : string {
        return $indentation === '' ? self::TAB_INDENT : ($indentation[0] === self::TAB_INDENT ? self::TAB_INDENT : self::SPACES_INDENT);
    }
    
    /**
     * @param list<int> $codePointers
     */
    public static function fixIndentation(File $phpcsFile, array $codePointers, string $defaultIndentation) : string {
        $tokens = $phpcsFile->getTokens();
        $eolLength = strlen($phpcsFile->eolChar);
        $code = '';
        $inHeredoc = false;
        foreach ($codePointers as $no => $codePointer) {
            $content = $tokens[$codePointer]['content'];
            if (!$inHeredoc && ($no === 0 || substr($tokens[$codePointer - 1]['content'], -$eolLength) === $phpcsFile->eolChar)) {
                if ($content === $phpcsFile->eolChar) {
                    // Nothing
                }
                elseif ($content[0] === self::TAB_INDENT) {
                    $content = substr($content, 1);
                }
                elseif (substr($content, 0, self::DEFAULT_INDENTATION_WIDTH) === self::SPACES_INDENT) {
                    $content = substr($content, self::DEFAULT_INDENTATION_WIDTH);
                }
                else {
                    $content = $defaultIndentation . ltrim($content);
                }
            }
            if (in_array($tokens[$codePointer]['code'], [
                T_START_HEREDOC,
                T_START_NOWDOC,
            ], true)) {
                $inHeredoc = true;
            }
            elseif (in_array($tokens[$codePointer]['code'], [
                T_END_HEREDOC,
                T_END_NOWDOC,
            ], true)) {
                $inHeredoc = false;
            }
            $code .= $content;
        }
        return rtrim($code);
    }
    public static function convertTabsToSpaces(File $phpcsFile, string $code) : string {
        return preg_replace_callback('~^(\\t+)~', static function (array $matches) use ($phpcsFile) : string {
            $indentation = str_repeat(' ', $phpcsFile->config->tabWidth !== 0 ? $phpcsFile->config->tabWidth : self::DEFAULT_INDENTATION_WIDTH);
            return str_repeat($indentation, strlen($matches[1]));
        }, $code);
    }

}

Members

Title Sort descending Modifiers Object type Summary
IndentationHelper::addIndentation public static function
IndentationHelper::convertTabsToSpaces public static function
IndentationHelper::DEFAULT_INDENTATION_WIDTH public constant
IndentationHelper::fixIndentation public static function *
IndentationHelper::getIndentation public static function
IndentationHelper::getOneIndentationLevel public static function
IndentationHelper::SPACES_INDENT public constant
IndentationHelper::TAB_INDENT public constant

API Navigation

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