PHP 8.2.31
Preview: ArrayKeyValue.php Size: 3.60 KB
/opt/cpanel/ea-wappspector/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/ArrayKeyValue.php

<?php declare(strict_types = 1);

namespace SlevomatCodingStandard\Helpers;

use PHP_CodeSniffer\Files\File;
use function array_reverse;
use function current;
use function in_array;
use function ltrim;
use function rtrim;
use function strpos;
use function trim;
use const T_CLOSURE;
use const T_COMMA;
use const T_DOUBLE_ARROW;
use const T_ELLIPSIS;
use const T_WHITESPACE;

/**
 * @internal
 */
class ArrayKeyValue
{

	private int $pointerStart;

	private int $pointerEnd;

	private ?string $indent = null;

	private ?string $key = null;

	private ?int $pointerArrow = null;

	private ?int $pointerComma = null;

	private bool $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::ARRAY_TOKEN_CODES, true)) {
				$i = ArrayHelper::openClosePointers($token)[1];
				continue;
			}

			if ($token['code'] === T_DOUBLE_ARROW) {
				if (current(array_reverse($token['conditions'])) === T_CLOSURE) {
					continue;
				}

				$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::INLINE_COMMENT_TOKEN_CODES, 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;
	}

}

Directory Contents

Dirs: 0 × Files: 41

Name Size Perms Modified Actions
1.11 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
8.67 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
10.15 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
6.25 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
3.60 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.14 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
5.00 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.18 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
3.03 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
430 B lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.52 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
10.20 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.03 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
7.12 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
502 B lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.97 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
16.55 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
7.34 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.84 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
5.80 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
893 B lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.27 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.56 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
6.39 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.32 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
15.13 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.48 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
1.20 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.01 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
485 B lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.27 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
4.97 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
12.87 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
774 B lrw-r--r-- 2025-09-13 08:53:30
Edit Download
740 B lrw-r--r-- 2025-09-13 08:53:30
Edit Download
966 B lrw-r--r-- 2025-09-13 08:53:30
Edit Download
10.67 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
2.53 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
7.82 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
5.03 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download
9.28 KB lrw-r--r-- 2025-09-13 08:53:30
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).