Skip to content

Commit e7d2598

Browse files
authored
Move all $symbolName logic into one method (#244)
1 parent 45aae72 commit e7d2598

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/Visitor.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,8 @@ public function enterNode(Node $node)
8181
return null;
8282
}
8383

84-
$symbolName = self::getNodeName($node);
85-
86-
if ($node instanceof ClassMethod || $node instanceof Property) {
87-
$parent = $this->stack[count($this->stack) - 2];
88-
\assert($parent instanceof \PhpParser\Node\Stmt\ClassLike);
89-
90-
if ($parent->name !== null) {
91-
$symbolName = sprintf(
92-
'%1$s::%2$s%3$s',
93-
$parent->name->name,
94-
$node instanceof Property ? '$' : '',
95-
$symbolName
96-
);
97-
}
98-
}
99-
$node->setAttribute('fullSymbolName', $symbolName);
84+
$symbolName = $this->getSymbolName($node);
85+
$node->setAttribute('WPStubs_symbolName', $symbolName);
10086

10187
$additions = $this->generateAdditionalTagsFromDoc($docComment);
10288
if (count($additions) > 0) {
@@ -126,17 +112,32 @@ public function enterNode(Node $node)
126112
return null;
127113
}
128114

129-
private static function getNodeName(Node $node): string
115+
private function getSymbolName(Node $node): string
130116
{
131117
if ((($node instanceof Function_) || ($node instanceof ClassMethod) || ($node instanceof Class_)) && $node->name instanceof Identifier) {
132-
return $node->name->name;
118+
$name = $node->name->name;
133119
}
134120

135121
if ($node instanceof Property) {
136-
return $node->props[0]->name->name;
122+
$name = $node->props[0]->name->name;
137123
}
138124

139-
return '';
125+
\assert(isset($name), 'Node does not have a name');
126+
127+
if ($node instanceof Function_ || $node instanceof Class_) {
128+
return $name;
129+
}
130+
131+
$parent = $this->stack[count($this->stack) - 2];
132+
\assert($parent instanceof \PhpParser\Node\Stmt\ClassLike);
133+
\assert($parent->name instanceof \PhpParser\Node\Identifier);
134+
135+
return sprintf(
136+
'%1$s::%2$s%3$s',
137+
$parent->name->name,
138+
$node instanceof Property ? '$' : '',
139+
$name
140+
);
140141
}
141142

142143
/**
@@ -164,10 +165,10 @@ private function postProcessNode(Node $node): void
164165
return;
165166
}
166167

167-
/** @var ?string $fullSymbolName */
168-
$fullSymbolName = $node->getAttribute('fullSymbolName');
168+
/** @var ?string $symbolName */
169+
$symbolName = $node->getAttribute('WPStubs_symbolName');
169170

170-
if ($fullSymbolName === null) {
171+
if ($symbolName === null) {
171172
return;
172173
}
173174

@@ -177,13 +178,13 @@ private function postProcessNode(Node $node): void
177178
return;
178179
}
179180

180-
$newDocComment = $this->addTags($fullSymbolName, $docComment);
181+
$newDocComment = $this->addTags($symbolName, $docComment);
181182

182183
if ($newDocComment instanceof Doc) {
183184
$node->setDocComment($newDocComment);
184185
}
185186

186-
if (! isset($this->additionalTagStrings[$fullSymbolName])) {
187+
if (! isset($this->additionalTagStrings[$symbolName])) {
187188
return;
188189
}
189190

@@ -193,7 +194,7 @@ private function postProcessNode(Node $node): void
193194
return;
194195
}
195196

196-
$newDocComment = $this->addStringTags($fullSymbolName, $docComment);
197+
$newDocComment = $this->addStringTags($symbolName, $docComment);
197198

198199
if (! ($newDocComment instanceof Doc)) {
199200
return;

0 commit comments

Comments
 (0)