Skip to content

Commit d66fa71

Browse files
committed
Turn class strings into const
1 parent 7d61142 commit d66fa71

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/RectorRule.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Knuckles\Scribe\Tags2Attributes\TagParsers\UrlParamTagParser;
1717
use PhpParser\Node;
1818
use PhpParser\Node\Attribute;
19+
use PhpParser\Node\Expr\ClassConstFetch;
1920
use PhpParser\Node\Name\FullyQualified;
2021
use PHPStan\PhpDocParser\Ast\Node as DocNode;
2122
use PhpParser\Node\AttributeGroup;
@@ -241,6 +242,7 @@ private function processGenericTags(PhpDocInfo $phpDocInfo) : array
241242
}
242243

243244
$attributeClass = $annotationToAttribute->getAttributeClass();
245+
244246
$items = $this->getArgs(
245247
$tag, $docNode->value?->value, $phpDocInfo
246248
);
@@ -249,6 +251,7 @@ private function processGenericTags(PhpDocInfo $phpDocInfo) : array
249251
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args);
250252
$attribute = new Attribute($fullyQualified, $args);
251253
$attributeGroups[] = new AttributeGroup([$attribute]);
254+
252255
$phpDocInfo->markAsChanged();
253256
return PhpDocNodeTraverser::NODE_REMOVE;
254257
}
@@ -273,6 +276,19 @@ public function getArgs($tag, $tagContent, $phpDocInfo)
273276
return $arguments;
274277
};
275278

279+
$convertClassNamesToConst = function ($args) {
280+
foreach ($args as $key => $value) {
281+
if (is_string($value) && class_exists($value)) {
282+
$fullyQualified = new FullyQualified($value);
283+
$args[$key] = new ClassConstFetch($fullyQualified, 'class');
284+
} else {
285+
$args[$key] = $value;
286+
}
287+
}
288+
289+
return $args;
290+
};
291+
276292
return match(strtolower($tag)) {
277293
'header' => explode(' ', $tagContent),
278294
'urlparam' => $parseAndRemoveEmptyKeys(UrlParamTagParser::class),
@@ -282,10 +298,10 @@ public function getArgs($tag, $tagContent, $phpDocInfo)
282298

283299
'response' => $parseAndRemoveEmptyKeys(ResponseTagParser::class),
284300
'responsefile' => $parseAndRemoveEmptyKeys(ResponseFileTagParser::class),
285-
'apiresource' => $parseAndRemoveEmptyKeys(ApiResourceTagParser::class, $phpDocInfo->getPhpDocNode()->getTags()),
286-
'apiresourcecollection' => $parseAndRemoveEmptyKeys(ApiResourceTagParser::class, $phpDocInfo->getPhpDocNode()->getTags(), true),
287-
'transformer' => $parseAndRemoveEmptyKeys(TransformerTagParser::class, $phpDocInfo->getPhpDocNode()->getTags()),
288-
'transformercollection' => $parseAndRemoveEmptyKeys(TransformerTagParser::class, $phpDocInfo->getPhpDocNode()->getTags(), true),
301+
'apiresource' => $convertClassNamesToConst($parseAndRemoveEmptyKeys(ApiResourceTagParser::class, $phpDocInfo->getPhpDocNode()->getTags())),
302+
'apiresourcecollection' => $convertClassNamesToConst($parseAndRemoveEmptyKeys(ApiResourceTagParser::class, $phpDocInfo->getPhpDocNode()->getTags(), true)),
303+
'transformer' => $convertClassNamesToConst($parseAndRemoveEmptyKeys(TransformerTagParser::class, $phpDocInfo->getPhpDocNode()->getTags())),
304+
'transformercollection' => $convertClassNamesToConst($parseAndRemoveEmptyKeys(TransformerTagParser::class, $phpDocInfo->getPhpDocNode()->getTags(), true)),
289305

290306
'subgroup' => $parseAndRemoveEmptyKeys(SubgroupTagParser::class, $phpDocInfo->getPhpDocNode()->getTags()),
291307
};

0 commit comments

Comments
 (0)