コード例 #1
0
 public function testGetLines()
 {
     $doc = new DocBlock(self::$sample);
     $this->assertInternalType('array', $doc->getLines());
     $this->assertCount(15, $doc->getLines());
     foreach ($doc->getLines() as $index => $line) {
         $this->assertInstanceOf('PhpCsFixer\\DocBlock\\Line', $line);
         $this->assertSame($doc->getLine($index), $line);
     }
     $this->assertEmpty($doc->getLine(15));
 }
コード例 #2
0
 /**
  * Make sure the description is separated from the annotations.
  *
  * @param DocBlock $doc
  */
 private function fixDescription(DocBlock $doc)
 {
     foreach ($doc->getLines() as $index => $line) {
         if ($line->containsATag()) {
             break;
         }
         if ($line->containsUsefulContent()) {
             $next = $doc->getLine($index + 1);
             if ($next->containsATag()) {
                 $line->addBlank();
                 break;
             }
         }
     }
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $token) {
         if (!$token->isGivenKind(T_DOC_COMMENT)) {
             continue;
         }
         $doc = new DocBlock($token->getContent());
         $end = $this->findShortDescriptionEnd($doc->getLines());
         if (null !== $end) {
             $line = $doc->getLine($end);
             $content = rtrim($line->getContent());
             if (!$this->isCorrectlyFormatted($content)) {
                 $line->setContent($content . '.' . $this->whitespacesConfig->getLineEnding());
                 $token->setContent($doc->getContent());
             }
         }
     }
 }
コード例 #4
0
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     foreach ($tokens as $token) {
         if (!$token->isGivenKind(T_DOC_COMMENT)) {
             continue;
         }
         $doc = new DocBlock($token->getContent());
         // don't process single line docblocks
         if (1 === count($doc->getLines())) {
             continue;
         }
         $annotations = $doc->getAnnotationsOfType(array('param', 'return', 'type', 'var'));
         // only process docblocks where the first meaningful annotation is @type or @var
         if (!isset($annotations[0]) || !in_array($annotations[0]->getTag()->getName(), array('type', 'var'), true)) {
             continue;
         }
         $this->fixLine($doc->getLine($annotations[0]->getStart()));
         $token->setContent($doc->getContent());
     }
 }
コード例 #5
0
ファイル: PhpdocTrimFixer.php プロジェクト: thekabal/tki
 /**
  * Make sure the last useful is immediately before after the final line.
  *
  * @param string $content
  *
  * @return string
  */
 private function fixEnd($content)
 {
     $doc = new DocBlock($content);
     $lines = array_reverse($doc->getLines());
     $total = count($lines);
     foreach ($lines as $index => $line) {
         if (!$line->isTheEnd()) {
             // don't remove lines with content and don't entirely delete docblocks
             if ($total - $index < 3 || $line->containsUsefulContent()) {
                 break;
             }
             $line->remove();
         }
     }
     return $doc->getContent();
 }
 /**
  * {@inheritdoc}
  */
 public function fix(\SplFileInfo $file, Tokens $tokens)
 {
     for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
         $token = $tokens[$index];
         if (!$token->isGivenKind(T_DOC_COMMENT)) {
             continue;
         }
         if (1 === preg_match('/inheritdoc/i', $token->getContent())) {
             continue;
         }
         $index = $tokens->getNextMeaningfulToken($index);
         if (null === $index) {
             return;
         }
         while ($tokens[$index]->isGivenKind(array(T_ABSTRACT, T_FINAL, T_PRIVATE, T_PROTECTED, T_PUBLIC, T_STATIC, T_VAR))) {
             $index = $tokens->getNextMeaningfulToken($index);
         }
         if (!$tokens[$index]->isGivenKind(T_FUNCTION)) {
             continue;
         }
         $openIndex = $tokens->getNextTokenOfKind($index, array('('));
         $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
         $arguments = array();
         foreach ($this->getArguments($tokens, $openIndex, $index) as $start => $end) {
             $argumentInfo = $this->prepareArgumentInformation($tokens, $start, $end);
             if (!$this->configuration['only_untyped'] || '' === $argumentInfo['type']) {
                 $arguments[$argumentInfo['name']] = $argumentInfo;
             }
         }
         if (!count($arguments)) {
             continue;
         }
         $doc = new DocBlock($token->getContent());
         $lastParamLine = null;
         foreach ($doc->getAnnotationsOfType('param') as $annotation) {
             $pregMatched = preg_match('/^[^$]+(\\$\\w+).*$/s', $annotation->getContent(), $matches);
             if (1 === $pregMatched) {
                 unset($arguments[$matches[1]]);
             }
             $lastParamLine = max($lastParamLine, $annotation->getEnd());
         }
         if (!count($arguments)) {
             continue;
         }
         $lines = $doc->getLines();
         $linesCount = count($lines);
         preg_match('/^(\\s*).*$/', $lines[$linesCount - 1]->getContent(), $matches);
         $indent = $matches[1];
         $newLines = array();
         foreach ($arguments as $argument) {
             $type = $argument['type'] ?: 'mixed';
             if ('?' !== $type[0] && 'null' === strtolower($argument['default'])) {
                 $type = 'null|' . $type;
             }
             $newLines[] = new Line(sprintf('%s* @param %s %s%s', $indent, $type, $argument['name'], $this->whitespacesConfig->getLineEnding()));
         }
         array_splice($lines, $lastParamLine ? $lastParamLine + 1 : $linesCount - 1, 0, $newLines);
         $token->setContent(implode('', $lines));
     }
 }