/**
  * If $constantToken is the token holding the value of the constant, update the value to $value
  *
  * @param Token $constantToken
  * @param mixed $value
  */
 private function attemptUpdate(Token $constantToken, $value)
 {
     $type = $constantToken->getType();
     if ($type === T_CONSTANT_ENCAPSED_STRING) {
         $constantToken->setValue(sprintf("'%s'", $value));
     } elseif ($type === T_LNUMBER || $type === T_ARRAY) {
         $constantToken->setValue($value);
     }
 }
 /**
  * @param Token $token
  * @return bool
  */
 protected function isValidBodyEndToken(Token $token)
 {
     if (T_WHITESPACE !== $token->getType()) {
         return false;
     }
     if ($this->linesNum === 0 and strpos($token->getValue(), "\n") === false) {
         return true;
     }
     return $this->linesNum === count(explode("\n", $token->getValue())) - 2;
 }
 /**
  * @param Token $token
  * @param string $emptyLines
  * @return string
  */
 protected function getTokenNewValue(Token $token, $emptyLines)
 {
     if ($token->getType() !== T_WHITESPACE) {
         return $token->getValue() . $emptyLines;
     }
     $lines = explode("\n", $token->getValue());
     $lineStart = current($lines);
     $lineEnd = end($lines);
     return $lineStart . $emptyLines . $lineEnd;
 }
Exemple #4
0
 /**
  * Convert php code to array of tokens
  *
  * @param string $code
  * @return Token[]
  * @throws Exception
  */
 public static function getTokensFromString($code)
 {
     $tokens = token_get_all($code);
     foreach ($tokens as $index => $tokenData) {
         if (!is_array($tokenData)) {
             $previousIndex = $index - 1;
             /** @var Token $previousToken */
             $previousToken = $tokens[$previousIndex];
             $line = $previousToken->getLine() + substr_count($previousToken->getValue(), "\n");
             $tokenData = [Token::INVALID_TYPE, $tokenData, $line];
         }
         $token = new Token($tokenData);
         $token->setIndex($index);
         $tokens[$index] = $token;
     }
     return $tokens;
 }
 /**
  * @param Token $tokenStart
  * @param Token $tokenEnd
  * @return Collection
  */
 public function extractByTokens(Token $tokenStart, Token $tokenEnd)
 {
     $collection = new Collection();
     $startIndex = $tokenStart->getIndex();
     $endIndex = $tokenEnd->getIndex();
     foreach ($this->getItems() as $token) {
         if ($token->getIndex() >= $startIndex and $token->getIndex() <= $endIndex) {
             $collection->append($token);
         }
     }
     return $collection;
 }
 /**
  * Move to specific position
  *
  * @param Token $token
  * @return Token|null
  */
 public function moveToToken(Token $token)
 {
     if (!$token->isValid()) {
         $this->setValid(false);
         return new Token();
     }
     $tokenIndex = $token->getIndex();
     foreach ($this->collection as $index => $collectionToken) {
         if ($collectionToken->getIndex() == $tokenIndex) {
             $this->setPosition($index);
             return $collectionToken;
         }
     }
     $this->setValid(false);
     return new Token();
 }