Skip to content

Commit

Permalink
Updated Rector to commit ed16cab5a2fa6982539e06ad70ac9c119c7dc346
Browse files Browse the repository at this point in the history
rectorphp/rector-src@ed16cab [Php80] Add typed property Closure support on ClassPropertyAssignToConstructorPromotionRector (#3453)
  • Loading branch information
TomasVotruba committed Mar 4, 2023
1 parent b66a828 commit 57f48de
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
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 @@ -44,6 +48,7 @@
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 Down Expand Up @@ -94,6 +99,11 @@ final class PHPStanNodeScopeResolver
* @var \Rector\Core\Util\Reflection\PrivatesAccessor
*/
private $privatesAccessor;
/**
* @readonly
* @var \Rector\Core\StaticReflection\SourceLocator\RenamedClassesSourceLocator
*/
private $renamedClassesSourceLocator;
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
Expand All @@ -109,7 +119,7 @@ final class PHPStanNodeScopeResolver
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
*/
private $classAnalyzer;
public function __construct(ChangedFilesDetector $changedFilesDetector, DependencyResolver $dependencyResolver, NodeScopeResolver $nodeScopeResolver, ReflectionProvider $reflectionProvider, RemoveDeepChainMethodCallNodeVisitor $removeDeepChainMethodCallNodeVisitor, \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory $scopeFactory, PrivatesAccessor $privatesAccessor, NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder, ClassAnalyzer $classAnalyzer)
public function __construct(ChangedFilesDetector $changedFilesDetector, DependencyResolver $dependencyResolver, NodeScopeResolver $nodeScopeResolver, ReflectionProvider $reflectionProvider, RemoveDeepChainMethodCallNodeVisitor $removeDeepChainMethodCallNodeVisitor, \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory $scopeFactory, PrivatesAccessor $privatesAccessor, RenamedClassesSourceLocator $renamedClassesSourceLocator, NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder, ClassAnalyzer $classAnalyzer)
{
$this->changedFilesDetector = $changedFilesDetector;
$this->dependencyResolver = $dependencyResolver;
Expand All @@ -118,9 +128,11 @@ public function __construct(ChangedFilesDetector $changedFilesDetector, Dependen
$this->removeDeepChainMethodCallNodeVisitor = $removeDeepChainMethodCallNodeVisitor;
$this->scopeFactory = $scopeFactory;
$this->privatesAccessor = $privatesAccessor;
$this->renamedClassesSourceLocator = $renamedClassesSourceLocator;
$this->nodeNameResolver = $nodeNameResolver;
$this->betterNodeFinder = $betterNodeFinder;
$this->classAnalyzer = $classAnalyzer;
$this->decoratePHPStanNodeScopeResolverWithRenamedClassSourceLocator($this->nodeScopeResolver);
}
/**
* @param Stmt[] $stmts
Expand Down Expand Up @@ -385,4 +397,23 @@ private function resolveAndSaveDependentFiles(array $stmts, MutatingScope $mutat
}
$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);
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'd9dc05621793de2fa23815b73e8db94c8f56b30d';
public const PACKAGE_VERSION = 'ed16cab5a2fa6982539e06ad70ac9c119c7dc346';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-03-04 08:51:14';
public const RELEASE_DATE = '2023-03-04 08:50:08';
/**
* @var int
*/
Expand Down
59 changes: 59 additions & 0 deletions src/StaticReflection/SourceLocator/RenamedClassesSourceLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare (strict_types=1);
namespace Rector\Core\StaticReflection\SourceLocator;

use PhpParser\Builder\Class_;
use PHPStan\BetterReflection\Identifier\Identifier;
use PHPStan\BetterReflection\Identifier\IdentifierType;
use PHPStan\BetterReflection\Reflection\Reflection;
use PHPStan\BetterReflection\Reflection\ReflectionClass;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use Rector\Core\Configuration\RenamedClassesDataCollector;
/**
* Inspired from \PHPStan\BetterReflection\SourceLocator\Type\StringSourceLocator
*/
final class RenamedClassesSourceLocator implements SourceLocator
{
/**
* @readonly
* @var \Rector\Core\Configuration\RenamedClassesDataCollector
*/
private $renamedClassesDataCollector;
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector)
{
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
}
public function locateIdentifier(Reflector $reflector, Identifier $identifier) : ?Reflection
{
if (!$identifier->isClass()) {
return null;
}
$identifierName = $identifier->getName();
foreach ($this->renamedClassesDataCollector->getOldClasses() as $oldClass) {
if ($identifierName !== $oldClass) {
continue;
}
/* Use ReflectionProvider causes infinite loop */
if (!\class_exists($oldClass)) {
continue;
}
return $this->createFakeReflectionClassFromClassName($oldClass);
}
return null;
}
/**
* @return array<int, Reflection>
*/
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType) : array
{
return [];
}
private function createFakeReflectionClassFromClassName(string $oldClass) : ReflectionClass
{
$classBuilder = new Class_($oldClass);
$class = $classBuilder->getNode();
return ReflectionClass::createFromInstance($class);
}
}
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit18c8ac9e9b0749b98a5580804b1c6c81::getLoader();
return ComposerAutoloaderInit5a8c5b53ccb2dcf8d65b0ea67959554e::getLoader();
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,7 @@
'Rector\\Core\\Rector\\AbstractScopeAwareRector' => $baseDir . '/src/Rector/AbstractScopeAwareRector.php',
'Rector\\Core\\Reflection\\ReflectionResolver' => $baseDir . '/src/Reflection/ReflectionResolver.php',
'Rector\\Core\\StaticReflection\\DynamicSourceLocatorDecorator' => $baseDir . '/src/StaticReflection/DynamicSourceLocatorDecorator.php',
'Rector\\Core\\StaticReflection\\SourceLocator\\RenamedClassesSourceLocator' => $baseDir . '/src/StaticReflection/SourceLocator/RenamedClassesSourceLocator.php',
'Rector\\Core\\Util\\ArrayChecker' => $baseDir . '/src/Util/ArrayChecker.php',
'Rector\\Core\\Util\\ArrayParametersMerger' => $baseDir . '/src/Util/ArrayParametersMerger.php',
'Rector\\Core\\Util\\MemoryLimiter' => $baseDir . '/src/Util/MemoryLimiter.php',
Expand Down
10 changes: 5 additions & 5 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit18c8ac9e9b0749b98a5580804b1c6c81
class ComposerAutoloaderInit5a8c5b53ccb2dcf8d65b0ea67959554e
{
private static $loader;

Expand All @@ -22,17 +22,17 @@ public static function getLoader()
return self::$loader;
}

spl_autoload_register(array('ComposerAutoloaderInit18c8ac9e9b0749b98a5580804b1c6c81', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit5a8c5b53ccb2dcf8d65b0ea67959554e', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit18c8ac9e9b0749b98a5580804b1c6c81', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit5a8c5b53ccb2dcf8d65b0ea67959554e', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit18c8ac9e9b0749b98a5580804b1c6c81::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit5a8c5b53ccb2dcf8d65b0ea67959554e::getInitializer($loader));

$loader->setClassMapAuthoritative(true);
$loader->register(true);

$filesToLoad = \Composer\Autoload\ComposerStaticInit18c8ac9e9b0749b98a5580804b1c6c81::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit5a8c5b53ccb2dcf8d65b0ea67959554e::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
Expand Down
9 changes: 5 additions & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInit18c8ac9e9b0749b98a5580804b1c6c81
class ComposerStaticInit5a8c5b53ccb2dcf8d65b0ea67959554e
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
Expand Down Expand Up @@ -1803,6 +1803,7 @@ class ComposerStaticInit18c8ac9e9b0749b98a5580804b1c6c81
'Rector\\Core\\Rector\\AbstractScopeAwareRector' => __DIR__ . '/../..' . '/src/Rector/AbstractScopeAwareRector.php',
'Rector\\Core\\Reflection\\ReflectionResolver' => __DIR__ . '/../..' . '/src/Reflection/ReflectionResolver.php',
'Rector\\Core\\StaticReflection\\DynamicSourceLocatorDecorator' => __DIR__ . '/../..' . '/src/StaticReflection/DynamicSourceLocatorDecorator.php',
'Rector\\Core\\StaticReflection\\SourceLocator\\RenamedClassesSourceLocator' => __DIR__ . '/../..' . '/src/StaticReflection/SourceLocator/RenamedClassesSourceLocator.php',
'Rector\\Core\\Util\\ArrayChecker' => __DIR__ . '/../..' . '/src/Util/ArrayChecker.php',
'Rector\\Core\\Util\\ArrayParametersMerger' => __DIR__ . '/../..' . '/src/Util/ArrayParametersMerger.php',
'Rector\\Core\\Util\\MemoryLimiter' => __DIR__ . '/../..' . '/src/Util/MemoryLimiter.php',
Expand Down Expand Up @@ -3133,9 +3134,9 @@ class ComposerStaticInit18c8ac9e9b0749b98a5580804b1c6c81
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit18c8ac9e9b0749b98a5580804b1c6c81::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit18c8ac9e9b0749b98a5580804b1c6c81::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit18c8ac9e9b0749b98a5580804b1c6c81::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit5a8c5b53ccb2dcf8d65b0ea67959554e::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit5a8c5b53ccb2dcf8d65b0ea67959554e::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit5a8c5b53ccb2dcf8d65b0ea67959554e::$classMap;

}, null, ClassLoader::class);
}
Expand Down

0 comments on commit 57f48de

Please sign in to comment.