/** * {@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('return'); if (empty($annotations)) { continue; } foreach ($annotations as $annotation) { $this->fixAnnotation($doc, $annotation); } $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()); } }
/** * @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())); }
/** * 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(); }
public function testGetAnnotationsOfTypeFoo() { $doc = new DocBlock(self::$sample); $annotations = $doc->getAnnotationsOfType('foo'); $this->assertInternalType('array', $annotations); $this->assertCount(0, $annotations); }
/** * {@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)); } }