晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
|
Server : Apache System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64 User : rainic ( 1014) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /opt/cpanel/ea-wappspector/vendor/rector/rector/rules/Renaming/NodeManipulator/ |
Upload File : |
<?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;
}
}