?????????? ????????? - ??????????????? - /home/agenciai/public_html/cd38d8/Php81.zip
???????
PK �,%\½[;[ [ NodeFactory/EnumFactory.phpnu �[��� <?php declare (strict_types=1); namespace Rector\Php81\NodeFactory; use RectorPrefix202411\Nette\Utils\Strings; use PhpParser\BuilderFactory; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Identifier; use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassConst; use PhpParser\Node\Stmt\ClassMethod; 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\NodeNameResolver\NodeNameResolver; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PhpParser\Node\Value\ValueResolver; final class EnumFactory { /** * @readonly * @var \Rector\NodeNameResolver\NodeNameResolver */ private $nodeNameResolver; /** * @readonly * @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory */ private $phpDocInfoFactory; /** * @readonly * @var \PhpParser\BuilderFactory */ private $builderFactory; /** * @readonly * @var \Rector\PhpParser\Node\Value\ValueResolver */ private $valueResolver; /** * @readonly * @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])/'; /** * @var string * @see https://regex101.com/r/FneU33/1 */ private const MULTI_UNDERSCORES_REGEX = '#_{2,}#'; public function __construct(NodeNameResolver $nodeNameResolver, PhpDocInfoFactory $phpDocInfoFactory, BuilderFactory $builderFactory, ValueResolver $valueResolver, BetterNodeFinder $betterNodeFinder) { $this->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<int|string, mixed> $mapping */ 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; if ($enumNameInSnakeCase) { $enumName = \strtoupper(Strings::replace($nodeValue->methodName, self::PASCAL_CASE_TO_UNDERSCORE_REGEX, '_$0')); $enumName = Strings::replace($enumName, self::MULTI_UNDERSCORES_REGEX, '_'); } else { $enumName = \strtoupper($nodeValue->methodName); } $enumExpr = $this->builderFactory->val($enumValue); return new EnumCase($enumName, $enumExpr, [], ['startLine' => $class->getStartLine(), 'endLine' => $class->getEndLine()]); } /** * @return array<int|string, mixed> */ private function generateMappingFromClass(Class_ $class) : array { $classMethod = $class->getMethod('values'); if (!$classMethod instanceof ClassMethod) { return []; } $returns = $this->betterNodeFinder->findReturnsScoped($classMethod); /** @var array<int|string, mixed> $mapping */ $mapping = []; foreach ($returns as $return) { if (!$return->expr instanceof Array_) { continue; } $mapping = $this->collectMappings($return->expr->items, $mapping); } return $mapping; } /** * @param null[]|ArrayItem[] $items * @param array<int|string, mixed> $mapping * @return array<int|string, mixed> */ private function collectMappings(array $items, array $mapping) : array { foreach ($items as $item) { if (!$item instanceof ArrayItem) { continue; } if (!$item->key instanceof LNumber && !$item->key instanceof String_) { continue; } if (!$item->value instanceof LNumber && !$item->value instanceof String_) { continue; } $mapping[$item->key->value] = $item->value->value; } return $mapping; } /** * @param array<int|string, mixed> $mapping */ private function getIdentifierTypeFromMappings(array $mapping) : string { $callableGetType = static function ($value) : string { return \gettype($value); }; $valueTypes = \array_map($callableGetType, $mapping); $uniqueValueTypes = \array_unique($valueTypes); if (\count($uniqueValueTypes) === 1) { $identifierType = \reset($uniqueValueTypes); if ($identifierType === 'integer') { $identifierType = 'int'; } } else { $identifierType = 'string'; } return $identifierType; } } PK �,%\����� � # NodeAnalyzer/ComplexNewAnalyzer.phpnu �[��� <?php declare (strict_types=1); namespace Rector\Php81\NodeAnalyzer; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\ArrayItem; use PhpParser\Node\Expr\New_; use PhpParser\Node\Name\FullyQualified; use Rector\NodeAnalyzer\ExprAnalyzer; final class ComplexNewAnalyzer { /** * @readonly * @var \Rector\NodeAnalyzer\ExprAnalyzer */ private $exprAnalyzer; public function __construct(ExprAnalyzer $exprAnalyzer) { $this->exprAnalyzer = $exprAnalyzer; } public function isDynamic(New_ $new) : bool { if (!$new->class instanceof FullyQualified) { return \true; } if ($new->isFirstClassCallable()) { return \false; } $args = $new->getArgs(); foreach ($args as $arg) { $value = $arg->value; if ($this->isAllowedNew($value)) { continue; } // new inside array is allowed for New in initializer if ($value instanceof Array_ && $this->isAllowedArray($value)) { continue; } if (!$this->exprAnalyzer->isDynamicExpr($value)) { continue; } return \true; } return \false; } private function isAllowedNew(Expr $expr) : bool { if ($expr instanceof New_) { return !$this->isDynamic($expr); } return \false; } private function isAllowedArray(Array_ $array) : bool { if (!$this->exprAnalyzer->isDynamicArray($array)) { return \true; } $arrayItems = $array->items; foreach ($arrayItems as $arrayItem) { if (!$arrayItem instanceof ArrayItem) { continue; } if (!$arrayItem->value instanceof New_) { return \false; } if ($this->isDynamic($arrayItem->value)) { return \false; } } return \true; } } PK �,%\���3 3 - NodeAnalyzer/CoalesePropertyAssignMatcher.phpnu �[��� <?php declare (strict_types=1); namespace Rector\Php81\NodeAnalyzer; use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\BinaryOp\Coalesce; use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use Rector\NodeNameResolver\NodeNameResolver; final class CoalesePropertyAssignMatcher { /** * @readonly * @var \Rector\Php81\NodeAnalyzer\ComplexNewAnalyzer */ private $complexNewAnalyzer; /** * @readonly * @var \Rector\NodeNameResolver\NodeNameResolver */ private $nodeNameResolver; public function __construct(\Rector\Php81\NodeAnalyzer\ComplexNewAnalyzer $complexNewAnalyzer, NodeNameResolver $nodeNameResolver) { $this->complexNewAnalyzer = $complexNewAnalyzer; $this->nodeNameResolver = $nodeNameResolver; } /** * Matches * * $this->value = $param ?? 'default'; */ public function matchCoalesceAssignsToLocalPropertyNamed(Stmt $stmt, string $propertyName) : ?Coalesce { if (!$stmt instanceof Expression) { return null; } if (!$stmt->expr instanceof Assign) { return null; } $assign = $stmt->expr; if (!$assign->expr instanceof Coalesce) { return null; } $coalesce = $assign->expr; if (!$coalesce->right instanceof New_) { return null; } if ($this->complexNewAnalyzer->isDynamic($coalesce->right)) { return null; } if (!$this->isLocalPropertyFetchNamed($assign->var, $propertyName)) { return null; } return $assign->expr; } private function isLocalPropertyFetchNamed(Expr $expr, string $propertyName) : bool { if (!$expr instanceof PropertyFetch) { return \false; } if (!$this->nodeNameResolver->isName($expr->var, 'this')) { return \false; } return $this->nodeNameResolver->isName($expr->name, $propertyName); } } PK �,%\3���� � - Rector/Class_/SpatieEnumClassToEnumRector.phpnu �[��� <?php declare (strict_types=1); namespace Rector\Php81\Rector\Class_; 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; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\Php81\Rector\Class_\SpatieEnumClassToEnumRector\SpatieEnumClassToEnumRectorTest */ final class SpatieEnumClassToEnumRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface { /** * @readonly * @var \Rector\Php81\NodeFactory\EnumFactory */ private $enumFactory; /** * @var string */ public const TO_UPPER_SNAKE_CASE = 'toUpperSnakeCase'; /** * @var bool */ private $toUpperSnakeCase = \false; public function __construct(EnumFactory $enumFactory) { $this->enumFactory = $enumFactory; } public function provideMinPhpVersion() : int { return PhpVersionFeature::ENUM; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Refactor Spatie enum class to native Enum', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' use \Spatie\Enum\Enum; /** * @method static self draft() * @method static self published() * @method static self archived() */ class StatusEnum extends Enum { } CODE_SAMPLE , <<<'CODE_SAMPLE' enum StatusEnum : string { case DRAFT = 'draft'; case PUBLISHED = 'published'; case ARCHIVED = 'archived'; } CODE_SAMPLE , [self::TO_UPPER_SNAKE_CASE => \false])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [Class_::class]; } /** * @param Class_ $node */ public function refactor(Node $node) : ?Enum_ { if (!$this->isObjectType($node, new ObjectType('Spatie\\Enum\\Enum'))) { return null; } return $this->enumFactory->createFromSpatieClass($node, $this->toUpperSnakeCase); } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { $this->toUpperSnakeCase = $configuration[self::TO_UPPER_SNAKE_CASE] ?? \false; } } PK �,%\�!��' ' * Rector/Class_/MyCLabsClassToEnumRector.phpnu �[��� <?php declare (strict_types=1); namespace Rector\Php81\Rector\Class_; use PhpParser\Node; use PhpParser\Node\Stmt\Class_; use PHPStan\Type\ObjectType; use Rector\Php81\NodeFactory\EnumFactory; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\Php81\Rector\Class_\MyCLabsClassToEnumRector\MyCLabsClassToEnumRectorTest */ final class MyCLabsClassToEnumRector extends AbstractRector implements MinPhpVersionInterface { /** * @readonly * @var \Rector\Php81\NodeFactory\EnumFactory */ private $enumFactory; public function __construct(EnumFactory $enumFactory) { $this->enumFactory = $enumFactory; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Refactor MyCLabs enum class to native Enum', [new CodeSample(<<<'CODE_SAMPLE' use MyCLabs\Enum\Enum; final class Action extends Enum { private const VIEW = 'view'; private const EDIT = 'edit'; } CODE_SAMPLE , <<<'CODE_SAMPLE' enum Action : string { case VIEW = 'view'; case EDIT = 'edit'; } CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [Class_::class]; } /** * @param Class_ $node */ public function refactor(Node $node) : ?Node { if (!$this->isObjectType($node, new ObjectType('MyCLabs\\Enum\\Enum'))) { return null; } return $this->enumFactory->createFromClass($node); } public function provideMinPhpVersion() : int { return PhpVersionFeature::ENUM; } } PK �,%\�?Q# # 8 Rector/MethodCall/MyCLabsMethodCallToEnumConstRector.phpnu �[��� <?php declare (strict_types=1); namespace Rector\Php81\Rector\MethodCall; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\Identical; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\ObjectType; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\MyCLabsMethodCallToEnumConstRectorTest */ final class MyCLabsMethodCallToEnumConstRector extends AbstractRector implements MinPhpVersionInterface { /** * @readonly * @var \PHPStan\Reflection\ReflectionProvider */ private $reflectionProvider; /** * @var string[] */ private const ENUM_METHODS = ['from', 'values', 'keys', 'isValid', 'search', 'toArray', 'assertValidValue']; public function __construct(ReflectionProvider $reflectionProvider) { $this->reflectionProvider = $reflectionProvider; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Refactor MyCLabs enum fetch to Enum const', [new CodeSample(<<<'CODE_SAMPLE' $name = SomeEnum::VALUE()->getKey(); CODE_SAMPLE , <<<'CODE_SAMPLE' $name = SomeEnum::VALUE; CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [MethodCall::class, StaticCall::class]; } /** * @param MethodCall|StaticCall $node */ public function refactor(Node $node) : ?Node { if ($node->name instanceof Expr) { return null; } $enumCaseName = $this->getName($node->name); if ($enumCaseName === null) { return null; } if ($this->shouldOmitEnumCase($enumCaseName)) { return null; } if ($node instanceof MethodCall) { return $this->refactorMethodCall($node, $enumCaseName); } if (!$this->isObjectType($node->class, new ObjectType('MyCLabs\\Enum\\Enum'))) { return null; } $className = $this->getName($node->class); if (!\is_string($className)) { return null; } if (!$this->isEnumConstant($className, $enumCaseName)) { return null; } return $this->nodeFactory->createClassConstFetch($className, $enumCaseName); } public function provideMinPhpVersion() : int { return PhpVersionFeature::ENUM; } private function isEnumConstant(string $className, string $constant) : bool { $classReflection = $this->reflectionProvider->getClass($className); return $classReflection->hasConstant($constant); } private function refactorGetKeyMethodCall(MethodCall $methodCall) : ?ClassConstFetch { if (!$methodCall->var instanceof StaticCall) { return null; } $staticCall = $methodCall->var; $className = $this->getName($staticCall->class); if ($className === null) { return null; } $enumCaseName = $this->getName($staticCall->name); if ($enumCaseName === null) { return null; } if ($this->shouldOmitEnumCase($enumCaseName)) { return null; } return $this->nodeFactory->createClassConstFetch($className, $enumCaseName); } private function refactorGetValueMethodCall(MethodCall $methodCall) : ?PropertyFetch { if (!$methodCall->var instanceof StaticCall) { return null; } $staticCall = $methodCall->var; $className = $this->getName($staticCall->class); if ($className === null) { return null; } $enumCaseName = $this->getName($staticCall->name); if ($enumCaseName === null) { return null; } if ($this->shouldOmitEnumCase($enumCaseName)) { return null; } $classConstFetch = $this->nodeFactory->createClassConstFetch($className, $enumCaseName); return new PropertyFetch($classConstFetch, 'value'); } private function refactorEqualsMethodCall(MethodCall $methodCall) : ?Identical { $expr = $this->getNonEnumReturnTypeExpr($methodCall->var); if (!$expr instanceof Expr) { $expr = $this->getValidEnumExpr($methodCall->var); if (!$expr instanceof Expr) { return null; } } $arg = $methodCall->getArgs()[0] ?? null; if (!$arg instanceof Arg) { return null; } $right = $this->getNonEnumReturnTypeExpr($arg->value); if (!$right instanceof Expr) { $right = $this->getValidEnumExpr($arg->value); if (!$right instanceof Expr) { return null; } } return new Identical($expr, $right); } /** * @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $node */ private function isCallerClassEnum($node) : bool { if ($node instanceof StaticCall) { return $this->isObjectType($node->class, new ObjectType('MyCLabs\\Enum\\Enum')); } return $this->isObjectType($node->var, new ObjectType('MyCLabs\\Enum\\Enum')); } /** * @return null|\PhpParser\Node\Expr\ClassConstFetch|\PhpParser\Node\Expr */ private function getNonEnumReturnTypeExpr(Node $node) { if (!$node instanceof StaticCall && !$node instanceof MethodCall) { return null; } if ($this->isCallerClassEnum($node)) { $methodName = $this->getName($node->name); if ($methodName === null) { return null; } if ($node instanceof StaticCall) { $className = $this->getName($node->class); } if ($node instanceof MethodCall) { $className = $this->getName($node->var); } if ($className === null) { return null; } $classReflection = $this->reflectionProvider->getClass($className); // method self::getValidEnumExpr process enum static methods from constants if ($classReflection->hasConstant($methodName)) { return null; } } return $node; } /** * @return null|\PhpParser\Node\Expr\ClassConstFetch|\PhpParser\Node\Expr */ private function getValidEnumExpr(Node $node) { switch (\get_class($node)) { case Variable::class: case PropertyFetch::class: return $this->getPropertyFetchOrVariable($node); case StaticCall::class: return $this->getEnumConstFetch($node); default: return null; } } /** * @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\Variable $expr * @return null|\PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\Variable */ private function getPropertyFetchOrVariable($expr) { if (!$this->isObjectType($expr, new ObjectType('MyCLabs\\Enum\\Enum'))) { return null; } return $expr; } private function getEnumConstFetch(StaticCall $staticCall) : ?\PhpParser\Node\Expr\ClassConstFetch { $className = $this->getName($staticCall->class); if ($className === null) { return null; } $enumCaseName = $this->getName($staticCall->name); if ($enumCaseName === null) { return null; } if ($this->shouldOmitEnumCase($enumCaseName)) { return null; } return $this->nodeFactory->createClassConstFetch($className, $enumCaseName); } /** * @return null|\PhpParser\Node\Expr\ClassConstFetch|\PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\BinaryOp\Identical */ private function refactorMethodCall(MethodCall $methodCall, string $methodName) { if (!$this->isObjectType($methodCall->var, new ObjectType('MyCLabs\\Enum\\Enum'))) { return null; } if ($methodName === 'getKey') { return $this->refactorGetKeyMethodCall($methodCall); } if ($methodName === 'getValue') { return $this->refactorGetValueMethodCall($methodCall); } if ($methodName === 'equals') { return $this->refactorEqualsMethodCall($methodCall); } return null; } private function shouldOmitEnumCase(string $enumCaseName) : bool { return \in_array($enumCaseName, self::ENUM_METHODS, \true); } } PK �,%\�l� � ; Rector/MethodCall/SpatieEnumMethodCallToEnumConstRector.phpnu �[��� <?php declare (strict_types=1); namespace Rector\Php81\Rector\MethodCall; use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; use PHPStan\Type\ObjectType; use Rector\Rector\AbstractRector; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector\MyCLabsMethodCallToEnumConstRectorTest */ final class SpatieEnumMethodCallToEnumConstRector extends AbstractRector implements MinPhpVersionInterface { /** * @var string */ private const SPATIE_FQN = 'Spatie\\Enum\\Enum'; /** * @var string[] */ private const ENUM_METHODS = ['from', 'values', 'keys', 'isValid', 'search', 'toArray', 'assertValidValue']; public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Refactor Spatie enum method calls', [new CodeSample(<<<'CODE_SAMPLE' $value1 = SomeEnum::SOME_CONSTANT()->getValue(); $value2 = SomeEnum::SOME_CONSTANT()->value; $name1 = SomeEnum::SOME_CONSTANT()->getName(); $name2 = SomeEnum::SOME_CONSTANT()->name; CODE_SAMPLE , <<<'CODE_SAMPLE' $value1 = SomeEnum::SOME_CONSTANT->value; $value2 = SomeEnum::SOME_CONSTANT->value; $name1 = SomeEnum::SOME_CONSTANT->name; $name2 = SomeEnum::SOME_CONSTANT->name; CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [MethodCall::class, StaticCall::class]; } /** * @param MethodCall|StaticCall $node */ public function refactor(Node $node) : ?Node { if ($node->name instanceof Expr) { return null; } $enumCaseName = $this->getName($node->name); if ($enumCaseName === null) { return null; } if ($this->shouldOmitEnumCase($enumCaseName)) { return null; } if ($node instanceof MethodCall) { return $this->refactorMethodCall($node, $enumCaseName); } if (!$this->isObjectType($node->class, new ObjectType(self::SPATIE_FQN))) { return null; } $className = $this->getName($node->class); if (!\is_string($className)) { return null; } $constantName = \strtoupper($enumCaseName); return $this->nodeFactory->createClassConstFetch($className, $constantName); } public function provideMinPhpVersion() : int { return PhpVersionFeature::ENUM; } private function refactorGetterToMethodCall(MethodCall $methodCall, string $property) : ?PropertyFetch { if (!$methodCall->var instanceof StaticCall) { return null; } $staticCall = $methodCall->var; $className = $this->getName($staticCall->class); if ($className === null) { return null; } $enumCaseName = $this->getName($staticCall->name); if ($enumCaseName === null) { return null; } if ($this->shouldOmitEnumCase($enumCaseName)) { return null; } $upperCaseName = \strtoupper($enumCaseName); $classConstFetch = $this->nodeFactory->createClassConstFetch($className, $upperCaseName); return new PropertyFetch($classConstFetch, $property); } private function refactorMethodCall(MethodCall $methodCall, string $methodName) : ?\PhpParser\Node\Expr\PropertyFetch { if (!$this->isObjectType($methodCall->var, new ObjectType(self::SPATIE_FQN))) { return null; } if ($methodName === 'getName') { return $this->refactorGetterToMethodCall($methodCall, 'name'); } if ($methodName === 'label') { return $this->refactorGetterToMethodCall($methodCall, 'name'); } if ($methodName === 'getValue') { return $this->refactorGetterToMethodCall($methodCall, 'value'); } if ($methodName === 'value') { return $this->refactorGetterToMethodCall($methodCall, 'value'); } return null; } private function shouldOmitEnumCase(string $enumCaseName) : bool { return \in_array($enumCaseName, self::ENUM_METHODS, \true); } } PK �,%\�0�� � - Rector/ClassMethod/NewInInitializerRector.phpnu �[��� <?php declare (strict_types=1); namespace Rector\Php81\Rector\ClassMethod; use PhpParser\Node; use PhpParser\Node\Expr\BinaryOp\Coalesce; use PhpParser\Node\NullableType; use PhpParser\Node\Param; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; use PHPStan\Reflection\ClassReflection; use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer; use Rector\NodeManipulator\StmtsManipulator; use Rector\Php81\NodeAnalyzer\CoalesePropertyAssignMatcher; use Rector\Rector\AbstractRector; use Rector\Reflection\ReflectionResolver; use Rector\ValueObject\MethodName; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\NewInInitializerRectorTest */ final class NewInInitializerRector extends AbstractRector implements MinPhpVersionInterface { /** * @readonly * @var \Rector\Reflection\ReflectionResolver */ private $reflectionResolver; /** * @readonly * @var \Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer */ private $classChildAnalyzer; /** * @readonly * @var \Rector\Php81\NodeAnalyzer\CoalesePropertyAssignMatcher */ private $coalesePropertyAssignMatcher; /** * @readonly * @var \Rector\NodeManipulator\StmtsManipulator */ private $stmtsManipulator; public function __construct(ReflectionResolver $reflectionResolver, ClassChildAnalyzer $classChildAnalyzer, CoalesePropertyAssignMatcher $coalesePropertyAssignMatcher, StmtsManipulator $stmtsManipulator) { $this->reflectionResolver = $reflectionResolver; $this->classChildAnalyzer = $classChildAnalyzer; $this->coalesePropertyAssignMatcher = $coalesePropertyAssignMatcher; $this->stmtsManipulator = $stmtsManipulator; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Replace property declaration of new state with direct new', [new CodeSample(<<<'CODE_SAMPLE' class SomeClass { private Logger $logger; public function __construct( ?Logger $logger = null, ) { $this->logger = $logger ?? new NullLogger; } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function __construct( private Logger $logger = new NullLogger, ) { } } CODE_SAMPLE )]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [Class_::class]; } /** * @param Class_ $node */ public function refactor(Node $node) : ?Node { if ($node->stmts === null || $node->stmts === []) { return null; } if ($node->isAbstract() || $node->isAnonymous()) { return null; } $constructClassMethod = $node->getMethod(MethodName::CONSTRUCT); if (!$constructClassMethod instanceof ClassMethod) { return null; } $params = $this->resolveParams($constructClassMethod); if ($params === []) { return null; } $hasChanged = \false; // stmts variable defined to avoid unset overlap when used via array_slice() on // StmtsManipulator::isVariableUsedInNextStmt() // @see https://github.com/rectorphp/rector-src/pull/5968 // @see https://3v4l.org/eojhk $stmts = (array) $constructClassMethod->stmts; foreach ((array) $constructClassMethod->stmts as $key => $stmt) { foreach ($params as $param) { $paramName = $this->getName($param); $coalesce = $this->coalesePropertyAssignMatcher->matchCoalesceAssignsToLocalPropertyNamed($stmt, $paramName); if (!$coalesce instanceof Coalesce) { continue; } if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmts, $key + 1, $paramName)) { continue; } /** @var NullableType $currentParamType */ $currentParamType = $param->type; $param->type = $currentParamType->type; $param->default = $coalesce->right; unset($constructClassMethod->stmts[$key]); $this->processPropertyPromotion($node, $param, $paramName); $hasChanged = \true; } } if ($hasChanged) { return $node; } return null; } public function provideMinPhpVersion() : int { return PhpVersionFeature::NEW_INITIALIZERS; } /** * @return Param[] */ private function resolveParams(ClassMethod $classMethod) : array { $params = $this->matchConstructorParams($classMethod); if ($params === []) { return []; } if ($this->isOverrideAbstractMethod($classMethod)) { return []; } return $params; } private function isOverrideAbstractMethod(ClassMethod $classMethod) : bool { $classReflection = $this->reflectionResolver->resolveClassReflection($classMethod); $methodName = $this->nodeNameResolver->getName($classMethod); return $classReflection instanceof ClassReflection && $this->classChildAnalyzer->hasAbstractParentClassMethod($classReflection, $methodName); } private function processPropertyPromotion(Class_ $class, Param $param, string $paramName) : void { foreach ($class->stmts as $key => $stmt) { if (!$stmt instanceof Property) { continue; } $property = $stmt; if (!$this->isName($stmt, $paramName)) { continue; } $param->flags = $property->flags; $param->attrGroups = \array_merge($property->attrGroups, $param->attrGroups); unset($class->stmts[$key]); } } /** * @return Param[] */ private function matchConstructorParams(ClassMethod $classMethod) : array { // skip empty constructor assigns, as we need those here if ($classMethod->stmts === null || $classMethod->stmts === []) { return []; } return \array_filter($classMethod->params, static function (Param $param) : bool { return $param->type instanceof NullableType; }); } } PK �,%\-l_o&