generatePartialCode() public method

Generate code from tokens between given indexes.
public generatePartialCode ( integer $start, integer $end ) : string
$start integer start index
$end integer end index
return string
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $uses = array_reverse($tokensAnalyzer->getImportUseIndexes());
     foreach ($uses as $index) {
         $endIndex = $tokens->getNextTokenOfKind($index, array(';'));
         $declarationContent = $tokens->generatePartialCode($index + 1, $endIndex - 1);
         $declarationParts = explode(',', $declarationContent);
         if (1 === count($declarationParts)) {
             continue;
         }
         $declarationContent = array();
         foreach ($declarationParts as $declarationPart) {
             $declarationContent[] = 'use ' . trim($declarationPart) . ';';
         }
         $declarationContent = implode("\n" . $this->detectIndent($tokens, $index), $declarationContent);
         for ($i = $index; $i <= $endIndex; ++$i) {
             $tokens[$i]->clear();
         }
         $declarationTokens = Tokens::fromCode('<?php ' . $declarationContent);
         $declarationTokens[0]->clear();
         $declarationTokens->clearEmptyTokens();
         $tokens->insertAt($index, $declarationTokens);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $tokensAnalyzer = new TokensAnalyzer($tokens);
     $uses = array_reverse($tokensAnalyzer->getImportUseIndexes());
     foreach ($uses as $index) {
         $endIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
         $previous = $tokens->getPrevMeaningfulToken($endIndex);
         if ($tokens[$previous]->equals('}')) {
             $start = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $previous, false);
             $declarationContent = $tokens->generatePartialCode($start + 1, $previous - 1);
             $prefix = '';
             for ($i = $index + 1; $i < $start; ++$i) {
                 $prefix .= $tokens[$i]->getContent();
             }
             $prefix = ' ' . ltrim($prefix);
         } else {
             $declarationContent = $tokens->generatePartialCode($index + 1, $endIndex - 1);
             $prefix = ' ';
         }
         $declarationParts = explode(',', $declarationContent);
         if (1 === count($declarationParts)) {
             continue;
         }
         $declarationContent = array();
         foreach ($declarationParts as $declarationPart) {
             $declarationContent[] = 'use' . $prefix . trim($declarationPart) . ';';
         }
         $declarationContent = implode("\n" . $this->detectIndent($tokens, $index), $declarationContent);
         for ($i = $index; $i < $endIndex; ++$i) {
             $tokens[$i]->clear();
         }
         if ($tokens[$endIndex]->equals(';')) {
             $tokens[$endIndex]->clear();
         }
         $declarationTokens = Tokens::fromCode('<?php ' . $declarationContent);
         $declarationTokens[0]->clear();
         $declarationTokens->clearEmptyTokens();
         $tokens->insertAt($index, $declarationTokens);
     }
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     $namespace = false;
     $namespaceIndex = 0;
     $namespaceEndIndex = 0;
     $classyName = null;
     $classyIndex = 0;
     foreach ($tokens as $index => $token) {
         if ($token->isGivenKind(T_NAMESPACE)) {
             if (false !== $namespace) {
                 return;
             }
             $namespaceIndex = $tokens->getNextMeaningfulToken($index);
             $namespaceEndIndex = $tokens->getNextTokenOfKind($index, array(';'));
             $namespace = trim($tokens->generatePartialCode($namespaceIndex, $namespaceEndIndex - 1));
         } elseif ($token->isClassy()) {
             if (null !== $classyName) {
                 return;
             }
             $classyIndex = $tokens->getNextMeaningfulToken($index);
             $classyName = $tokens[$classyIndex]->getContent();
         }
     }
     if (null === $classyName) {
         return;
     }
     if (false !== $namespace) {
         $normNamespace = str_replace('\\', '/', $namespace);
         $path = str_replace('\\', '/', $file->getRealPath());
         $dir = dirname($path);
         if (isset($this->configuration['dir'])) {
             $dir = substr($dir, strlen(realpath($this->configuration['dir'])) + 1);
             if (strlen($normNamespace) > strlen($dir)) {
                 if ('' !== $dir) {
                     $normNamespace = substr($normNamespace, -strlen($dir));
                 } else {
                     $normNamespace = '';
                 }
             }
         }
         $dir = substr($dir, -strlen($normNamespace));
         if (false === $dir) {
             $dir = '';
         }
         $filename = basename($path, '.php');
         if ($classyName !== $filename) {
             $tokens[$classyIndex]->setContent($filename);
         }
         if ($normNamespace !== $dir && strtolower($normNamespace) === strtolower($dir)) {
             for ($i = $namespaceIndex; $i <= $namespaceEndIndex; ++$i) {
                 $tokens[$i]->clear();
             }
             $namespace = substr($namespace, 0, -strlen($dir)) . str_replace('/', '\\', $dir);
             $newNamespace = Tokens::fromCode('<?php namespace ' . $namespace . ';');
             $newNamespace[0]->clear();
             $newNamespace[1]->clear();
             $newNamespace[2]->clear();
             $newNamespace->clearEmptyTokens();
             $tokens->insertAt($namespaceIndex, $newNamespace);
         }
     } else {
         $normClass = str_replace('_', '/', $classyName);
         $path = str_replace('\\', '/', $file->getRealPath());
         $filename = substr($path, -strlen($normClass) - 4, -4);
         if ($normClass !== $filename && strtolower($normClass) === strtolower($filename)) {
             $tokens[$classyIndex]->setContent(str_replace('/', '_', $filename));
         }
     }
 }
 /**
  * @param Tokens $tokens
  * @param int    $classIndex
  */
 private function replaceClassKeyword(Tokens $tokens, $classIndex)
 {
     $classEndIndex = $classIndex - 2;
     $classBeginIndex = $classEndIndex;
     while ($tokens[--$classBeginIndex]->isGivenKind(array(T_NS_SEPARATOR, T_STRING))) {
     }
     ++$classBeginIndex;
     $classString = $tokens->generatePartialCode($classBeginIndex, $classEndIndex);
     $classImport = false;
     foreach ($this->imports as $alias => $import) {
         if ($classString === $alias) {
             $classImport = $import;
             break;
         }
         $classStringArray = explode('\\', $classString);
         $namespaceToTest = $classStringArray[0];
         if (0 === strcmp($namespaceToTest, substr($import, -strlen($namespaceToTest)))) {
             $classImport = $import;
             break;
         }
     }
     $tokens->clearRange($classBeginIndex, $classIndex);
     $tokens->insertAt($classBeginIndex, new Token(array(T_CONSTANT_ENCAPSED_STRING, "'" . $this->makeClassFQN($classImport, $classString) . "'")));
 }
Exemplo n.º 5
0
 private function getNamespaceUseDeclarations(Tokens $tokens, array $useIndexes)
 {
     $uses = array();
     foreach ($useIndexes as $index) {
         $declarationEndIndex = $tokens->getNextTokenOfKind($index, array(';', array(T_CLOSE_TAG)));
         $declarationContent = $tokens->generatePartialCode($index + 1, $declarationEndIndex - 1);
         if (false !== strpos($declarationContent, ',') || false !== strpos($declarationContent, '{')) {
             continue;
         }
         $declarationParts = preg_split('/\\s+as\\s+/i', $declarationContent);
         if (1 === count($declarationParts)) {
             $fullName = $declarationContent;
             $declarationParts = explode('\\', $fullName);
             $shortName = end($declarationParts);
             $aliased = false;
         } else {
             list($fullName, $shortName) = $declarationParts;
             $declarationParts = explode('\\', $fullName);
             $aliased = $shortName !== end($declarationParts);
         }
         $shortName = trim($shortName);
         $uses[$shortName] = array('fullName' => trim($fullName), 'shortName' => $shortName, 'aliased' => $aliased, 'start' => $index, 'end' => $declarationEndIndex);
     }
     return $uses;
 }
 private function getNamespaceUseDeclarations(Tokens $tokens, array $useIndexes)
 {
     $uses = array();
     foreach ($useIndexes as $index) {
         $declarationEndIndex = $tokens->getNextTokenOfKind($index, array(';'));
         $declarationContent = $tokens->generatePartialCode($index + 1, $declarationEndIndex - 1);
         // ignore multiple use statements like: `use BarB, BarC as C, BarD;`
         // that should be split into few separate statements
         if (false !== strpos($declarationContent, ',')) {
             continue;
         }
         $declarationParts = preg_split('/\\s+as\\s+/i', $declarationContent);
         if (1 === count($declarationParts)) {
             $fullName = $declarationContent;
             $declarationParts = explode('\\', $fullName);
             $shortName = end($declarationParts);
             $aliased = false;
         } else {
             $fullName = $declarationParts[0];
             $shortName = $declarationParts[1];
             $declarationParts = explode('\\', $fullName);
             $aliased = $shortName !== end($declarationParts);
         }
         $shortName = trim($shortName);
         $uses[$shortName] = array('aliased' => $aliased, 'end' => $declarationEndIndex, 'fullName' => trim($fullName), 'shortName' => $shortName, 'start' => $index);
     }
     return $uses;
 }