DocBlockAnalyzer: add getAnnotationFromNode

This commit is contained in:
TomasVotruba 2017-09-08 01:33:12 +02:00
parent 073231b5df
commit ee14bf9654
2 changed files with 23 additions and 4 deletions

View File

@ -5,6 +5,7 @@ namespace Rector\NodeAnalyzer;
use PhpCsFixer\DocBlock\DocBlock;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use Rector\Exception\NotImplementedException;
final class DocBlockAnalyzer
{
@ -27,6 +28,27 @@ final class DocBlockAnalyzer
$node->setDocComment(new Doc($docBlock->getContent()));
}
public function getAnnotationFromNode(Node $node, string $annotation): string
{
$docBlock = new DocBlock($node->getDocComment());
$annotationTags = $docBlock->getAnnotationsOfType($annotation);
if (count($annotationTags) === 0) {
return '';
}
if (count($annotationTags) === 1 && $annotationTags[0]->getTag()->getName() === 'var') {
return implode('|' , $annotationTags[0]->getTypes());
}
throw new NotImplementedException(sprintf(
'Not implemented yet. Go to "%s::%s()" and add check for "%s" annotation.',
__CLASS__,
__METHOD__,
$annotation
));
}
private function createDocBlockFromNode(Node $node): DocBlock
{
return new DocBlock($node->getDocComment());

View File

@ -69,10 +69,7 @@ final class InjectPropertyRector extends AbstractRector
private function addPropertyToCollector(Property $propertyNode): void
{
$propertyDocBlock = new DocBlock($propertyNode->getDocComment());
$propertyType = $propertyDocBlock->getAnnotationsOfType('var')[0]
->getTypes()[0];
$propertyType = $this->docBlockAnalyzer->getAnnotationFromNode($propertyNode, 'var');
$propertyName = (string) $propertyNode->props[0]->name;