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(); }
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(); }
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->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->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, 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, $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()); } }
/** * {@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(); }
protected function checkProperty(Tokens $tokens, $index) { $variableName = $tokens[$index]->getContent(); $visibilityIndex = $tokens->getPrevMeaningfulToken($index); $visibilityToken = $tokens[$visibilityIndex]; // Only public properties are allowed if ($visibilityToken->getId() !== T_PUBLIC) { $tokens->reportAt($index, new Message(E_ERROR, 'check_class_property_must_be_public', ['property' => $variableName, 'visibility' => $visibilityToken->getId() === T_PRIVATE ? 'private' : 'protected'])); return false; } // A doc block is required with a @var for ($i = $visibilityIndex - 1; $i >= 0; --$i) { $token = $tokens[$i]; if ($token->isGivenKind([T_WHITESPACE, T_ENCAPSED_AND_WHITESPACE])) { continue; } if (!$token->isGivenKind(T_DOC_COMMENT)) { $tokens->reportAt($index, new Message(E_ERROR, 'check_class_property_must_have_a_phpdoc', ['property' => $variableName])); break; } $doc = new DocBlock($token->getContent()); $annotations = $doc->getAnnotationsOfType('var'); if (count($annotations) === 0) { $tokens->reportAt($i, new Message(E_ERROR, 'check_class_property_phpdoc_must_have_var', ['property' => $variableName])); break; } if (count($annotations) > 1) { $tokens->reportAt($i, new Message(E_ERROR, 'check_class_property_phpdoc_invalid_var', ['property' => $variableName])); break; } foreach ($annotations as $annotation) { $parts = preg_split('/(\\s+)/Su', trim($annotation->getContent(), " \t\n\r\v*"), 3); if (count($parts) <= 1) { $tokens->reportAt($i, new Message(E_ERROR, 'check_class_property_phpdoc_invalid_var', ['property' => $variableName])); } } break; } return $variableName; }
/** * 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 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())); }
public function testGetAnnotationsOfTypeFoo() { $doc = new DocBlock(self::$sample); $annotations = $doc->getAnnotationsOfType('foo'); $this->assertInternalType('array', $annotations); $this->assertCount(0, $annotations); }