nodeNameResolver = $nodeNameResolver;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->builderFactory = $builderFactory;
$this->valueResolver = $valueResolver;
$this->betterNodeFinder = $betterNodeFinder;
}
public function createFromClass(Class_ $class) : Enum_
{
$shortClassName = $this->nodeNameResolver->getShortName($class);
$enum = new Enum_($shortClassName, [], ['startLine' => $class->getStartLine(), 'endLine' => $class->getEndLine()]);
$enum->namespacedName = $class->namespacedName;
$constants = $class->getConstants();
$enum->stmts = $class->getTraitUses();
if ($constants !== []) {
$value = $this->valueResolver->getValue($constants[0]->consts[0]->value);
$enum->scalarType = \is_string($value) ? new Identifier('string') : new Identifier('int');
// constant to cases
foreach ($constants as $constant) {
$enum->stmts[] = $this->createEnumCaseFromConst($constant);
}
}
$enum->stmts = \array_merge($enum->stmts, $class->getMethods());
return $enum;
}
public function createFromSpatieClass(Class_ $class, bool $enumNameInSnakeCase = \false) : Enum_
{
$shortClassName = $this->nodeNameResolver->getShortName($class);
$enum = new Enum_($shortClassName, [], ['startLine' => $class->getStartLine(), 'endLine' => $class->getEndLine()]);
$enum->namespacedName = $class->namespacedName;
// constant to cases
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
$docBlockMethods = $phpDocInfo->getTagsByName('@method');
if ($docBlockMethods !== []) {
$mapping = $this->generateMappingFromClass($class);
$identifierType = $this->getIdentifierTypeFromMappings($mapping);
$enum->scalarType = new Identifier($identifierType);
foreach ($docBlockMethods as $docBlockMethod) {
$enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod, $class, $mapping, $enumNameInSnakeCase);
}
}
return $enum;
}
private function createEnumCaseFromConst(ClassConst $classConst) : EnumCase
{
$constConst = $classConst->consts[0];
$enumCase = new EnumCase($constConst->name, $constConst->value, [], ['startLine' => $constConst->getStartLine(), 'endLine' => $constConst->getEndLine()]);
// mirror comments
$enumCase->setAttribute(AttributeKey::PHP_DOC_INFO, $classConst->getAttribute(AttributeKey::PHP_DOC_INFO));
$enumCase->setAttribute(AttributeKey::COMMENTS, $classConst->getAttribute(AttributeKey::COMMENTS));
return $enumCase;
}
/**
* @param array