/** * @param callable $pattern * @param Collection $collection * @return Collection[] * @throws Exception */ protected function iterateOverCollections(callable $pattern, Collection $collection) { $result = []; $collection->rewind(); foreach ($collection as $index => $token) { $querySequence = new QuerySequence($collection, $index); $patternResult = $pattern($querySequence); if ($patternResult === null) { continue; } if (!$patternResult instanceof Collection) { throw new Exception('Invalid result from pattern callback. Expect Collection. Given:' . gettype($patternResult)); } $result[] = $patternResult; } return $result; }
/** * If $constant is the start of the constant defining $key, updat its value to $value * * @param Collection $tokens * @param Token $constant * @param string $key * @param mixed $value */ private function handleConstant(Collection $tokens, Token $constant, $key, $value) { $lineEnd = false; $found = false; $index = $constant->getIndex(); $tokens->rewind(); while ($lineEnd === false && ($constantToken = $tokens->getNext($index))) { if ($constantToken->getValue() === ';') { $lineEnd = true; } if ($constantToken->getType() === T_STRING && $constantToken->getValue() === $key) { $found = true; } elseif ($found === true) { $this->attemptUpdate($constantToken, $value); } $index++; } }
/** * @param Collection $collection * @return Collection */ protected function getInvalidTokens(Collection $collection) { $resultCollection = new Collection(); $classBody = (new PatternMatcher($collection))->apply(new ClassPattern())->getCollections(); if (empty($classBody)) { return $resultCollection; } foreach ($classBody as $body) { if ($body->count() === 0) { continue; } $resultCollection->append($body->getLast()); } foreach ($resultCollection as $index => $token) { if ($this->isValidBodyEndToken($token)) { $resultCollection->offsetUnset($index); } } $resultCollection->rewind(); return $resultCollection; }