mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
DocBlockAnalyzer: add getAnnotationFromNode
This commit is contained in:
parent
073231b5df
commit
ee14bf9654
@ -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());
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user