public function testGetAnnotations() { $doc = new DocBlock(self::$sample); $this->assertInternalType('array', $doc->getAnnotations()); $this->assertCount(5, $doc->getAnnotations()); foreach ($doc->getAnnotations() as $index => $annotations) { $this->assertInstanceOf('PhpCsFixer\\DocBlock\\Annotation', $annotations); $this->assertSame($doc->getAnnotation($index), $annotations); } $this->assertEmpty($doc->getAnnotation(5)); }
/** * {@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()); } }
/** * Make sure the annotations are correctly separated. * * @param DocBlock $doc * * @return string */ private function fixAnnotations(DocBlock $doc) { foreach ($doc->getAnnotations() as $index => $annotation) { $next = $doc->getAnnotation($index + 1); if (null === $next) { break; } if (true === $next->getTag()->valid()) { if (TagComparator::shouldBeTogether($annotation->getTag(), $next->getTag())) { $this->ensureAreTogether($doc, $annotation, $next); } else { $this->ensureAreSeparate($doc, $annotation, $next); } } } return $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()); } }