mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-14 12:32:00 +02:00
Updated Rector to commit 86e740248314edf23ef3307ac2a804a54c898282
86e7402483
Allows to convert Spatie enum names to snake upper case (#5435)
This commit is contained in:
parent
22304be44f
commit
1142f3d356
@ -3,6 +3,7 @@
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php81\NodeFactory;
|
||||
|
||||
use RectorPrefix202401\Nette\Utils\Strings;
|
||||
use PhpParser\BuilderFactory;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\ArrayItem;
|
||||
@ -49,6 +50,14 @@ final class EnumFactory
|
||||
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
/**
|
||||
* @var string
|
||||
* @see https://stackoverflow.com/a/2560017
|
||||
* @see https://regex101.com/r/2xEQVj/1 for changing iso9001 to iso_9001
|
||||
* @see https://regex101.com/r/Ykm6ub/1 for changing XMLParser to XML_Parser
|
||||
* @see https://regex101.com/r/Zv4JhD/1 for changing needsReview to needs_Review
|
||||
*/
|
||||
private const PASCAL_CASE_TO_UNDERSCORE_REGEX = '/(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])/';
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, PhpDocInfoFactory $phpDocInfoFactory, BuilderFactory $builderFactory, ValueResolver $valueResolver, BetterNodeFinder $betterNodeFinder)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
@ -75,7 +84,7 @@ final class EnumFactory
|
||||
$enum->stmts = \array_merge($enum->stmts, $class->getMethods());
|
||||
return $enum;
|
||||
}
|
||||
public function createFromSpatieClass(Class_ $class) : 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()]);
|
||||
@ -88,7 +97,7 @@ final class EnumFactory
|
||||
$identifierType = $this->getIdentifierTypeFromMappings($mapping);
|
||||
$enum->scalarType = new Identifier($identifierType);
|
||||
foreach ($docBlockMethods as $docBlockMethod) {
|
||||
$enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod, $class, $mapping);
|
||||
$enum->stmts[] = $this->createEnumCaseFromDocComment($docBlockMethod, $class, $mapping, $enumNameInSnakeCase);
|
||||
}
|
||||
}
|
||||
return $enum;
|
||||
@ -105,12 +114,16 @@ final class EnumFactory
|
||||
/**
|
||||
* @param array<int|string, mixed> $mapping
|
||||
*/
|
||||
private function createEnumCaseFromDocComment(PhpDocTagNode $phpDocTagNode, Class_ $class, array $mapping = []) : EnumCase
|
||||
private function createEnumCaseFromDocComment(PhpDocTagNode $phpDocTagNode, Class_ $class, array $mapping = [], bool $enumNameInSnakeCase = \false) : EnumCase
|
||||
{
|
||||
/** @var MethodTagValueNode $nodeValue */
|
||||
$nodeValue = $phpDocTagNode->value;
|
||||
$enumValue = $mapping[$nodeValue->methodName] ?? $nodeValue->methodName;
|
||||
$enumName = \strtoupper($nodeValue->methodName);
|
||||
if ($enumNameInSnakeCase) {
|
||||
$enumName = \strtoupper(Strings::replace($nodeValue->methodName, self::PASCAL_CASE_TO_UNDERSCORE_REGEX, '_$0'));
|
||||
} else {
|
||||
$enumName = \strtoupper($nodeValue->methodName);
|
||||
}
|
||||
$enumExpr = $this->builderFactory->val($enumValue);
|
||||
return new EnumCase($enumName, $enumExpr, [], ['startLine' => $class->getStartLine(), 'endLine' => $class->getEndLine()]);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Enum_;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Php81\NodeFactory\EnumFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\ValueObject\PhpVersionFeature;
|
||||
@ -19,13 +20,17 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
*
|
||||
* @see \Rector\Tests\Php81\Rector\Class_\SpatieEnumClassToEnumRector\SpatieEnumClassToEnumRectorTest
|
||||
*/
|
||||
final class SpatieEnumClassToEnumRector extends AbstractRector implements MinPhpVersionInterface
|
||||
final class SpatieEnumClassToEnumRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Php81\NodeFactory\EnumFactory
|
||||
*/
|
||||
private $enumFactory;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $toUpperSnakeCase = \false;
|
||||
public function __construct(EnumFactory $enumFactory)
|
||||
{
|
||||
$this->enumFactory = $enumFactory;
|
||||
@ -73,6 +78,13 @@ CODE_SAMPLE
|
||||
if (!$this->isObjectType($node, new ObjectType('Spatie\\Enum\\Enum'))) {
|
||||
return null;
|
||||
}
|
||||
return $this->enumFactory->createFromSpatieClass($node);
|
||||
return $this->enumFactory->createFromSpatieClass($node, $this->toUpperSnakeCase);
|
||||
}
|
||||
/**
|
||||
* @param mixed[] $configuration
|
||||
*/
|
||||
public function configure(array $configuration) : void
|
||||
{
|
||||
$this->toUpperSnakeCase = \true === ($configuration['toUpperSnakeCase'] ?? \false);
|
||||
}
|
||||
}
|
||||
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'a8cc19a3c0387f082fd432e04afce363a90ba5b0';
|
||||
public const PACKAGE_VERSION = '86e740248314edf23ef3307ac2a804a54c898282';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-01-06 18:04:23';
|
||||
public const RELEASE_DATE = '2024-01-06 14:15:07';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user