Skip to content

Commit

Permalink
[Core] Remove RenamedClassesSourceLocator (#3454)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Mar 4, 2023
1 parent ed16cab commit d9dc056
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\ScopeContext;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Node\UnreachableStatementNode;
use PHPStan\Reflection\BetterReflection\Reflector\MemoizingReflector;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ObjectType;
Expand All @@ -49,7 +45,6 @@
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\StaticReflection\SourceLocator\RenamedClassesSourceLocator;
use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -75,12 +70,10 @@ public function __construct(
private readonly RemoveDeepChainMethodCallNodeVisitor $removeDeepChainMethodCallNodeVisitor,
private readonly ScopeFactory $scopeFactory,
private readonly PrivatesAccessor $privatesAccessor,
private readonly RenamedClassesSourceLocator $renamedClassesSourceLocator,
private readonly NodeNameResolver $nodeNameResolver,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ClassAnalyzer $classAnalyzer
) {
$this->decoratePHPStanNodeScopeResolverWithRenamedClassSourceLocator($this->nodeScopeResolver);
}

/**
Expand Down Expand Up @@ -445,45 +438,4 @@ private function resolveAndSaveDependentFiles(

$this->changedFilesDetector->addFileWithDependencies($filePath, $dependentFiles);
}

/**
* In case PHPStan tried to parse a file with missing class, it fails.
* But sometimes we want to rename old class that is missing with Rector..
*
* That's why we have to skip fatal errors of PHPStan caused by missing class,
* so Rector can fix it first. Then run Rector again to refactor code with new classes.
*/
private function decoratePHPStanNodeScopeResolverWithRenamedClassSourceLocator(
NodeScopeResolver $nodeScopeResolver
): void {
// 1. get PHPStan locator
/** @var MemoizingReflector $classReflector */
$classReflector = $this->privatesAccessor->getPrivatePropertyOfClass(
$nodeScopeResolver,
'reflector',
Reflector::class
);

$reflector = $this->privatesAccessor->getPrivatePropertyOfClass(
$classReflector,
'reflector',
Reflector::class
);

/** @var SourceLocator $sourceLocator */
$sourceLocator = $this->privatesAccessor->getPrivatePropertyOfClass(
$reflector,
'sourceLocator',
SourceLocator::class
);

// 2. get Rector locator
$aggregateSourceLocator = new AggregateSourceLocator([$sourceLocator, $this->renamedClassesSourceLocator]);
$this->privatesAccessor->setPrivatePropertyOfClass(
$reflector,
'sourceLocator',
$aggregateSourceLocator,
AggregateSourceLocator::class
);
}
}
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,3 @@ parameters:
"""
count: 2
path: src/Rector/AbstractRector.php

-
message: "#^Function \"class_exists\\(\\)\" cannot be used/left in the code\\: use ReflectionProvider\\-\\>has\\*\\(\\) instead$#"
count: 1
path: src/StaticReflection/SourceLocator/RenamedClassesSourceLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\Fixture;

class NameExtendsNotExists extends \NotExistsClass
{
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\Fixture;

class NameExtendsNotExists extends \NewClass
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
'Doctrine\DBAL\DBALException' => 'Doctrine\DBAL\Exception',
'Bar' => 'BarInterface',
'App' => 'Illuminate\Support\Facades\App',
'NotExistsClass' => 'NewClass',
]);
};
64 changes: 0 additions & 64 deletions src/StaticReflection/SourceLocator/RenamedClassesSourceLocator.php

This file was deleted.

0 comments on commit d9dc056

Please sign in to comment.