public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens as $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType(static::$tags); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $this->fixTypes($annotation); } $token->setContent($doc->getContent()); } return $tokens->generateCode(); }
public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $doc = new DocBlock($token->getContent()); if (1 === count($doc->getLines())) { continue; } $annotations = $doc->getAnnotationsOfType(array('param', 'return', 'type', '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()); } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); 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()); } return $tokens->generateCode(); }
private function ensureAreSeparate(DocBlock $doc, Annotation $first, Annotation $second) { $pos = $first->getEnd(); $final = $second->getStart() - 1; if ($pos === $final) { $doc->getLine($pos)->addBlank(); return; } for ($pos = $pos + 1; $pos < $final; ++$pos) { $doc->getLine($pos)->remove(); } }
/** * {@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()); } }
private function fixEnd($content) { $doc = new DocBlock($content); $lines = array_reverse($doc->getLines()); $total = count($lines); foreach ($lines as $index => $line) { if (!$line->isTheEnd()) { if ($total - $index < 3 || $line->containsUsefulContent()) { break; } $line->remove(); } } return $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) { $line = $doc->getLine($annotation->getStart()); $line->setContent(str_replace(static::$input, static::$output, $line->getContent())); } $token->setContent($doc->getContent()); } }
public function fix(SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $docBlock = new DocBlock($token->getContent()); $annotations = $docBlock->getAnnotationsOfType('author'); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $annotation->remove(); } $token->setContent($docBlock->getContent()); } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType('type'); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $line = $doc->getLine($annotation->getStart()); $line->setContent(str_replace('@type', '@var', $line->getContent())); } $token->setContent($doc->getContent()); } return $tokens->generateCode(); }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType(array('package', 'subpackage')); // nothing to do if there are no annotations if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $annotation->remove(); } $token->setContent($doc->getContent()); } return $tokens->generateCode(); }
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); 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->tags); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $this->fixTypes($annotation); } $token->setContent($doc->getContent()); } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $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 . ".\n"); $token->setContent($doc->getContent()); } } } return $tokens->generateCode(); }
/** * {@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(self::$tags); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $this->fixScalars($doc->getLine($annotation->getStart()), $annotation->getTag()->getName()); } $token->setContent($doc->getContent()); } }
/** * resolvePhpDocParamTypes. * * @param $tokens * @param $useDeclarations * @param $namespaceDeclarations */ private function resolvePhpDocParamTypes($tokens, $useDeclarations, $namespaceDeclarations) { foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType('param'); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $line = $doc->getLine($annotation->getStart()); $matches = $this->getMatches($line->getContent()); if (null === $matches) { continue; } if (!isset($matches['hint'])) { continue; } if (substr($matches['hint'], 0, 1) === '\\') { continue; } if (false !== strpos($matches['hint'], '\\')) { continue; } $resolved = false; // resolve hint foreach ($useDeclarations as $useDeclaration) { // replace hint by useDeclaration fullname if it match if (in_array($matches['hint'], array($useDeclaration['shortName'], $useDeclaration['aliased']), true)) { $line->setContent(str_replace($matches['hint'], $useDeclaration['fullName'], $line->getContent())); $resolved = true; break; } } if ($resolved) { continue; } // add namespace if needed $namespace = $namespaceDeclarations[0]['name']; if (false === strpos($matches['hint'], '\\')) { $line->setContent(str_replace($matches['hint'], $namespace . '\\' . $matches['hint'], $line->getContent())); } } $token->setContent($doc->getContent()); } }
public function fix(SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); $function = $tokens->findSequence([[T_FUNCTION], [T_STRING, self::CONSTRUCTOR_NAME], '(']); if (null !== $function) { $docBlockIndex = $tokens->getPrevTokenOfKind(key($function), [[T_DOC_COMMENT]]); $docBlockToken = $tokens[$docBlockIndex]; $docBlock = new DocBlock($docBlockToken->getContent()); $annotations = $docBlock->getAnnotationsOfType('return'); if (!empty($annotations)) { foreach ($annotations as $annotation) { $annotation->remove(); } $docBlockToken->setContent($docBlock->getContent()); } } return $tokens->generateCode(); }
/** * {@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('var'); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $line = $doc->getLine($annotation->getStart()); $line->setContent(str_replace('@var', '@type', $line->getContent())); } $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 . ".\n"); $token->setContent($doc->getContent()); } } } }
/** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $doc = new DocBlock($token->getContent()); // don't process single line docblocks if (1 === count($doc->getLines())) { continue; } $annotations = $doc->getAnnotationsOfType(array('var', 'type')); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $this->fixLine($doc->getLine($annotation->getStart())); } $token->setContent($doc->getContent()); } return $tokens->generateCode(); }
/** * {@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()); } }
private function moveReturnAnnotations($content) { $doc = new DocBlock($content); $returns = $doc->getAnnotationsOfType('return'); if (empty($returns)) { return $content; } $others = $doc->getAnnotationsOfType(array('param', 'throws')); if (empty($others)) { return $content; } $start = $returns[0]->getStart(); $line = $doc->getLine($start); foreach (array_reverse($others) as $other) { if ($other->getEnd() > $start) { $line->setContent($other->getContent() . $line->getContent()); $other->remove(); } } 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()); // don't process single line docblocks if (1 === count($doc->getLines())) { continue; } $annotations = $doc->getAnnotationsOfType(array('var', 'type')); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $this->fixLine($doc->getLine($annotation->getStart())); } $token->setContent($doc->getContent()); } }
public function fix(SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); $thisYear = date('Y'); foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $token->setContent(preg_replace_callback('/Copyright © (?<firstYear>\\d{4})-\\d{4}, [^\\.]+/', function ($matches) use($thisYear) { return 'Copyright © ' . $matches['firstYear'] . '-' . $thisYear . ', ' . 'Hoa community'; }, $token->getContent())); $docBlock = new DocBlock($token->getContent()); $annotations = $docBlock->getAnnotationsOfType('copyright'); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $line = $docBlock->getLine($annotation->getStart()); $line->setContent(' * @copyright Copyright © 2007-' . $thisYear . ' Hoa community' . "\n"); } $token->setContent($docBlock->getContent()); } return $tokens->generateCode(); }
public function fix(SplFileInfo $file, $content) { $tokens = Tokens::fromCode($content); foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) { $docBlock = new DocBlock($token->getContent()); $annotations = $docBlock->getAnnotationsOfType('var'); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $line = $docBlock->getLine($annotation->getStart()); $lineContent = $line->getContent(); if (0 !== preg_match('/^(?<before>.*?@var )(?<one>[^\\s]+) (?<two>\\w+)/', $lineContent, $matches)) { if ('object' === $matches['two']) { $line->setContent($matches['before'] . $matches['one'] . "\n"); } else { $line->setContent($matches['before'] . $matches['two'] . "\n"); } } } $token->setContent($docBlock->getContent()); } return $tokens->generateCode(); }
/** * @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()); }
/** * 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(); } }
public function testGetAnnotationsOfTypeFoo() { $doc = new DocBlock(self::$sample); $annotations = $doc->getAnnotationsOfType('foo'); $this->assertInternalType('array', $annotations); $this->assertCount(0, $annotations); }
/** * 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 provideLinesWithTag */ public function testTag($pos, $tag) { $doc = new DocBlock(self::$sample); $line = $doc->getLine($pos); $this->assertSame($tag, $line->containsATag()); }