/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { foreach ($tokens as $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotations(); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { if (!$annotation->getTag()->valid() || !in_array($annotation->getTag()->getName(), $this->configuration['tags'], true)) { continue; } $content = $annotation->getContent(); if (1 !== preg_match('/[.。]$/u', $content) || 0 !== preg_match('/[.。](?!$)/u', $content, $matches)) { continue; } $endLine = $doc->getLine($annotation->getEnd()); $endLine->setContent(preg_replace('/(?<![.。])[.。](\\s+)$/u', '\\1', $endLine->getContent())); $startLine = $doc->getLine($annotation->getStart()); $optionalTypeRegEx = $annotation->supportTypes() ? sprintf('(?:%s\\s+(?:\\$\\w+\\s+)?)?', preg_quote(implode('|', $annotation->getTypes()))) : ''; $content = preg_replace_callback('/^(\\s*\\*\\s*@\\w+\\s+' . $optionalTypeRegEx . ')(.*)$/', function (array $matches) { return $matches[1] . lcfirst($matches[2]); }, $startLine->getContent(), 1); $startLine->setContent($content); } $token->setContent($doc->getContent()); } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType(static::$search); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $annotation->getTag()->setName(static::$replace); } $token->setContent($doc->getContent()); } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { foreach ($tokens as $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType($this->tags); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $this->fixTypes($annotation); } $token->setContent($doc->getContent()); } }
/** * {@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()); } } } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { foreach ($tokens as $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType($this->configuration); // nothing to do if there are no annotations if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $annotation->remove(); } $token->setContent($doc->getContent()); } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { $searchFor = array_keys($this->configuration); foreach ($tokens as $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType($searchFor); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $annotation->getTag()->setName($this->configuration[$annotation->getTag()->getName()]); } $token->setContent($doc->getContent()); } }
/** * Make sure the expected number of new lines prefix a namespace. * * @param Tokens $tokens * @param string[] $type */ protected function removeAnnotations(Tokens $tokens, array $type) { foreach ($tokens as $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType($type); // nothing to do if there are no annotations if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $annotation->remove(); } $token->setContent($doc->getContent()); } }
/** * {@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()); } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, Tokens $tokens) { foreach ($tokens as $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotations(); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { if ($annotation->getTag()->valid()) { $line = $doc->getLine($annotation->getEnd()); $content = preg_replace('/(?<![.。])[.。](\\s+)$/u', '\\1', $line->getContent()); if (null !== $content) { $line->setContent($content); } } } $token->setContent($doc->getContent()); } }
/** * Move all return annotations after param and throws annotations. * * @param string $content * * @return string */ private function moveReturnAnnotations($content) { $doc = new DocBlock($content); $returns = $doc->getAnnotationsOfType('return'); // nothing to do if there are no return annotations if (empty($returns)) { return $content; } $others = $doc->getAnnotationsOfType(array('param', 'throws')); // nothing to do if there are no other annotations if (empty($others)) { return $content; } // get the index of the first line of the first return annotation $start = $returns[0]->getStart(); $line = $doc->getLine($start); // move stuff about if required foreach (array_reverse($others) as $other) { if ($other->getEnd() > $start) { // we're doing this to maintain the original line indexes $line->setContent($other->getContent() . $line->getContent()); $other->remove(); } } return $doc->getContent(); }
/** * @dataProvider provideRemoveCases */ public function testRemove($index, $start, $end) { $doc = new DocBlock(self::$sample); $annotation = $doc->getAnnotation($index); $annotation->remove(); $this->assertSame('', $annotation->getContent()); $this->assertSame('', $doc->getLine($start)->getContent()); $this->assertSame('', $doc->getLine($end)->getContent()); }
/** * @dataProvider provideLinesWithTag */ public function testTag($pos, $tag) { $doc = new DocBlock(self::$sample); $line = $doc->getLine($pos); $this->assertSame($tag, $line->containsATag()); }
public function testGetAnnotationsOfTypeFoo() { $doc = new DocBlock(self::$sample); $annotations = $doc->getAnnotationsOfType('foo'); $this->assertInternalType('array', $annotations); $this->assertCount(0, $annotations); }
/** * Force the given annotations to have one empty line between each other. * * @param DocBlock $doc * @param Annotation $first * @param Annotation $second */ private function ensureAreSeparate(DocBlock $doc, Annotation $first, Annotation $second) { $pos = $first->getEnd(); $final = $second->getStart() - 1; // check if we need to add a line, or need to remove one or more lines if ($pos === $final) { $doc->getLine($pos)->addBlank(); return; } for ($pos = $pos + 1; $pos < $final; ++$pos) { $doc->getLine($pos)->remove(); } }
/** * @dataProvider provideTestClasses */ public function testThatTestClassesAreInternal(\ReflectionClass $rc) { $doc = new DocBlock($rc->getDocComment()); $this->assertNotEmpty($doc->getAnnotationsOfType('internal'), sprintf('Test class %s should have internal annotation.', $rc->getName())); }
/** * 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(); }
/** * Remove return void or return null annotations.. * * @param DocBlock $doc * @param Annotation $annotation */ private function fixAnnotation(DocBlock $doc, Annotation $annotation) { if (1 === preg_match('/@return\\s+(void|null)(?!\\|)/', $doc->getLine($annotation->getStart())->getContent())) { $annotation->remove(); } }
/** * {@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)); } }