|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace PhpCsFixerCustomFixers\Analyzer; |
| 6 | + |
| 7 | +use PhpCsFixer\Tokenizer\CT; |
| 8 | +use PhpCsFixer\Tokenizer\Tokens; |
| 9 | +use PhpCsFixerCustomFixers\Analyzer\Analysis\ArrayElementAnalysis; |
| 10 | + |
| 11 | +/** |
| 12 | + * @internal |
| 13 | + */ |
| 14 | +final class ArrayAnalyzer |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @return ArrayElementAnalysis[] |
| 18 | + */ |
| 19 | + public function getElements(Tokens $tokens, int $index): array |
| 20 | + { |
| 21 | + if ($tokens[$index]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) { |
| 22 | + /** @var int $arrayContentStartIndex */ |
| 23 | + $arrayContentStartIndex = $tokens->getNextMeaningfulToken($index); |
| 24 | + |
| 25 | + /** @var int $arrayContentEndIndex */ |
| 26 | + $arrayContentEndIndex = $tokens->getPrevMeaningfulToken($tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index)); |
| 27 | + |
| 28 | + return $this->getElementsForArrayContent($tokens, $arrayContentStartIndex, $arrayContentEndIndex); |
| 29 | + } |
| 30 | + |
| 31 | + if ($tokens[$index]->isGivenKind(T_ARRAY)) { |
| 32 | + /** @var int $arrayOpenBraceIndex */ |
| 33 | + $arrayOpenBraceIndex = $tokens->getNextTokenOfKind($index, ['(']); |
| 34 | + |
| 35 | + /** @var int $arrayContentStartIndex */ |
| 36 | + $arrayContentStartIndex = $tokens->getNextMeaningfulToken($arrayOpenBraceIndex); |
| 37 | + |
| 38 | + /** @var int $arrayContentEndIndex */ |
| 39 | + $arrayContentEndIndex = $tokens->getPrevMeaningfulToken($tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $arrayOpenBraceIndex)); |
| 40 | + |
| 41 | + return $this->getElementsForArrayContent($tokens, $arrayContentStartIndex, $arrayContentEndIndex); |
| 42 | + } |
| 43 | + |
| 44 | + throw new \InvalidArgumentException(\sprintf('Index %d is not an array.', $index)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return ArrayElementAnalysis[] |
| 49 | + */ |
| 50 | + private function getElementsForArrayContent(Tokens $tokens, int $startIndex, int $endIndex): array |
| 51 | + { |
| 52 | + $elements = []; |
| 53 | + |
| 54 | + $index = $startIndex; |
| 55 | + while ($endIndex >= $index = $this->nextCandidateIndex($tokens, $index)) { |
| 56 | + if (!$tokens[$index]->equals(',')) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + /** @var int $elementEndIndex */ |
| 61 | + $elementEndIndex = $tokens->getPrevMeaningfulToken($index); |
| 62 | + |
| 63 | + $elements[] = $this->createArrayElementAnalysis($tokens, $startIndex, $elementEndIndex); |
| 64 | + |
| 65 | + /** @var int $startIndex */ |
| 66 | + $startIndex = $tokens->getNextMeaningfulToken($index); |
| 67 | + } |
| 68 | + |
| 69 | + if ($startIndex <= $endIndex) { |
| 70 | + $elements[] = $this->createArrayElementAnalysis($tokens, $startIndex, $endIndex); |
| 71 | + } |
| 72 | + |
| 73 | + return $elements; |
| 74 | + } |
| 75 | + |
| 76 | + private function createArrayElementAnalysis(Tokens $tokens, int $startIndex, int $endIndex): ArrayElementAnalysis |
| 77 | + { |
| 78 | + $index = $startIndex; |
| 79 | + while ($endIndex > $index = $this->nextCandidateIndex($tokens, $index)) { |
| 80 | + if (!$tokens[$index]->isGivenKind(T_DOUBLE_ARROW)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + /** @var int $keyEndIndex */ |
| 85 | + $keyEndIndex = $tokens->getPrevMeaningfulToken($index); |
| 86 | + |
| 87 | + /** @var int $valueStartIndex */ |
| 88 | + $valueStartIndex = $tokens->getNextMeaningfulToken($index); |
| 89 | + |
| 90 | + return new ArrayElementAnalysis($startIndex, $keyEndIndex, $valueStartIndex, $endIndex); |
| 91 | + } |
| 92 | + |
| 93 | + return new ArrayElementAnalysis(null, null, $startIndex, $endIndex); |
| 94 | + } |
| 95 | + |
| 96 | + private function nextCandidateIndex(Tokens $tokens, int $index): int |
| 97 | + { |
| 98 | + if ($tokens[$index]->equals('{')) { |
| 99 | + $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index); |
| 100 | + } |
| 101 | + |
| 102 | + if ($tokens[$index]->equals('(')) { |
| 103 | + $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index); |
| 104 | + } |
| 105 | + |
| 106 | + if ($tokens[$index]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) { |
| 107 | + $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index); |
| 108 | + } |
| 109 | + |
| 110 | + if ($tokens[$index]->isGivenKind(T_ARRAY)) { |
| 111 | + /** @var int $arrayOpenBraceIndex */ |
| 112 | + $arrayOpenBraceIndex = $tokens->getNextTokenOfKind($index, ['(']); |
| 113 | + |
| 114 | + $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $arrayOpenBraceIndex); |
| 115 | + } |
| 116 | + |
| 117 | + return $index + 1; |
| 118 | + } |
| 119 | +} |
0 commit comments