REDROOM
PHP 8.2.31
Path:
Logout
Edit File
Size: 3.87 KB
Close
/opt/cpanel/ea-wappspector/vendor/rector/rector/rules/Renaming/Rector/PropertyFetch/RenamePropertyRector.php
Text
Base64
<?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; } }
Save
Close
Exit & Reset
Text mode: syntax highlighting auto-detects file type.
Directory Contents
Dirs: 0 × Files: 1
Delete Selected
Select All
Select None
Sort:
Name
Size
Modified
Enable drag-to-move
Name
Size
Perms
Modified
Actions
RenamePropertyRector.php
3.87 KB
lrw-r--r--
2024-11-08 13:59:10
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Zip Selected
If ZipArchive is unavailable, a
.tar
will be created (no compression).