/** * {@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) { $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()); $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) { 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(); }
/** * 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(); }
public function testEmptyContent() { $doc = new DocBlock(''); $this->assertSame('', $doc->getContent()); }