?????????? ????????? - ??????????????? - /home/agenciai/public_html/cd38d8/Renaming.tar
???????
NodeManipulator/SwitchManipulator.php 0000644 00000001355 15126447325 0014050 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\NodeManipulator; use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Break_; final class SwitchManipulator { /** * @param Stmt[] $stmts * @return Stmt[] */ public function removeBreakNodes(array $stmts) : array { foreach ($stmts as $key => $node) { if (!$node instanceof Break_) { continue; } if (!$node->num instanceof LNumber || $node->num->value === 1) { unset($stmts[$key]); continue; } $node->num = $node->num->value === 2 ? null : new LNumber($node->num->value - 1); } return $stmts; } } NodeManipulator/ClassRenamer.php 0000644 00000021705 15126447325 0012753 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\NodeManipulator; use PhpParser\Node; use PhpParser\Node\AttributeGroup; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassLike; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Type\ObjectType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocClassRenamer; use Rector\BetterPhpDocParser\ValueObject\NodeTypes; use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockClassRenamer; use Rector\NodeTypeResolver\ValueObject\OldToNewType; use Rector\Renaming\Collector\RenamedNameCollector; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; use Rector\Util\FileHasher; final class ClassRenamer { /** * @readonly * @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocClassRenamer */ private $phpDocClassRenamer; /** * @readonly * @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory */ private $phpDocInfoFactory; /** * @readonly * @var \Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockClassRenamer */ private $docBlockClassRenamer; /** * @readonly * @var \PHPStan\Reflection\ReflectionProvider */ private $reflectionProvider; /** * @readonly * @var \Rector\Util\FileHasher */ private $fileHasher; /** * @readonly * @var \Rector\Comments\NodeDocBlock\DocBlockUpdater */ private $docBlockUpdater; /** * @readonly * @var \Rector\Renaming\Collector\RenamedNameCollector */ private $renamedNameCollector; /** * @var array<string, OldToNewType[]> */ private $oldToNewTypesByCacheKey = []; public function __construct(PhpDocClassRenamer $phpDocClassRenamer, PhpDocInfoFactory $phpDocInfoFactory, DocBlockClassRenamer $docBlockClassRenamer, ReflectionProvider $reflectionProvider, FileHasher $fileHasher, DocBlockUpdater $docBlockUpdater, RenamedNameCollector $renamedNameCollector) { $this->phpDocClassRenamer = $phpDocClassRenamer; $this->phpDocInfoFactory = $phpDocInfoFactory; $this->docBlockClassRenamer = $docBlockClassRenamer; $this->reflectionProvider = $reflectionProvider; $this->fileHasher = $fileHasher; $this->docBlockUpdater = $docBlockUpdater; $this->renamedNameCollector = $renamedNameCollector; } /** * @param array<string, string> $oldToNewClasses * @return ($node is FullyQualified ? FullyQualified : Node) */ public function renameNode(Node $node, array $oldToNewClasses, ?Scope $scope) : ?Node { $oldToNewTypes = $this->createOldToNewTypes($oldToNewClasses); if ($node instanceof FullyQualified) { return $this->refactorName($node, $oldToNewClasses); } $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); if ($phpDocInfo instanceof PhpDocInfo) { $hasPhpDocChanged = $this->refactorPhpDoc($node, $oldToNewTypes, $oldToNewClasses, $phpDocInfo); if ($hasPhpDocChanged) { return $node; } } if ($node instanceof ClassLike) { return $this->refactorClassLike($node, $oldToNewClasses, $scope); } return null; } /** * @param OldToNewType[] $oldToNewTypes * @param array<string, string> $oldToNewClasses */ private function refactorPhpDoc(Node $node, array $oldToNewTypes, array $oldToNewClasses, PhpDocInfo $phpDocInfo) : bool { if (!$phpDocInfo->hasByTypes(NodeTypes::TYPE_AWARE_NODES) && !$phpDocInfo->hasByAnnotationClasses(NodeTypes::TYPE_AWARE_DOCTRINE_ANNOTATION_CLASSES)) { return \false; } if ($node instanceof AttributeGroup) { return \false; } $hasChanged = $this->docBlockClassRenamer->renamePhpDocType($phpDocInfo, $oldToNewTypes, $node); $hasChanged = $this->phpDocClassRenamer->changeTypeInAnnotationTypes($node, $phpDocInfo, $oldToNewClasses, $hasChanged); if ($hasChanged) { $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); return \true; } return \false; } private function shouldSkip(string $newName, FullyQualified $fullyQualified) : bool { if ($fullyQualified->getAttribute(AttributeKey::IS_STATICCALL_CLASS_NAME) === \true && $this->reflectionProvider->hasClass($newName)) { $classReflection = $this->reflectionProvider->getClass($newName); return $classReflection->isInterface(); } return \false; } /** * @param array<string, string> $oldToNewClasses */ private function refactorName(FullyQualified $fullyQualified, array $oldToNewClasses) : ?FullyQualified { if ($fullyQualified->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === \true) { return null; } $stringName = $fullyQualified->toString(); $newName = $oldToNewClasses[$stringName] ?? null; if ($newName === null) { return null; } if (!$this->isClassToInterfaceValidChange($fullyQualified, $newName)) { return null; } if ($this->shouldSkip($newName, $fullyQualified)) { return null; } $this->renamedNameCollector->add($stringName); return new FullyQualified($newName); } /** * @param array<string, string> $oldToNewClasses */ private function refactorClassLike(ClassLike $classLike, array $oldToNewClasses, ?Scope $scope) : ?Node { // rename interfaces if (!$classLike instanceof Class_) { return null; } $hasChanged = \false; $classLike->implements = \array_unique($classLike->implements); foreach ($classLike->implements as $key => $implementName) { $namespaceName = $scope instanceof Scope ? $scope->getNamespace() : null; $fullyQualifiedName = $namespaceName . '\\' . $implementName->toString(); $newName = $oldToNewClasses[$fullyQualifiedName] ?? null; if ($newName === null) { continue; } $classLike->implements[$key] = new FullyQualified($newName); $hasChanged = \true; } if ($hasChanged) { return $classLike; } return null; } /** * Checks validity: * * - extends SomeClass * - extends SomeInterface * * - new SomeClass * - new SomeInterface * * - implements SomeInterface * - implements SomeClass */ private function isClassToInterfaceValidChange(FullyQualified $fullyQualified, string $newClassName) : bool { if (!$this->reflectionProvider->hasClass($newClassName)) { return \true; } $classReflection = $this->reflectionProvider->getClass($newClassName); // ensure new is not with interface if ($fullyQualified->getAttribute(AttributeKey::IS_NEW_INSTANCE_NAME) !== \true) { return $this->isValidClassNameChange($fullyQualified, $classReflection); } if (!$classReflection->isInterface()) { return $this->isValidClassNameChange($fullyQualified, $classReflection); } return \false; } private function isValidClassNameChange(FullyQualified $fullyQualified, ClassReflection $classReflection) : bool { if ($fullyQualified->getAttribute(AttributeKey::IS_CLASS_EXTENDS) === \true) { // is class to interface? if ($classReflection->isInterface()) { return \false; } if ($classReflection->isFinalByKeyword()) { return \false; } } if ($fullyQualified->getAttribute(AttributeKey::IS_CLASS_IMPLEMENT) === \true) { // is interface to class? return !$classReflection->isClass(); } return \true; } /** * @param array<string, string> $oldToNewClasses * @return OldToNewType[] */ private function createOldToNewTypes(array $oldToNewClasses) : array { $serialized = \serialize($oldToNewClasses); $cacheKey = $this->fileHasher->hash($serialized); if (isset($this->oldToNewTypesByCacheKey[$cacheKey])) { return $this->oldToNewTypesByCacheKey[$cacheKey]; } $oldToNewTypes = []; foreach ($oldToNewClasses as $oldClass => $newClass) { $oldObjectType = new ObjectType($oldClass); $newObjectType = new FullyQualifiedObjectType($newClass); $oldToNewTypes[] = new OldToNewType($oldObjectType, $newObjectType); } $this->oldToNewTypesByCacheKey[$cacheKey] = $oldToNewTypes; return $oldToNewTypes; } } Contract/RenameClassConstFetchInterface.php 0000644 00000000443 15126447325 0015043 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Contract; use PHPStan\Type\ObjectType; interface RenameClassConstFetchInterface { public function getOldObjectType() : ObjectType; public function getOldConstant() : string; public function getNewConstant() : string; } Contract/MethodCallRenameInterface.php 0000644 00000000500 15126447325 0014023 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Contract; use PHPStan\Type\ObjectType; interface MethodCallRenameInterface { public function getClass() : string; public function getObjectType() : ObjectType; public function getOldMethod() : string; public function getNewMethod() : string; } Contract/RenameAnnotationInterface.php 0000644 00000000320 15126447325 0014121 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Contract; interface RenameAnnotationInterface { public function getOldAnnotation() : string; public function getNewAnnotation() : string; } Collector/RenamedNameCollector.php 0000644 00000000674 15126447325 0013246 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Collector; final class RenamedNameCollector { /** * @var string[] */ private $names = []; public function add(string $name) : void { $this->names[] = $name; } public function has(string $name) : bool { return \in_array($name, $this->names, \true); } public function reset() : void { $this->names = []; } } ValueObject/RenameStaticMethod.php 0000644 00000002603 15126447325 0013212 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; use PHPStan\Type\ObjectType; use Rector\Validation\RectorAssert; final class RenameStaticMethod { /** * @readonly * @var string */ private $oldClass; /** * @readonly * @var string */ private $oldMethod; /** * @readonly * @var string */ private $newClass; /** * @readonly * @var string */ private $newMethod; public function __construct(string $oldClass, string $oldMethod, string $newClass, string $newMethod) { $this->oldClass = $oldClass; $this->oldMethod = $oldMethod; $this->newClass = $newClass; $this->newMethod = $newMethod; RectorAssert::className($oldClass); RectorAssert::methodName($oldMethod); RectorAssert::className($newClass); RectorAssert::methodName($newMethod); } public function getOldObjectType() : ObjectType { return new ObjectType($this->oldClass); } public function getOldMethod() : string { return $this->oldMethod; } public function getNewClass() : string { return $this->newClass; } public function getNewMethod() : string { return $this->newMethod; } public function hasClassChanged() : bool { return $this->oldClass !== $this->newClass; } } ValueObject/RenameProperty.php 0000644 00000002022 15126447325 0012441 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; use PHPStan\Type\ObjectType; use Rector\Validation\RectorAssert; final class RenameProperty { /** * @readonly * @var string */ private $type; /** * @readonly * @var string */ private $oldProperty; /** * @readonly * @var string */ private $newProperty; public function __construct(string $type, string $oldProperty, string $newProperty) { $this->type = $type; $this->oldProperty = $oldProperty; $this->newProperty = $newProperty; RectorAssert::className($type); RectorAssert::propertyName($oldProperty); RectorAssert::propertyName($newProperty); } public function getObjectType() : ObjectType { return new ObjectType($this->type); } public function getOldProperty() : string { return $this->oldProperty; } public function getNewProperty() : string { return $this->newProperty; } } ValueObject/MethodCallRenameWithArrayKey.php 0000644 00000002717 15126447325 0015150 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; use PHPStan\Type\ObjectType; use Rector\Renaming\Contract\MethodCallRenameInterface; use Rector\Validation\RectorAssert; final class MethodCallRenameWithArrayKey implements MethodCallRenameInterface { /** * @readonly * @var string */ private $class; /** * @readonly * @var string */ private $oldMethod; /** * @readonly * @var string */ private $newMethod; /** * @readonly * @var mixed */ private $arrayKey; /** * @param mixed $arrayKey */ public function __construct(string $class, string $oldMethod, string $newMethod, $arrayKey) { $this->class = $class; $this->oldMethod = $oldMethod; $this->newMethod = $newMethod; $this->arrayKey = $arrayKey; RectorAssert::className($class); RectorAssert::methodName($oldMethod); RectorAssert::methodName($newMethod); } public function getClass() : string { return $this->class; } public function getObjectType() : ObjectType { return new ObjectType($this->class); } public function getOldMethod() : string { return $this->oldMethod; } public function getNewMethod() : string { return $this->newMethod; } /** * @return mixed */ public function getArrayKey() { return $this->arrayKey; } } ValueObject/RenameClassAndConstFetch.php 0000644 00000002643 15126447325 0014277 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; use PHPStan\Type\ObjectType; use Rector\Renaming\Contract\RenameClassConstFetchInterface; use Rector\Validation\RectorAssert; final class RenameClassAndConstFetch implements RenameClassConstFetchInterface { /** * @readonly * @var string */ private $oldClass; /** * @readonly * @var string */ private $oldConstant; /** * @readonly * @var string */ private $newClass; /** * @readonly * @var string */ private $newConstant; public function __construct(string $oldClass, string $oldConstant, string $newClass, string $newConstant) { $this->oldClass = $oldClass; $this->oldConstant = $oldConstant; $this->newClass = $newClass; $this->newConstant = $newConstant; RectorAssert::className($oldClass); RectorAssert::constantName($oldConstant); RectorAssert::className($newClass); RectorAssert::constantName($newConstant); } public function getOldObjectType() : ObjectType { return new ObjectType($this->oldClass); } public function getOldConstant() : string { return $this->oldConstant; } public function getNewConstant() : string { return $this->newConstant; } public function getNewClass() : string { return $this->newClass; } } ValueObject/RenameFunctionLikeParamWithinCallLikeArg.php 0000644 00000003333 15126447325 0017414 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; use PHPStan\Type\ObjectType; use Rector\Validation\RectorAssert; final class RenameFunctionLikeParamWithinCallLikeArg { /** * @readonly * @var string */ private $className; /** * @readonly * @var string */ private $methodName; /** * @var int<0, max>|string * @readonly */ private $callLikePosition; /** * @var int<0, max> * @readonly */ private $functionLikePosition; /** * @readonly * @var string */ private $newParamName; /** * @param int<0, max>|string $callLikePosition * @param int<0, max> $functionLikePosition */ public function __construct(string $className, string $methodName, $callLikePosition, int $functionLikePosition, string $newParamName) { $this->className = $className; $this->methodName = $methodName; $this->callLikePosition = $callLikePosition; $this->functionLikePosition = $functionLikePosition; $this->newParamName = $newParamName; RectorAssert::className($className); } public function getObjectType() : ObjectType { return new ObjectType($this->className); } public function getMethodName() : string { return $this->methodName; } /** * @return int<0, max>|string */ public function getCallLikePosition() { return $this->callLikePosition; } /** * @return int<0, max> */ public function getFunctionLikePosition() : int { return $this->functionLikePosition; } public function getNewParamName() : string { return $this->newParamName; } } ValueObject/RenameAttribute.php 0000644 00000001207 15126447325 0012564 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; /** * @api */ final class RenameAttribute { /** * @readonly * @var string */ private $oldAttribute; /** * @readonly * @var string */ private $newAttribute; public function __construct(string $oldAttribute, string $newAttribute) { $this->oldAttribute = $oldAttribute; $this->newAttribute = $newAttribute; } public function getOldAttribute() : string { return $this->oldAttribute; } public function getNewAttribute() : string { return $this->newAttribute; } } ValueObject/MethodCallRename.php 0000644 00000002250 15126447325 0012634 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; use PHPStan\Type\ObjectType; use Rector\Renaming\Contract\MethodCallRenameInterface; use Rector\Validation\RectorAssert; final class MethodCallRename implements MethodCallRenameInterface { /** * @readonly * @var string */ private $class; /** * @readonly * @var string */ private $oldMethod; /** * @readonly * @var string */ private $newMethod; public function __construct(string $class, string $oldMethod, string $newMethod) { $this->class = $class; $this->oldMethod = $oldMethod; $this->newMethod = $newMethod; RectorAssert::className($class); RectorAssert::methodName($oldMethod); RectorAssert::methodName($newMethod); } public function getClass() : string { return $this->class; } public function getObjectType() : ObjectType { return new ObjectType($this->class); } public function getOldMethod() : string { return $this->oldMethod; } public function getNewMethod() : string { return $this->newMethod; } } ValueObject/RenameAnnotation.php 0000644 00000001361 15126447325 0012734 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; use Rector\Renaming\Contract\RenameAnnotationInterface; /** * @api */ final class RenameAnnotation implements RenameAnnotationInterface { /** * @readonly * @var string */ private $oldAnnotation; /** * @readonly * @var string */ private $newAnnotation; public function __construct(string $oldAnnotation, string $newAnnotation) { $this->oldAnnotation = $oldAnnotation; $this->newAnnotation = $newAnnotation; } public function getOldAnnotation() : string { return $this->oldAnnotation; } public function getNewAnnotation() : string { return $this->newAnnotation; } } ValueObject/RenameAnnotationByType.php 0000644 00000002053 15126447325 0014070 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; use PHPStan\Type\ObjectType; use Rector\Renaming\Contract\RenameAnnotationInterface; use Rector\Validation\RectorAssert; final class RenameAnnotationByType implements RenameAnnotationInterface { /** * @readonly * @var string */ private $type; /** * @readonly * @var string */ private $oldAnnotation; /** * @readonly * @var string */ private $newAnnotation; public function __construct(string $type, string $oldAnnotation, string $newAnnotation) { $this->type = $type; $this->oldAnnotation = $oldAnnotation; $this->newAnnotation = $newAnnotation; RectorAssert::className($type); } public function getObjectType() : ObjectType { return new ObjectType($this->type); } public function getOldAnnotation() : string { return $this->oldAnnotation; } public function getNewAnnotation() : string { return $this->newAnnotation; } } ValueObject/RenameClassConstFetch.php 0000644 00000002233 15126447325 0013647 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\ValueObject; use PHPStan\Type\ObjectType; use Rector\Renaming\Contract\RenameClassConstFetchInterface; use Rector\Validation\RectorAssert; final class RenameClassConstFetch implements RenameClassConstFetchInterface { /** * @readonly * @var string */ private $oldClass; /** * @readonly * @var string */ private $oldConstant; /** * @readonly * @var string */ private $newConstant; public function __construct(string $oldClass, string $oldConstant, string $newConstant) { $this->oldClass = $oldClass; $this->oldConstant = $oldConstant; $this->newConstant = $newConstant; RectorAssert::className($oldClass); RectorAssert::constantName($oldConstant); RectorAssert::constantName($newConstant); } public function getOldObjectType() : ObjectType { return new ObjectType($this->oldClass); } public function getOldConstant() : string { return $this->oldConstant; } public function getNewConstant() : string { return $this->newConstant; } } Rector/StaticCall/RenameStaticMethodRector.php 0000644 00000004647 15126447325 0015461 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\StaticCall; use PhpParser\Node; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Identifier; use PhpParser\Node\Name\FullyQualified; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; use Rector\Renaming\ValueObject\RenameStaticMethod; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\StaticCall\RenameStaticMethodRector\RenameStaticMethodRectorTest */ final class RenameStaticMethodRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var RenameStaticMethod[] */ private $staticMethodRenames = []; public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Turns method names to new ones.', [new ConfiguredCodeSample('SomeClass::oldStaticMethod();', 'AnotherExampleClass::newStaticMethod();', [new RenameStaticMethod('SomeClass', 'oldMethod', 'AnotherExampleClass', 'newStaticMethod')])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [StaticCall::class]; } /** * @param StaticCall $node */ public function refactor(Node $node) : ?Node { foreach ($this->staticMethodRenames as $staticMethodRename) { if (!$this->isName($node->name, $staticMethodRename->getOldMethod())) { continue; } if (!$this->isObjectType($node->class, $staticMethodRename->getOldObjectType())) { continue; } return $this->rename($node, $staticMethodRename); } return null; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allIsAOf($configuration, RenameStaticMethod::class); $this->staticMethodRenames = $configuration; } private function rename(StaticCall $staticCall, RenameStaticMethod $renameStaticMethod) : StaticCall { $staticCall->name = new Identifier($renameStaticMethod->getNewMethod()); if ($renameStaticMethod->hasClassChanged()) { $staticCall->class = new FullyQualified($renameStaticMethod->getNewClass()); } return $staticCall; } } Rector/Class_/RenameAttributeRector.php 0000644 00000005462 15126447325 0014211 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\Class_; use PhpParser\Node; use PhpParser\Node\Attribute; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Property; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; use Rector\Renaming\ValueObject\RenameAttribute; use Rector\ValueObject\PhpVersionFeature; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\Class_\RenameAttributeRector\RenameAttributeRectorTest */ final class RenameAttributeRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface { /** * @var RenameAttribute[] */ private $renameAttributes = []; public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Rename attribute class names', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' #[SimpleRoute()] class SomeClass { } CODE_SAMPLE , <<<'CODE_SAMPLE' #[BasicRoute()] class SomeClass { } CODE_SAMPLE , [new RenameAttribute('SimpleRoute', 'BasicRoute')])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [Class_::class, ClassMethod::class, Property::class]; } /** * @param Class_|ClassMethod|Property $node */ public function refactor(Node $node) : ?Node { $hasChanged = \false; foreach ($node->attrGroups as $attrGroup) { foreach ($attrGroup->attrs as $attr) { $newAttributeName = $this->matchNewAttributeName($attr); if (!\is_string($newAttributeName)) { continue; } $attr->name = new FullyQualified($newAttributeName); $hasChanged = \true; } } if ($hasChanged) { return $node; } return null; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allIsAOf($configuration, RenameAttribute::class); $this->renameAttributes = $configuration; } public function provideMinPhpVersion() : int { return PhpVersionFeature::ATTRIBUTES; } private function matchNewAttributeName(Attribute $attribute) : ?string { foreach ($this->renameAttributes as $renameAttribute) { if ($this->isName($attribute->name, $renameAttribute->getOldAttribute())) { return $renameAttribute->getNewAttribute(); } } return null; } } Rector/PropertyFetch/RenamePropertyRector.php 0000644 00000007566 15126447325 0015473 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\PropertyFetch; use PhpParser\Node; use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Property; use PhpParser\Node\VarLikeIdentifier; use PHPStan\Type\ObjectType; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; use Rector\Renaming\ValueObject\RenameProperty; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\PropertyFetch\RenamePropertyRector\RenamePropertyRectorTest */ final class RenamePropertyRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var RenameProperty[] */ private $renamedProperties = []; /** * @var bool */ private $hasChanged = \false; public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Replaces defined old properties by new ones.', [new ConfiguredCodeSample('$someObject->someOldProperty;', '$someObject->someNewProperty;', [new RenameProperty('SomeClass', 'someOldProperty', 'someNewProperty')])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [PropertyFetch::class, ClassLike::class]; } /** * @param PropertyFetch|ClassLike $node */ public function refactor(Node $node) : ?Node { if ($node instanceof ClassLike) { $this->hasChanged = \false; foreach ($this->renamedProperties as $renamedProperty) { $this->renameProperty($node, $renamedProperty); } if ($this->hasChanged) { return $node; } return null; } return $this->refactorPropertyFetch($node); } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allIsAOf($configuration, RenameProperty::class); $this->renamedProperties = $configuration; } private function renameProperty(ClassLike $classLike, RenameProperty $renameProperty) : void { $classLikeName = (string) $this->nodeNameResolver->getName($classLike); $renamePropertyObjectType = $renameProperty->getObjectType(); $className = $renamePropertyObjectType->getClassName(); $classLikeNameObjectType = new ObjectType($classLikeName); $classNameObjectType = new ObjectType($className); $isSuperType = $classNameObjectType->isSuperTypeOf($classLikeNameObjectType)->yes(); if ($classLikeName !== $className && !$isSuperType) { return; } $property = $classLike->getProperty($renameProperty->getOldProperty()); if (!$property instanceof Property) { return; } $newProperty = $renameProperty->getNewProperty(); $targetNewProperty = $classLike->getProperty($newProperty); if ($targetNewProperty instanceof Property) { return; } $this->hasChanged = \true; $property->props[0]->name = new VarLikeIdentifier($newProperty); } private function refactorPropertyFetch(PropertyFetch $propertyFetch) : ?PropertyFetch { foreach ($this->renamedProperties as $renamedProperty) { $oldProperty = $renamedProperty->getOldProperty(); if (!$this->isName($propertyFetch, $oldProperty)) { continue; } if (!$this->isObjectType($propertyFetch->var, $renamedProperty->getObjectType())) { continue; } $propertyFetch->name = new Identifier($renamedProperty->getNewProperty()); return $propertyFetch; } return null; } } Rector/MethodCall/RenameMethodRector.php 0000644 00000020636 15126447325 0014276 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\MethodCall; use PhpParser\BuilderHelpers; use PhpParser\Node; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\NullsafeMethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Identifier; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Interface_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\ReflectionProvider; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeManipulator\ClassManipulator; use Rector\Rector\AbstractScopeAwareRector; use Rector\Reflection\ReflectionResolver; use Rector\Renaming\Contract\MethodCallRenameInterface; use Rector\Renaming\ValueObject\MethodCallRename; use Rector\Renaming\ValueObject\MethodCallRenameWithArrayKey; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\RenameMethodRectorTest */ final class RenameMethodRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface { /** * @readonly * @var \Rector\NodeManipulator\ClassManipulator */ private $classManipulator; /** * @readonly * @var \Rector\Reflection\ReflectionResolver */ private $reflectionResolver; /** * @readonly * @var \PHPStan\Reflection\ReflectionProvider */ private $reflectionProvider; /** * @var MethodCallRenameInterface[] */ private $methodCallRenames = []; public function __construct(ClassManipulator $classManipulator, ReflectionResolver $reflectionResolver, ReflectionProvider $reflectionProvider) { $this->classManipulator = $classManipulator; $this->reflectionResolver = $reflectionResolver; $this->reflectionProvider = $reflectionProvider; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Turns method names to new ones.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' $someObject = new SomeExampleClass; $someObject->oldMethod(); CODE_SAMPLE , <<<'CODE_SAMPLE' $someObject = new SomeExampleClass; $someObject->newMethod(); CODE_SAMPLE , [new MethodCallRename('SomeExampleClass', 'oldMethod', 'newMethod')])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [MethodCall::class, NullsafeMethodCall::class, StaticCall::class, Class_::class, Interface_::class]; } /** * @param MethodCall|NullsafeMethodCall|StaticCall|Class_|Interface_ $node */ public function refactorWithScope(Node $node, Scope $scope) : ?Node { if ($node instanceof Class_ || $node instanceof Interface_) { return $this->refactorClass($node, $scope); } return $this->refactorMethodCallAndStaticCall($node); } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allIsAOf($configuration, MethodCallRenameInterface::class); $this->methodCallRenames = $configuration; } /** * @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\NullsafeMethodCall|\PhpParser\Node\Expr\StaticCall $call */ private function shouldSkipClassMethod($call, MethodCallRenameInterface $methodCallRename) : bool { $classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($call); if (!$classReflection instanceof ClassReflection) { return \false; } $targetClass = $methodCallRename->getClass(); if (!$this->reflectionProvider->hasClass($targetClass)) { return \false; } $targetClassReflection = $this->reflectionProvider->getClass($targetClass); if ($classReflection->getName() === $targetClassReflection->getName()) { return \false; } // different with configured ClassLike source? it is a child, which may has old and new exists if (!$classReflection->hasMethod($methodCallRename->getOldMethod())) { return \false; } return $classReflection->hasMethod($methodCallRename->getNewMethod()); } /** * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface */ private function hasClassNewClassMethod($classOrInterface, MethodCallRenameInterface $methodCallRename) : bool { return (bool) $classOrInterface->getMethod($methodCallRename->getNewMethod()); } private function shouldKeepForParentInterface(MethodCallRenameInterface $methodCallRename, ?ClassReflection $classReflection) : bool { if (!$classReflection instanceof ClassReflection) { return \false; } // interface can change current method, as parent contract is still valid if (!$classReflection->isInterface()) { return \false; } return $this->classManipulator->hasParentMethodOrInterface($methodCallRename->getObjectType(), $methodCallRename->getOldMethod()); } /** * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface * @return \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_|null */ private function refactorClass($classOrInterface, Scope $scope) { if (!$scope->isInClass()) { return null; } $classReflection = $scope->getClassReflection(); $hasChanged = \false; foreach ($classOrInterface->getMethods() as $classMethod) { $methodName = $this->getName($classMethod->name); if ($methodName === null) { continue; } foreach ($this->methodCallRenames as $methodCallRename) { if ($this->shouldSkipRename($methodName, $classMethod, $methodCallRename, $classOrInterface, $classReflection)) { continue; } $classMethod->name = new Identifier($methodCallRename->getNewMethod()); $hasChanged = \true; continue 2; } } if ($hasChanged) { return $classOrInterface; } return null; } /** * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_ $classOrInterface */ private function shouldSkipRename(string $methodName, ClassMethod $classMethod, MethodCallRenameInterface $methodCallRename, $classOrInterface, ?ClassReflection $classReflection) : bool { if (!$this->nodeNameResolver->isStringName($methodName, $methodCallRename->getOldMethod())) { return \true; } if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($classMethod, $methodCallRename->getObjectType())) { return \true; } if ($this->shouldKeepForParentInterface($methodCallRename, $classReflection)) { return \true; } return $this->hasClassNewClassMethod($classOrInterface, $methodCallRename); } /** * @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\NullsafeMethodCall $call * @return \PhpParser\Node\Expr\ArrayDimFetch|null|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\NullsafeMethodCall */ private function refactorMethodCallAndStaticCall($call) { $callName = $this->getName($call->name); if ($callName === null) { return null; } foreach ($this->methodCallRenames as $methodCallRename) { if (!$this->nodeNameResolver->isStringName($callName, $methodCallRename->getOldMethod())) { continue; } if (!$this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType($call, $methodCallRename->getObjectType())) { continue; } if ($this->shouldSkipClassMethod($call, $methodCallRename)) { continue; } $call->name = new Identifier($methodCallRename->getNewMethod()); if ($methodCallRename instanceof MethodCallRenameWithArrayKey) { return new ArrayDimFetch($call, BuilderHelpers::normalizeValue($methodCallRename->getArrayKey())); } return $call; } return null; } } Rector/ClassConstFetch/RenameClassConstFetchRector.php 0000644 00000005742 15126447325 0017117 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\ClassConstFetch; use PhpParser\Node; use PhpParser\Node\Expr\ClassConstFetch; use PhpParser\Node\Identifier; use PhpParser\Node\Name\FullyQualified; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; use Rector\Renaming\Contract\RenameClassConstFetchInterface; use Rector\Renaming\ValueObject\RenameClassAndConstFetch; use Rector\Renaming\ValueObject\RenameClassConstFetch; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector\RenameClassConstFetchRectorTest */ final class RenameClassConstFetchRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var RenameClassConstFetchInterface[] */ private $renameClassConstFetches = []; public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Replaces defined class constants in their calls.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' $value = SomeClass::OLD_CONSTANT; $value = SomeClass::OTHER_OLD_CONSTANT; CODE_SAMPLE , <<<'CODE_SAMPLE' $value = SomeClass::NEW_CONSTANT; $value = DifferentClass::NEW_CONSTANT; CODE_SAMPLE , [new RenameClassConstFetch('SomeClass', 'OLD_CONSTANT', 'NEW_CONSTANT'), new RenameClassAndConstFetch('SomeClass', 'OTHER_OLD_CONSTANT', 'DifferentClass', 'NEW_CONSTANT')])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [ClassConstFetch::class]; } /** * @param ClassConstFetch $node */ public function refactor(Node $node) : ?ClassConstFetch { foreach ($this->renameClassConstFetches as $renameClassConstFetch) { if (!$this->isName($node->name, $renameClassConstFetch->getOldConstant())) { continue; } if (!$this->isObjectType($node->class, $renameClassConstFetch->getOldObjectType())) { continue; } if ($renameClassConstFetch instanceof RenameClassAndConstFetch) { return $this->createClassAndConstFetch($renameClassConstFetch); } $node->name = new Identifier($renameClassConstFetch->getNewConstant()); return $node; } return null; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allIsAOf($configuration, RenameClassConstFetchInterface::class); $this->renameClassConstFetches = $configuration; } private function createClassAndConstFetch(RenameClassAndConstFetch $renameClassAndConstFetch) : ClassConstFetch { return new ClassConstFetch(new FullyQualified($renameClassAndConstFetch->getNewClass()), new Identifier($renameClassAndConstFetch->getNewConstant())); } } Rector/ConstFetch/RenameConstantRector.php 0000644 00000004365 15126447325 0014674 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\ConstFetch; use PhpParser\Node; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Name; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; use Rector\Validation\RectorAssert; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\ConstFetch\RenameConstantRector\RenameConstantRectorTest */ final class RenameConstantRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var array<string, string> */ private $oldToNewConstants = []; public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Replace constant by new ones', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' final class SomeClass { public function run() { return MYSQL_ASSOC; } } CODE_SAMPLE , <<<'CODE_SAMPLE' final class SomeClass { public function run() { return MYSQLI_ASSOC; } } CODE_SAMPLE , ['MYSQL_ASSOC' => 'MYSQLI_ASSOC', 'OLD_CONSTANT' => 'NEW_CONSTANT'])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [ConstFetch::class]; } /** * @param ConstFetch $node */ public function refactor(Node $node) : ?Node { foreach ($this->oldToNewConstants as $oldConstant => $newConstant) { if (!$this->isName($node->name, $oldConstant)) { continue; } $node->name = new Name($newConstant); return $node; } return null; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allString(\array_keys($configuration)); Assert::allString($configuration); foreach ($configuration as $oldConstant => $newConstant) { RectorAssert::constantName($oldConstant); RectorAssert::constantName($newConstant); } /** @var array<string, string> $configuration */ $this->oldToNewConstants = $configuration; } } Rector/Name/RenameClassRector.php 0000644 00000006070 15126447325 0012763 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\Name; use PhpParser\Node; use PhpParser\Node\FunctionLike; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Property; use Rector\Configuration\RenamedClassesDataCollector; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\Renaming\NodeManipulator\ClassRenamer; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\Name\RenameClassRector\RenameClassRectorTest */ final class RenameClassRector extends AbstractRector implements ConfigurableRectorInterface { /** * @readonly * @var \Rector\Configuration\RenamedClassesDataCollector */ private $renamedClassesDataCollector; /** * @readonly * @var \Rector\Renaming\NodeManipulator\ClassRenamer */ private $classRenamer; public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ClassRenamer $classRenamer) { $this->renamedClassesDataCollector = $renamedClassesDataCollector; $this->classRenamer = $classRenamer; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Replaces defined classes by new ones.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' namespace App; use SomeOldClass; function someFunction(SomeOldClass $someOldClass): SomeOldClass { if ($someOldClass instanceof SomeOldClass) { return new SomeOldClass; } } CODE_SAMPLE , <<<'CODE_SAMPLE' namespace App; use SomeNewClass; function someFunction(SomeNewClass $someOldClass): SomeNewClass { if ($someOldClass instanceof SomeNewClass) { return new SomeNewClass; } } CODE_SAMPLE , ['App\\SomeOldClass' => 'App\\SomeNewClass'])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [FullyQualified::class, Property::class, FunctionLike::class, Expression::class, ClassLike::class, If_::class]; } /** * @param FunctionLike|FullyQualified|ClassLike|Expression|Namespace_|Property|If_ $node */ public function refactor(Node $node) : ?Node { $oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses(); if ($oldToNewClasses !== []) { $scope = $node->getAttribute(AttributeKey::SCOPE); return $this->classRenamer->renameNode($node, $oldToNewClasses, $scope); } return null; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allString($configuration); Assert::allString(\array_keys($configuration)); $this->renamedClassesDataCollector->addOldToNewClasses($configuration); } } Rector/ClassMethod/RenameAnnotationRector.php 0000644 00000011570 15126447325 0015357 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\ClassMethod; use PhpParser\Node; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Property; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\Comments\NodeDocBlock\DocBlockUpdater; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer; use Rector\Rector\AbstractRector; use Rector\Renaming\Contract\RenameAnnotationInterface; use Rector\Renaming\ValueObject\RenameAnnotationByType; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\ClassMethod\RenameAnnotationRector\RenameAnnotationRectorTest */ final class RenameAnnotationRector extends AbstractRector implements ConfigurableRectorInterface { /** * @readonly * @var \Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer */ private $docBlockTagReplacer; /** * @readonly * @var \Rector\Comments\NodeDocBlock\DocBlockUpdater */ private $docBlockUpdater; /** * @readonly * @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory */ private $phpDocInfoFactory; /** * @var RenameAnnotationInterface[] */ private $renameAnnotations = []; public function __construct(DocBlockTagReplacer $docBlockTagReplacer, DocBlockUpdater $docBlockUpdater, PhpDocInfoFactory $phpDocInfoFactory) { $this->docBlockTagReplacer = $docBlockTagReplacer; $this->docBlockUpdater = $docBlockUpdater; $this->phpDocInfoFactory = $phpDocInfoFactory; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Turns defined annotations above properties and methods to their new values.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' use PHPUnit\Framework\TestCase; final class SomeTest extends TestCase { /** * @test */ public function someMethod() { } } CODE_SAMPLE , <<<'CODE_SAMPLE' use PHPUnit\Framework\TestCase; final class SomeTest extends TestCase { /** * @scenario */ public function someMethod() { } } CODE_SAMPLE , [new RenameAnnotationByType('PHPUnit\\Framework\\TestCase', 'test', 'scenario')])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [Class_::class, Expression::class]; } /** * @param Class_|Expression $node */ public function refactor(Node $node) : ?Node { if ($node instanceof Expression) { return $this->refactorExpression($node); } $hasChanged = \false; foreach ($node->stmts as $stmt) { if (!$stmt instanceof ClassMethod && !$stmt instanceof Property) { continue; } $phpDocInfo = $this->phpDocInfoFactory->createFromNode($stmt); if (!$phpDocInfo instanceof PhpDocInfo) { continue; } foreach ($this->renameAnnotations as $renameAnnotation) { if ($renameAnnotation instanceof RenameAnnotationByType && !$this->isObjectType($node, $renameAnnotation->getObjectType())) { continue; } $hasDocBlockChanged = $this->docBlockTagReplacer->replaceTagByAnother($phpDocInfo, $renameAnnotation->getOldAnnotation(), $renameAnnotation->getNewAnnotation()); if ($hasDocBlockChanged) { $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt); $hasChanged = \true; } } } if (!$hasChanged) { return null; } return $node; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allIsAOf($configuration, RenameAnnotationInterface::class); $this->renameAnnotations = $configuration; } private function refactorExpression(Expression $expression) : ?Expression { $hasChanged = \false; $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($expression); foreach ($this->renameAnnotations as $renameAnnotation) { $hasDocBlockChanged = $this->docBlockTagReplacer->replaceTagByAnother($phpDocInfo, $renameAnnotation->getOldAnnotation(), $renameAnnotation->getNewAnnotation()); if ($hasDocBlockChanged) { $hasChanged = \true; } } if ($hasChanged) { $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($expression); return $expression; } return null; } } Rector/FuncCall/RenameFunctionRector.php 0000644 00000004275 15126447325 0014317 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\FuncCall; use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\FuncCall\RenameFunctionRector\RenameFunctionRectorTest */ final class RenameFunctionRector extends AbstractRector implements ConfigurableRectorInterface { /** * @var array<string, string> */ private $oldFunctionToNewFunction = []; public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Turns defined function call new one.', [new ConfiguredCodeSample('view("...", []);', 'Laravel\\Templating\\render("...", []);', ['view' => 'Laravel\\Templating\\render'])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [FuncCall::class]; } /** * @param FuncCall $node */ public function refactor(Node $node) : ?Node { $nodeName = $this->getName($node); if ($nodeName === null) { return null; } foreach ($this->oldFunctionToNewFunction as $oldFunction => $newFunction) { if (!$this->nodeNameResolver->isStringName($nodeName, $oldFunction)) { continue; } $node->name = $this->createName($newFunction); return $node; } return null; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allString(\array_values($configuration)); Assert::allString($configuration); $this->oldFunctionToNewFunction = $configuration; } private function createName(string $newFunction) : Name { if (\strpos($newFunction, '\\') !== \false) { return new FullyQualified($newFunction); } return new Name($newFunction); } } Rector/String_/RenameStringRector.php 0000644 00000004206 15126447325 0013710 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\String_; use PhpParser\Node; use PhpParser\Node\Scalar\String_; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\PhpParser\Node\Value\ValueResolver; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\String_\RenameStringRector\RenameStringRectorTest */ final class RenameStringRector extends AbstractRector implements ConfigurableRectorInterface { /** * @readonly * @var \Rector\PhpParser\Node\Value\ValueResolver */ private $valueResolver; /** * @var array<string, string> */ private $stringChanges = []; public function __construct(ValueResolver $valueResolver) { $this->valueResolver = $valueResolver; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Change string value', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' class SomeClass { public function run() { return 'ROLE_PREVIOUS_ADMIN'; } } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass { public function run() { return 'IS_IMPERSONATOR'; } } CODE_SAMPLE , ['ROLE_PREVIOUS_ADMIN' => 'IS_IMPERSONATOR'])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [String_::class]; } /** * @param String_ $node */ public function refactor(Node $node) : ?Node { foreach ($this->stringChanges as $oldValue => $newValue) { if (!$this->valueResolver->isValue($node, $oldValue)) { continue; } return new String_($newValue); } return null; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allString(\array_keys($configuration)); Assert::allString($configuration); $this->stringChanges = $configuration; } } Rector/FunctionLike/RenameFunctionLikeParamWithinCallLikeArgRector.php 0000644 00000017075 15126447325 0022230 0 ustar 00 <?php declare (strict_types=1); namespace Rector\Renaming\Rector\FunctionLike; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\FunctionLike; use PhpParser\Node\Identifier; use PhpParser\Node\Param; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Naming\Guard\BreakingVariableRenameGuard; use Rector\Naming\ParamRenamer\ParamRenamer; use Rector\Naming\ValueObject\ParamRename; use Rector\Naming\ValueObjectFactory\ParamRenameFactory; use Rector\Rector\AbstractRector; use Rector\Renaming\ValueObject\RenameFunctionLikeParamWithinCallLikeArg; use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; use RectorPrefix202411\Webmozart\Assert\Assert; /** * @see \Rector\Tests\Renaming\Rector\FunctionLike\RenameFunctionLikeParamWithinCallLikeArgRector\RenameFunctionLikeParamWithinCallLikeArgRectorTest */ final class RenameFunctionLikeParamWithinCallLikeArgRector extends AbstractRector implements ConfigurableRectorInterface { /** * @readonly * @var \Rector\Naming\Guard\BreakingVariableRenameGuard */ private $breakingVariableRenameGuard; /** * @readonly * @var \Rector\Naming\ParamRenamer\ParamRenamer */ private $paramRenamer; /** * @readonly * @var \Rector\Naming\ValueObjectFactory\ParamRenameFactory */ private $paramRenameFactory; /** * @var RenameFunctionLikeParamWithinCallLikeArg[] */ private $renameFunctionLikeParamWithinCallLikeArgs = []; public function __construct(BreakingVariableRenameGuard $breakingVariableRenameGuard, ParamRenamer $paramRenamer, ParamRenameFactory $paramRenameFactory) { $this->breakingVariableRenameGuard = $breakingVariableRenameGuard; $this->paramRenamer = $paramRenamer; $this->paramRenameFactory = $paramRenameFactory; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Rename param within closures and arrow functions based on use with specified method calls', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' (new SomeClass)->process(function ($param) {}); CODE_SAMPLE , <<<'CODE_SAMPLE' (new SomeClass)->process(function ($parameter) {}); CODE_SAMPLE , [new RenameFunctionLikeParamWithinCallLikeArg('SomeClass', 'process', 0, 0, 'parameter')])]); } /** * @return array<class-string<Node>> */ public function getNodeTypes() : array { return [MethodCall::class, StaticCall::class]; } /** * @param CallLike $node */ public function refactor(Node $node) : ?Node { $hasChanged = \false; foreach ($this->renameFunctionLikeParamWithinCallLikeArgs as $renameFunctionLikeParamWithinCallLikeArg) { if (!$node instanceof MethodCall && !$node instanceof StaticCall) { continue; } switch (\true) { case $node instanceof MethodCall: $type = $node->var; break; case $node instanceof StaticCall: $type = $node->class; break; } if (!$this->isObjectType($type, $renameFunctionLikeParamWithinCallLikeArg->getObjectType())) { continue; } if (($node->name ?? null) === null) { continue; } if (!$node->name instanceof Identifier) { continue; } if (!$this->isName($node->name, $renameFunctionLikeParamWithinCallLikeArg->getMethodName())) { continue; } $arg = $this->findArgFromMethodCall($renameFunctionLikeParamWithinCallLikeArg, $node); $functionLike = ($nullsafeVariable1 = $arg) ? $nullsafeVariable1->value : null; if (!$arg instanceof Arg) { continue; } if (!$functionLike instanceof FunctionLike) { continue; } $param = $this->findParameterFromArg($arg, $renameFunctionLikeParamWithinCallLikeArg); if (!$param instanceof Param) { continue; } if (!$param->var instanceof Variable) { continue; } if (($functionLike instanceof Closure || $functionLike instanceof ArrowFunction) && $this->breakingVariableRenameGuard->shouldSkipVariable((string) $this->nodeNameResolver->getName($param->var), $renameFunctionLikeParamWithinCallLikeArg->getNewParamName(), $functionLike, $param->var)) { continue; } $paramRename = $this->paramRenameFactory->createFromResolvedExpectedName($functionLike, $param, $renameFunctionLikeParamWithinCallLikeArg->getNewParamName()); if (!$paramRename instanceof ParamRename) { continue; } $this->paramRenamer->rename($paramRename); $hasChanged = \true; } if (!$hasChanged) { return null; } return $node; } /** * @param RenameFunctionLikeParamWithinCallLikeArg[] $configuration */ public function configure(array $configuration) : void { Assert::allIsAOf($configuration, RenameFunctionLikeParamWithinCallLikeArg::class); $this->renameFunctionLikeParamWithinCallLikeArgs = $configuration; } public function findParameterFromArg(Arg $arg, RenameFunctionLikeParamWithinCallLikeArg $renameFunctionLikeParamWithinCallLikeArg) : ?Param { $functionLike = $arg->value; if (!$functionLike instanceof FunctionLike) { return null; } return $functionLike->params[$renameFunctionLikeParamWithinCallLikeArg->getFunctionLikePosition()] ?? null; } private function findArgFromMethodCall(RenameFunctionLikeParamWithinCallLikeArg $renameFunctionLikeParamWithinCallLikeArg, CallLike $callLike) : ?Arg { if (\is_int($renameFunctionLikeParamWithinCallLikeArg->getCallLikePosition())) { return $this->processPositionalArg($callLike, $renameFunctionLikeParamWithinCallLikeArg); } return $this->processNamedArg($callLike, $renameFunctionLikeParamWithinCallLikeArg); } private function processPositionalArg(CallLike $callLike, RenameFunctionLikeParamWithinCallLikeArg $renameFunctionLikeParamWithinCallLikeArg) : ?Arg { if ($callLike->isFirstClassCallable()) { return null; } if ($callLike->getArgs() === []) { return null; } $arg = $callLike->args[$renameFunctionLikeParamWithinCallLikeArg->getCallLikePosition()] ?? null; if (!$arg instanceof Arg) { return null; } // int positions shouldn't have names if ($arg->name !== null) { return null; } return $arg; } private function processNamedArg(CallLike $callLike, RenameFunctionLikeParamWithinCallLikeArg $renameFunctionLikeParamWithinCallLikeArg) : ?Arg { $args = \array_filter($callLike->getArgs(), static function (Arg $arg) use($renameFunctionLikeParamWithinCallLikeArg) : bool { if ($arg->name === null) { return \false; } return $arg->name->name === $renameFunctionLikeParamWithinCallLikeArg->getCallLikePosition(); }); if ($args === []) { return null; } return \array_values($args)[0]; } }
| ver. 1.6 |
Github
|
.
| PHP 8.2.30 | ??????????? ?????????: 0 |
proxy
|
phpinfo
|
???????????