/** * {@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()); } }
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)); }
/** * {@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()); // 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()); } }
/** * 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(); } }
/** * 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()); }
/** * 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(); } }