mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-21 16:02:23 +02:00
Updated Rector to commit 6451387bb657ce665f47acb4aa1c64be7153677d
6451387bb6
[Performance][TypeDeclaration] Reduce resolve ClassReflection from Property on AllAssignNodePropertyTypeInferer (#4539)
This commit is contained in:
parent
7e9abb6ee1
commit
58d5c38f57
@ -134,7 +134,7 @@ CODE_SAMPLE
|
||||
if (!$property instanceof Property) {
|
||||
return \false;
|
||||
}
|
||||
$type = $this->allAssignNodePropertyTypeInferer->inferProperty($property);
|
||||
$type = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection);
|
||||
return $type instanceof ArrayType;
|
||||
}
|
||||
}
|
||||
|
@ -5,13 +5,16 @@ namespace Rector\TypeDeclaration\Rector\Property;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\TypeCombinator;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
|
||||
use Rector\Php74\Guard\MakePropertyTypedGuard;
|
||||
@ -46,6 +49,11 @@ final class TypedPropertyFromAssignsRector extends AbstractRector implements All
|
||||
* @var \Rector\Php74\Guard\MakePropertyTypedGuard
|
||||
*/
|
||||
private $makePropertyTypedGuard;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
@ -61,12 +69,13 @@ final class TypedPropertyFromAssignsRector extends AbstractRector implements All
|
||||
* @var bool
|
||||
*/
|
||||
private $inlinePublic = \false;
|
||||
public function __construct(AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer, PropertyTypeDecorator $propertyTypeDecorator, VarTagRemover $varTagRemover, MakePropertyTypedGuard $makePropertyTypedGuard)
|
||||
public function __construct(AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer, PropertyTypeDecorator $propertyTypeDecorator, VarTagRemover $varTagRemover, MakePropertyTypedGuard $makePropertyTypedGuard, ReflectionResolver $reflectionResolver)
|
||||
{
|
||||
$this->allAssignNodePropertyTypeInferer = $allAssignNodePropertyTypeInferer;
|
||||
$this->propertyTypeDecorator = $propertyTypeDecorator;
|
||||
$this->varTagRemover = $varTagRemover;
|
||||
$this->makePropertyTypedGuard = $makePropertyTypedGuard;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
}
|
||||
public function configure(array $configuration) : void
|
||||
{
|
||||
@ -103,47 +112,61 @@ CODE_SAMPLE
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Property::class];
|
||||
return [Class_::class];
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::TYPED_PROPERTIES;
|
||||
}
|
||||
/**
|
||||
* @param Property $node
|
||||
* @param Node\Stmt\Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$this->makePropertyTypedGuard->isLegal($node, $this->inlinePublic)) {
|
||||
return null;
|
||||
$hasChanged = \false;
|
||||
$classReflection = null;
|
||||
foreach ($node->getProperties() as $property) {
|
||||
if (!$this->makePropertyTypedGuard->isLegal($property, $this->inlinePublic)) {
|
||||
continue;
|
||||
}
|
||||
// non-private property can be anything with not inline public configured
|
||||
if (!$property->isPrivate() && !$this->inlinePublic) {
|
||||
continue;
|
||||
}
|
||||
if (!$classReflection instanceof ClassReflection) {
|
||||
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
|
||||
}
|
||||
if (!$classReflection instanceof ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
$inferredType = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection);
|
||||
if (!$inferredType instanceof Type) {
|
||||
continue;
|
||||
}
|
||||
if ($inferredType instanceof MixedType) {
|
||||
continue;
|
||||
}
|
||||
$inferredType = $this->decorateTypeWithNullableIfDefaultPropertyNull($property, $inferredType);
|
||||
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($inferredType, TypeKind::PROPERTY);
|
||||
if ($typeNode === null) {
|
||||
continue;
|
||||
}
|
||||
$hasChanged = \true;
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
|
||||
if ($inferredType instanceof UnionType) {
|
||||
$this->propertyTypeDecorator->decoratePropertyUnionType($inferredType, $typeNode, $property, $phpDocInfo, \false);
|
||||
} else {
|
||||
$property->type = $typeNode;
|
||||
}
|
||||
if (!$property->type instanceof Node) {
|
||||
continue;
|
||||
}
|
||||
$this->varTagRemover->removeVarTagIfUseless($phpDocInfo, $property);
|
||||
}
|
||||
// non-private property can be anything with not inline public configured
|
||||
if (!$node->isPrivate() && !$this->inlinePublic) {
|
||||
return null;
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
}
|
||||
$inferredType = $this->allAssignNodePropertyTypeInferer->inferProperty($node);
|
||||
if (!$inferredType instanceof Type) {
|
||||
return null;
|
||||
}
|
||||
if ($inferredType instanceof MixedType) {
|
||||
return null;
|
||||
}
|
||||
$inferredType = $this->decorateTypeWithNullableIfDefaultPropertyNull($node, $inferredType);
|
||||
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($inferredType, TypeKind::PROPERTY);
|
||||
if ($typeNode === null) {
|
||||
return null;
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
||||
if ($inferredType instanceof UnionType) {
|
||||
$this->propertyTypeDecorator->decoratePropertyUnionType($inferredType, $typeNode, $node, $phpDocInfo, \false);
|
||||
} else {
|
||||
$node->type = $typeNode;
|
||||
}
|
||||
if (!$node->type instanceof Node) {
|
||||
return null;
|
||||
}
|
||||
$this->varTagRemover->removeVarTagIfUseless($phpDocInfo, $node);
|
||||
return $node;
|
||||
return null;
|
||||
}
|
||||
private function decorateTypeWithNullableIfDefaultPropertyNull(Property $property, Type $inferredType) : Type
|
||||
{
|
||||
|
@ -8,7 +8,6 @@ use PhpParser\Node\Stmt\Property;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\PhpParser\ClassLikeAstResolver;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\TypeDeclaration\TypeInferer\AssignToPropertyTypeInferer;
|
||||
final class AllAssignNodePropertyTypeInferer
|
||||
@ -23,29 +22,19 @@ final class AllAssignNodePropertyTypeInferer
|
||||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\ClassLikeAstResolver
|
||||
*/
|
||||
private $classLikeAstResolver;
|
||||
public function __construct(AssignToPropertyTypeInferer $assignToPropertyTypeInferer, NodeNameResolver $nodeNameResolver, ReflectionResolver $reflectionResolver, ClassLikeAstResolver $classLikeAstResolver)
|
||||
public function __construct(AssignToPropertyTypeInferer $assignToPropertyTypeInferer, NodeNameResolver $nodeNameResolver, ClassLikeAstResolver $classLikeAstResolver)
|
||||
{
|
||||
$this->assignToPropertyTypeInferer = $assignToPropertyTypeInferer;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
$this->classLikeAstResolver = $classLikeAstResolver;
|
||||
}
|
||||
public function inferProperty(Property $property) : ?Type
|
||||
public function inferProperty(Property $property, ClassReflection $classReflection) : ?Type
|
||||
{
|
||||
$classReflection = $this->reflectionResolver->resolveClassReflection($property);
|
||||
if (!$classReflection instanceof ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
/** @var ClassLike $classLike */
|
||||
$classLike = $this->classLikeAstResolver->resolveClassFromClassReflection($classReflection);
|
||||
$propertyName = $this->nodeNameResolver->getName($property);
|
||||
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'd0604509d0c3e22a29d1f3f174873e7c7596abb4';
|
||||
public const PACKAGE_VERSION = '6451387bb657ce665f47acb4aa1c64be7153677d';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-07-19 11:33:09';
|
||||
public const RELEASE_DATE = '2023-07-19 11:33:37';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInite09481a487e6ca388178db508851eb30::getLoader();
|
||||
return ComposerAutoloaderInit200b7a9fb8b375f7c3b40455e9ee571b::getLoader();
|
||||
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInite09481a487e6ca388178db508851eb30
|
||||
class ComposerAutoloaderInit200b7a9fb8b375f7c3b40455e9ee571b
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInite09481a487e6ca388178db508851eb30
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInite09481a487e6ca388178db508851eb30', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit200b7a9fb8b375f7c3b40455e9ee571b', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInite09481a487e6ca388178db508851eb30', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit200b7a9fb8b375f7c3b40455e9ee571b', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInite09481a487e6ca388178db508851eb30::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit200b7a9fb8b375f7c3b40455e9ee571b::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInite09481a487e6ca388178db508851eb30::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit200b7a9fb8b375f7c3b40455e9ee571b::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInite09481a487e6ca388178db508851eb30
|
||||
class ComposerStaticInit200b7a9fb8b375f7c3b40455e9ee571b
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -3027,9 +3027,9 @@ class ComposerStaticInite09481a487e6ca388178db508851eb30
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInite09481a487e6ca388178db508851eb30::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInite09481a487e6ca388178db508851eb30::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInite09481a487e6ca388178db508851eb30::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit200b7a9fb8b375f7c3b40455e9ee571b::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit200b7a9fb8b375f7c3b40455e9ee571b::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit200b7a9fb8b375f7c3b40455e9ee571b::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user