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

Breadcrumb

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

class ArrayKeyValue

@internal

Hierarchy

  • class \SlevomatCodingStandard\Helpers\ArrayKeyValue

Expanded class hierarchy of ArrayKeyValue

1 file declares its use of ArrayKeyValue
AlphabeticallySortedByKeysSniff.php in vendor/slevomat/coding-standard/SlevomatCodingStandard/Sniffs/Arrays/AlphabeticallySortedByKeysSniff.php

File

vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/ArrayKeyValue.php, line 19

Namespace

SlevomatCodingStandard\Helpers
View source
class ArrayKeyValue {
    
    /** @var int */
    private $pointerStart;
    
    /** @var int */
    private $pointerEnd;
    
    /** @var ?string */
    private $indent = null;
    
    /** @var ?string */
    private $key = null;
    
    /** @var ?int */
    private $pointerArrow = null;
    
    /** @var ?int */
    private $pointerComma = null;
    
    /** @var bool */
    private $unpacking = false;
    public function __construct(File $phpcsFile, int $pointerStart, int $pointerEnd) {
        $this->pointerStart = $pointerStart;
        $this->pointerEnd = $pointerEnd;
        $this->addValues($phpcsFile);
    }
    public function getContent(File $phpcsFile, bool $normalize = false, ?string $indent = null) : string {
        if ($normalize === false) {
            return TokenHelper::getContent($phpcsFile, $this->pointerStart, $this->pointerEnd);
        }
        $content = '';
        $addCommaPtr = $this->pointerComma === null ? TokenHelper::findPreviousEffective($phpcsFile, $this->pointerEnd) : null;
        $tokens = $phpcsFile->getTokens();
        for ($pointer = $this->pointerStart; $pointer <= $this->pointerEnd; $pointer++) {
            $token = $tokens[$pointer];
            $content .= $token['content'];
            if ($pointer === $addCommaPtr) {
                $content .= ',';
            }
        }
        // Trim, but keep leading empty lines
        $content = ltrim($content, " \t");
        $content = rtrim($content);
        if ($indent !== null && strpos($content, $phpcsFile->eolChar) !== 0) {
            $content = $indent . $content;
        }
        return $content;
    }
    public function getIndent() : ?string {
        return $this->indent;
    }
    public function getKey() : ?string {
        return $this->key;
    }
    public function getPointerArrow() : ?int {
        return $this->pointerArrow;
    }
    public function getPointerComma() : ?int {
        return $this->pointerComma;
    }
    public function getPointerEnd() : int {
        return $this->pointerEnd;
    }
    public function getPointerStart() : int {
        return $this->pointerStart;
    }
    public function isUnpacking() : bool {
        return $this->unpacking;
    }
    private function addValues(File $phpcsFile) : void {
        $key = '';
        $tokens = $phpcsFile->getTokens();
        $firstNonWhitespace = null;
        for ($i = $this->pointerStart; $i <= $this->pointerEnd; $i++) {
            $token = $tokens[$i];
            if (in_array($token['code'], TokenHelper::$arrayTokenCodes, true)) {
                $i = ArrayHelper::openClosePointers($token)[1];
                continue;
            }
            if ($token['code'] === T_DOUBLE_ARROW) {
                $this->pointerArrow = $i;
                continue;
            }
            if ($token['code'] === T_COMMA) {
                $this->pointerComma = $i;
                continue;
            }
            if ($token['code'] === T_ELLIPSIS) {
                $this->unpacking = true;
                continue;
            }
            if ($this->pointerArrow !== null) {
                continue;
            }
            if ($firstNonWhitespace === null && $token['code'] !== T_WHITESPACE) {
                $firstNonWhitespace = $i;
            }
            if (in_array($token['code'], TokenHelper::$inlineCommentTokenCodes, true) === false) {
                $key .= $token['content'];
            }
        }
        $haveIndent = $firstNonWhitespace !== null && TokenHelper::findFirstNonWhitespaceOnLine($phpcsFile, $firstNonWhitespace) === $firstNonWhitespace;
        $this->indent = $haveIndent ? TokenHelper::getContent($phpcsFile, TokenHelper::findFirstTokenOnLine($phpcsFile, $firstNonWhitespace), $firstNonWhitespace - 1) : null;
        $this->key = $this->pointerArrow !== null ? trim($key) : null;
    }

}

Members

Title Sort descending Modifiers Object type Summary
ArrayKeyValue::$indent private property @var ?string
ArrayKeyValue::$key private property @var ?string
ArrayKeyValue::$pointerArrow private property @var ?int
ArrayKeyValue::$pointerComma private property @var ?int
ArrayKeyValue::$pointerEnd private property @var int
ArrayKeyValue::$pointerStart private property @var int
ArrayKeyValue::$unpacking private property @var bool
ArrayKeyValue::addValues private function
ArrayKeyValue::getContent public function
ArrayKeyValue::getIndent public function
ArrayKeyValue::getKey public function
ArrayKeyValue::getPointerArrow public function
ArrayKeyValue::getPointerComma public function
ArrayKeyValue::getPointerEnd public function
ArrayKeyValue::getPointerStart public function
ArrayKeyValue::isUnpacking public function
ArrayKeyValue::__construct public function

API Navigation

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