2021-05-14 09:53:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php81\NodeFactory;
|
2021-05-14 09:53:43 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\BuilderFactory;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassConst;
|
|
|
|
use PhpParser\Node\Stmt\Enum_;
|
|
|
|
use PhpParser\Node\Stmt\EnumCase;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
|
|
|
use Rector\Core\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-05-14 09:53:43 +00:00
|
|
|
final class EnumFactory
|
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-14 09:53:43 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
2021-08-12 14:48:46 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-08-12 14:48:46 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \PhpParser\BuilderFactory
|
2021-08-12 14:48:46 +00:00
|
|
|
*/
|
|
|
|
private $builderFactory;
|
2021-12-02 07:34:16 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-12-02 07:34:16 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
|
|
|
|
*/
|
|
|
|
private $valueResolver;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(NodeNameResolver $nodeNameResolver, PhpDocInfoFactory $phpDocInfoFactory, BuilderFactory $builderFactory, ValueResolver $valueResolver)
|
2021-05-14 09:53:43 +00:00
|
|
|
{
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
2021-08-12 14:48:46 +00:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
|
|
|
$this->builderFactory = $builderFactory;
|
2021-12-02 07:34:16 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2021-05-14 09:53:43 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function createFromClass(Class_ $class) : Enum_
|
2021-05-14 09:53:43 +00:00
|
|
|
{
|
|
|
|
$shortClassName = $this->nodeNameResolver->getShortName($class);
|
2022-10-17 13:37:08 +00:00
|
|
|
$enum = new Enum_($shortClassName, [], ['startLine' => $class->getStartLine(), 'endLine' => $class->getEndLine()]);
|
2022-06-21 14:59:06 +00:00
|
|
|
$enum->namespacedName = $class->namespacedName;
|
2021-12-02 07:34:16 +00:00
|
|
|
$constants = $class->getConstants();
|
2022-07-20 13:38:02 +00:00
|
|
|
$enum->stmts = $class->getTraitUses();
|
2021-12-02 07:34:16 +00:00
|
|
|
if ($constants !== []) {
|
|
|
|
$value = $this->valueResolver->getValue($constants[0]->consts[0]->value);
|
2022-06-07 08:22:29 +00:00
|
|
|
$enum->scalarType = \is_string($value) ? new Identifier('string') : new Identifier('int');
|
2021-12-02 07:34:16 +00:00
|
|
|
// constant to cases
|
|
|
|
foreach ($constants as $constant) {
|
|
|
|
$enum->stmts[] = $this->createEnumCaseFromConst($constant);
|
|
|
|
}
|
2021-05-14 09:53:43 +00:00
|
|
|
}
|
|
|
|
return $enum;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function createFromSpatieClass(Class_ $class) : Enum_
|
2021-08-12 14:48:46 +00:00
|
|
|
{
|
|
|
|
$shortClassName = $this->nodeNameResolver->getShortName($class);
|
2022-10-17 13:37:08 +00:00
|
|
|
$enum = new Enum_($shortClassName, [], ['startLine' => $class->getStartLine(), 'endLine' => $class->getEndLine()]);
|
2022-06-21 14:59:06 +00:00
|
|
|
$enum->namespacedName = $class->namespacedName;
|
2021-08-12 14:48:46 +00:00
|
|
|
// constant to cases
|
2022-06-13 09:09:58 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
|
|
|
|
$docBlockMethods = $phpDocInfo->getTagsByName('@method');
|
|
|
|
if ($docBlockMethods !== []) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$enum->scalarType = new Identifier('string');
|
2021-08-12 14:48:46 +00:00
|
|
|
foreach ($docBlockMethods as $docBlockMethod) {
|
|
|
|
$enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $enum;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createEnumCaseFromConst(ClassConst $classConst) : EnumCase
|
2021-05-14 09:53:43 +00:00
|
|
|
{
|
|
|
|
$constConst = $classConst->consts[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
$enumCase = new EnumCase($constConst->name, $constConst->value);
|
2021-08-12 14:48:46 +00:00
|
|
|
// mirror comments
|
2022-06-07 08:22:29 +00:00
|
|
|
$enumCase->setAttribute(AttributeKey::PHP_DOC_INFO, $classConst->getAttribute(AttributeKey::PHP_DOC_INFO));
|
|
|
|
$enumCase->setAttribute(AttributeKey::COMMENTS, $classConst->getAttribute(AttributeKey::COMMENTS));
|
2021-05-14 09:53:43 +00:00
|
|
|
return $enumCase;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createEnumCaseFromDocComment(PhpDocTagNode $phpDocTagNode) : EnumCase
|
2021-08-12 14:48:46 +00:00
|
|
|
{
|
2021-10-27 08:50:45 +00:00
|
|
|
/** @var MethodTagValueNode $nodeValue */
|
2021-08-12 16:01:53 +00:00
|
|
|
$nodeValue = $phpDocTagNode->value;
|
2022-06-13 09:09:58 +00:00
|
|
|
$enumName = \strtoupper($nodeValue->methodName);
|
|
|
|
$enumExpr = $this->builderFactory->val($nodeValue->methodName);
|
|
|
|
return new EnumCase($enumName, $enumExpr);
|
2021-08-12 14:48:46 +00:00
|
|
|
}
|
2021-05-14 09:53:43 +00:00
|
|
|
}
|