Пример #1
0
 public static function fromBaseToken(BaseToken $token)
 {
     $extended = new self($token->getPrototype());
     if ($token->isChanged()) {
         $extended->setContent('__CHANGE_HOLDER__' . $token->getContent());
         $extended->setContent($token->getContent());
     }
     return $extended;
 }
Пример #2
0
 public static function calculateTrailingWhitespaceIndent(Token $token)
 {
     if (!$token->isWhitespace()) {
         throw new \InvalidArgumentException('The given token must be whitespace.');
     }
     return ltrim(strrchr(str_replace(array("\r\n", "\r"), "\n", $token->getContent()), 10), "\n");
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public function process(Tokens $tokens, Token $token, $index)
 {
     if ($token->isGivenKind(T_USE) && $this->isUseForLambda($tokens, $index)) {
         $token->override(array(CT_USE_LAMBDA, $token->getContent()));
     }
     if (!$token->isClassy()) {
         return;
     }
     $prevTokenIndex = $tokens->getPrevMeaningfulToken($index);
     $prevToken = $prevTokenIndex === null ? null : $tokens[$prevTokenIndex];
     if ($prevToken->isGivenKind(T_DOUBLE_COLON)) {
         return;
     }
     // Skip whole class braces content.
     // That way we can skip whole tokens in class declaration, therefore skip `T_USE` for traits.
     $index = $tokens->getNextTokenOfKind($index, array('{'));
     $innerLimit = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
     while ($index < $innerLimit) {
         $token = $tokens[++$index];
         if (!$token->isGivenKind(T_USE)) {
             continue;
         }
         if ($this->isUseForLambda($tokens, $index)) {
             $token->override(array(CT_USE_LAMBDA, $token->getContent()));
         } else {
             $token->override(array(CT_USE_TRAIT, $token->getContent()));
         }
     }
 }
 private function fixWhitespace(Token $token)
 {
     $content = $token->getContent();
     if (substr_count($content, "\n") > 1) {
         $lines = Utils::splitLines($content);
         $token->setContent("\n" . end($lines));
     }
 }
 /**
  * Cleanup a whitespace token.
  *
  * @param Token $token
  */
 private function fixWhitespace(Token $token)
 {
     $content = $token->getContent();
     // if there is more than one new line in the whitespace, then we need to fix it
     if (substr_count($content, "\n") > 1) {
         // the final bit of the whitespace must be the next statement's indentation
         $lines = Utils::splitLines($content);
         $token->setContent("\n" . end($lines));
     }
 }
Пример #6
0
 /**
  * {@inheritdoc}
  */
 public function process(Tokens $tokens, Token $token, $index)
 {
     if (!$token->isGivenKind(array(T_CONST, T_FUNCTION))) {
         return;
     }
     $prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
     if ($prevToken->isGivenKind(T_USE)) {
         $token->override(array($token->isGivenKind(T_FUNCTION) ? CT_FUNCTION_IMPORT : CT_CONST_IMPORT, $token->getContent()));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function process(Tokens $tokens, Token $token, $index)
 {
     if (!$token->isGivenKind(T_NAMESPACE)) {
         return;
     }
     $nextIndex = $tokens->getNextMeaningfulToken($index);
     $nextToken = $tokens[$nextIndex];
     if ($nextToken->equals(array(T_NS_SEPARATOR))) {
         $token->override(array(CT_NAMESPACE_OPERATOR, $token->getContent()));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function process(Tokens $tokens, Token $token, $index)
 {
     if (!$token->isGivenKind(T_CLASS)) {
         return;
     }
     $prevIndex = $tokens->getPrevMeaningfulToken($index);
     $prevToken = $tokens[$prevIndex];
     if ($prevToken->isGivenKind(T_DOUBLE_COLON)) {
         $token->override(array(CT_CLASS_CONSTANT, $token->getContent()));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function process(Tokens $tokens, Token $token, $index)
 {
     if (!$token->isGivenKind(T_ARRAY)) {
         return;
     }
     $nextIndex = $tokens->getNextMeaningfulToken($index);
     $nextToken = $tokens[$nextIndex];
     if (!$nextToken->equals('(')) {
         $token->override(array(CT_ARRAY_TYPEHINT, $token->getContent()));
     }
 }
Пример #10
0
 private function isValidList(Tokens $tokens, Token $docsToken, $listIndex)
 {
     $endIndex = $tokens->getNextTokenOfKind($listIndex, array(')'));
     $docsContent = $docsToken->getContent();
     for ($index = $listIndex + 1; $index < $endIndex; ++$index) {
         $token = $tokens[$index];
         if ($token->isGivenKind(T_VARIABLE) && false !== strpos($docsContent, $token->getContent())) {
             return true;
         }
     }
     return false;
 }
 /**
  * {@inheritdoc}
  */
 public function process(Tokens $tokens, Token $token, $index)
 {
     if (!$token->isComment()) {
         return;
     }
     $content = $token->getContent();
     $trimmedContent = rtrim($content);
     // nothing trimmed, nothing to do
     if ($content === $trimmedContent) {
         return;
     }
     $whitespaces = substr($content, strlen($trimmedContent));
     $token->setContent($trimmedContent);
     if (isset($tokens[$index + 1]) && $tokens[$index + 1]->isGivenKind(T_WHITESPACE)) {
         $tokens[$index + 1]->setContent($whitespaces . $tokens[$index + 1]->getContent());
     } else {
         $tokens->insertAt($index + 1, new Token(array(T_WHITESPACE, $whitespaces)));
     }
 }
Пример #12
0
 /**
  * Register token as found.
  *
  * @param Token|array|string $token token prototype
  */
 private function registerFoundToken($token)
 {
     $tokenKind = $token instanceof Token ? $token->isArray() ? $token->getId() : $token->getContent() : (is_array($token) ? $token[0] : $token);
     $this->foundTokenKinds[$tokenKind] = true;
 }
Пример #13
0
 /**
  * Checks variable assignments for correct docblock usage.
  *
  * @param Tokens $tokens
  * @param Token  $docsToken     docs Token
  * @param int    $variableIndex index of variable Token
  *
  * @return bool
  */
 private function isValidVariable(Tokens $tokens, Token $docsToken, $variableIndex)
 {
     $nextIndex = $tokens->getNextMeaningfulToken($variableIndex);
     if (!$tokens[$nextIndex]->equals('=')) {
         return false;
     }
     return false !== strpos($docsToken->getContent(), $tokens[$variableIndex]->getContent());
 }
 private function fixWhitespace(Token $token)
 {
     if ($token->isWhitespace() && !$token->isWhitespace(" \t")) {
         $token->setContent(rtrim($token->getContent()) . ' ');
     }
 }
Пример #15
0
 /**
  * Transforms the heredoc start token to nowdoc notation.
  *
  * @param Token $token
  */
 private function convertToNowdoc(Token $token)
 {
     $token->setContent(preg_replace('/(?<=^<<<)"?(.*?)"?$/', '\'$1\'', $token->getContent()));
 }
Пример #16
0
 private function isOperator(Token $token)
 {
     static $arrayOperators = array(T_AND_EQUAL => true, T_BOOLEAN_AND => true, T_BOOLEAN_OR => true, T_CONCAT_EQUAL => true, T_DIV_EQUAL => true, T_DOUBLE_ARROW => true, T_IS_EQUAL => true, T_IS_GREATER_OR_EQUAL => true, T_IS_IDENTICAL => true, T_IS_NOT_EQUAL => true, T_IS_NOT_IDENTICAL => true, T_IS_SMALLER_OR_EQUAL => true, T_LOGICAL_AND => true, T_LOGICAL_OR => true, T_LOGICAL_XOR => true, T_MINUS_EQUAL => true, T_MOD_EQUAL => true, T_MUL_EQUAL => true, T_OR_EQUAL => true, T_PLUS_EQUAL => true, T_SL => true, T_SL_EQUAL => true, T_SR => true, T_SR_EQUAL => true, T_XOR_EQUAL => true);
     static $nonArrayOperators = array('=' => true);
     return $token->isArray() ? isset($arrayOperators[$token->getId()]) : isset($nonArrayOperators[$token->getContent()]);
 }