-
-
Notifications
You must be signed in to change notification settings - Fork 672
/
PhpDocInfoFactory.php
148 lines (124 loc) · 4.33 KB
/
PhpDocInfoFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocInfo;
use PhpParser\Node;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwarePhpDocNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\Attribute;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
use Rector\BetterPhpDocParser\Contract\PhpDocNodeFactoryInterface;
use Rector\BetterPhpDocParser\ValueObject\StartEndValueObject;
use Rector\Configuration\CurrentNodeProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\TypeComparator;
use Rector\NodeTypeResolver\StaticTypeMapper;
final class PhpDocInfoFactory
{
/**
* @var PhpDocParser
*/
private $phpDocParser;
/**
* @var Lexer
*/
private $lexer;
/**
* @var CurrentNodeProvider
*/
private $currentNodeProvider;
/**
* @var StaticTypeMapper
*/
private $staticTypeMapper;
/**
* @var TypeComparator
*/
private $typeComparator;
public function __construct(
PhpDocParser $phpDocParser,
Lexer $lexer,
CurrentNodeProvider $currentNodeProvider,
StaticTypeMapper $staticTypeMapper,
TypeComparator $typeComparator
) {
$this->phpDocParser = $phpDocParser;
$this->lexer = $lexer;
$this->currentNodeProvider = $currentNodeProvider;
$this->staticTypeMapper = $staticTypeMapper;
$this->typeComparator = $typeComparator;
}
public function createFromString(Node $node, string $content): PhpDocInfo
{
$tokens = $this->lexer->tokenize($content);
$phpDocNode = $this->parseTokensToPhpDocNode($tokens);
$phpDocInfo = new PhpDocInfo(
$phpDocNode,
$tokens,
$content,
$this->staticTypeMapper,
$node,
$this->typeComparator
);
$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
return $phpDocInfo;
}
public function createFromNode(Node $node): PhpDocInfo
{
/** needed for @see PhpDocNodeFactoryInterface */
$this->currentNodeProvider->setNode($node);
if ($node->getDocComment() === null) {
if ($node->getComments() !== []) {
$content = $this->createCommentsString($node);
$tokens = $this->lexer->tokenize($content);
$phpDocNode = $this->parseTokensToPhpDocNode($tokens);
} else {
$content = '';
$tokens = [];
$phpDocNode = new AttributeAwarePhpDocNode([]);
}
} else {
$content = $node->getDocComment()->getText();
$tokens = $this->lexer->tokenize($content);
$phpDocNode = $this->parseTokensToPhpDocNode($tokens);
$this->setPositionOfLastToken($phpDocNode);
}
$phpDocInfo = new PhpDocInfo(
$phpDocNode,
$tokens,
$content,
$this->staticTypeMapper,
$node,
$this->typeComparator
);
$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
return $phpDocInfo;
}
/**
* Needed for printing
*/
private function setPositionOfLastToken(AttributeAwarePhpDocNode $attributeAwarePhpDocNode): void
{
if ($attributeAwarePhpDocNode->children === []) {
return;
}
$phpDocChildNodes = $attributeAwarePhpDocNode->children;
/** @var AttributeAwareNodeInterface $lastChildNode */
$lastChildNode = array_pop($phpDocChildNodes);
/** @var StartEndValueObject $startEndValueObject */
$startEndValueObject = $lastChildNode->getAttribute(Attribute::START_END);
if ($startEndValueObject !== null) {
$attributeAwarePhpDocNode->setAttribute(Attribute::LAST_TOKEN_POSITION, $startEndValueObject->getEnd());
}
}
private function createCommentsString(Node $node): string
{
return implode('', $node->getComments());
}
private function parseTokensToPhpDocNode(array $tokens): AttributeAwarePhpDocNode
{
$tokenIterator = new TokenIterator($tokens);
return $this->phpDocParser->parse($tokenIterator);
}
}