Updated Rector to commit 141a94aa49a3f64e78e3dd2f2a4b1d91de425b8b

141a94aa49 [Performance] [Php74] Reduce ClassReflection lookup from property on PropertyTypeChangeGuard (#4545)
This commit is contained in:
Tomas Votruba 2023-07-20 07:24:15 +00:00
parent 12f401a463
commit 2a46c820ee
11 changed files with 57 additions and 38 deletions

View File

@ -4,6 +4,7 @@ declare (strict_types=1);
namespace Rector\Php74\Guard;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
final class MakePropertyTypedGuard
{
/**
@ -15,11 +16,11 @@ final class MakePropertyTypedGuard
{
$this->propertyTypeChangeGuard = $propertyTypeChangeGuard;
}
public function isLegal(Property $property, bool $inlinePublic = \true) : bool
public function isLegal(Property $property, ClassReflection $classReflection, bool $inlinePublic = \true) : bool
{
if ($property->type !== null) {
return \false;
}
return $this->propertyTypeChangeGuard->isLegal($property, $inlinePublic);
return $this->propertyTypeChangeGuard->isLegal($property, $classReflection, $inlinePublic);
}
}

View File

@ -7,7 +7,6 @@ use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\NodeAnalyzer\PropertyAnalyzer;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Privatization\Guard\ParentPropertyLookupGuard;
final class PropertyTypeChangeGuard
@ -32,28 +31,18 @@ final class PropertyTypeChangeGuard
* @var \Rector\Privatization\Guard\ParentPropertyLookupGuard
*/
private $parentPropertyLookupGuard;
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(NodeNameResolver $nodeNameResolver, PropertyAnalyzer $propertyAnalyzer, PropertyManipulator $propertyManipulator, ParentPropertyLookupGuard $parentPropertyLookupGuard, ReflectionResolver $reflectionResolver)
public function __construct(NodeNameResolver $nodeNameResolver, PropertyAnalyzer $propertyAnalyzer, PropertyManipulator $propertyManipulator, ParentPropertyLookupGuard $parentPropertyLookupGuard)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->propertyAnalyzer = $propertyAnalyzer;
$this->propertyManipulator = $propertyManipulator;
$this->parentPropertyLookupGuard = $parentPropertyLookupGuard;
$this->reflectionResolver = $reflectionResolver;
}
public function isLegal(Property $property, bool $inlinePublic = \true, bool $isConstructorPromotion = \false) : bool
public function isLegal(Property $property, ClassReflection $classReflection, bool $inlinePublic = \true, bool $isConstructorPromotion = \false) : bool
{
if (\count($property->props) > 1) {
return \false;
}
$classReflection = $this->reflectionResolver->resolveClassReflection($property);
if (!$classReflection instanceof ClassReflection) {
return \false;
}
/**
* - trait properties are unpredictable based on class context they appear in
* - on interface properties as well, as interface not allowed to have property

View File

@ -7,6 +7,7 @@ use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\Php74\Guard\PropertyTypeChangeGuard;
final class MakePropertyPromotionGuard
{
@ -19,9 +20,9 @@ final class MakePropertyPromotionGuard
{
$this->propertyTypeChangeGuard = $propertyTypeChangeGuard;
}
public function isLegal(Class_ $class, Property $property, Param $param, bool $inlinePublic = \true) : bool
public function isLegal(Class_ $class, ClassReflection $classReflection, Property $property, Param $param, bool $inlinePublic = \true) : bool
{
if (!$this->propertyTypeChangeGuard->isLegal($property, $inlinePublic, \true)) {
if (!$this->propertyTypeChangeGuard->isLegal($property, $classReflection, $inlinePublic, \true)) {
return \false;
}
if ($class->isFinal()) {

View File

@ -22,6 +22,7 @@ use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
@ -76,6 +77,11 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
*/
private $typeComparator;
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
/**
* @api
* @var string
@ -91,7 +97,7 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect
* @var bool
*/
private $inlinePublic = \false;
public function __construct(PromotedPropertyCandidateResolver $promotedPropertyCandidateResolver, VariableRenamer $variableRenamer, VarTagRemover $varTagRemover, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger, MakePropertyPromotionGuard $makePropertyPromotionGuard, TypeComparator $typeComparator)
public function __construct(PromotedPropertyCandidateResolver $promotedPropertyCandidateResolver, VariableRenamer $variableRenamer, VarTagRemover $varTagRemover, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger, MakePropertyPromotionGuard $makePropertyPromotionGuard, TypeComparator $typeComparator, ReflectionResolver $reflectionResolver)
{
$this->promotedPropertyCandidateResolver = $promotedPropertyCandidateResolver;
$this->variableRenamer = $variableRenamer;
@ -100,6 +106,7 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->makePropertyPromotionGuard = $makePropertyPromotionGuard;
$this->typeComparator = $typeComparator;
$this->reflectionResolver = $reflectionResolver;
}
public function getRuleDefinition() : RuleDefinition
{
@ -151,6 +158,7 @@ CODE_SAMPLE
return null;
}
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($constructClassMethod);
$classReflection = null;
foreach ($promotionCandidates as $promotionCandidate) {
// does property have some useful annotations?
$property = $promotionCandidate->getProperty();
@ -158,7 +166,13 @@ CODE_SAMPLE
if ($this->shouldSkipParam($param)) {
continue;
}
if (!$this->makePropertyPromotionGuard->isLegal($node, $property, $param, $this->inlinePublic)) {
if ($classReflection === null) {
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
}
if ($classReflection === null) {
return null;
}
if (!$this->makePropertyPromotionGuard->isLegal($node, $classReflection, $property, $param, $this->inlinePublic)) {
continue;
}
$propertyStmtKey = $property->getAttribute(AttributeKey::STMT_KEY);

View File

@ -26,10 +26,10 @@ final class PropertyTypeOverrideGuard
}
public function isLegal(Property $property, ClassReflection $classReflection) : bool
{
$propertyName = $this->nodeNameResolver->getName($property);
if (!$this->makePropertyTypedGuard->isLegal($property)) {
if (!$this->makePropertyTypedGuard->isLegal($property, $classReflection)) {
return \false;
}
$propertyName = $this->nodeNameResolver->getName($property);
foreach ($classReflection->getParents() as $parentClassReflection) {
$nativeReflectionClass = $parentClassReflection->getNativeReflection();
if (!$nativeReflectionClass->hasProperty($propertyName)) {

View File

@ -13,6 +13,7 @@ use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Php74\Guard\MakePropertyTypedGuard;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
@ -41,11 +42,17 @@ final class PropertyTypeFromStrictSetterGetterRector extends AbstractRector impl
* @var \Rector\Php74\Guard\MakePropertyTypedGuard
*/
private $makePropertyTypedGuard;
public function __construct(GetterTypeDeclarationPropertyTypeInferer $getterTypeDeclarationPropertyTypeInferer, SetterTypeDeclarationPropertyTypeInferer $setterTypeDeclarationPropertyTypeInferer, MakePropertyTypedGuard $makePropertyTypedGuard)
/**
* @readonly
* @var \Rector\Core\Reflection\ReflectionResolver
*/
private $reflectionResolver;
public function __construct(GetterTypeDeclarationPropertyTypeInferer $getterTypeDeclarationPropertyTypeInferer, SetterTypeDeclarationPropertyTypeInferer $setterTypeDeclarationPropertyTypeInferer, MakePropertyTypedGuard $makePropertyTypedGuard, ReflectionResolver $reflectionResolver)
{
$this->getterTypeDeclarationPropertyTypeInferer = $getterTypeDeclarationPropertyTypeInferer;
$this->setterTypeDeclarationPropertyTypeInferer = $setterTypeDeclarationPropertyTypeInferer;
$this->makePropertyTypedGuard = $makePropertyTypedGuard;
$this->reflectionResolver = $reflectionResolver;
}
public function getRuleDefinition() : RuleDefinition
{
@ -96,6 +103,7 @@ CODE_SAMPLE
public function refactor(Node $node) : ?Node
{
$hasChanged = \false;
$classReflection = null;
foreach ($node->getProperties() as $property) {
if ($property->type instanceof Node) {
continue;
@ -110,7 +118,13 @@ CODE_SAMPLE
if (!$this->isDefaultExprTypeCompatible($property, $getterSetterPropertyType)) {
continue;
}
if (!$this->makePropertyTypedGuard->isLegal($property, \false)) {
if ($classReflection === null) {
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
}
if ($classReflection === null) {
return null;
}
if (!$this->makePropertyTypedGuard->isLegal($property, $classReflection, \false)) {
continue;
}
$propertyTypeDeclaration = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($getterSetterPropertyType, TypeKind::PROPERTY);

View File

@ -126,9 +126,6 @@ CODE_SAMPLE
$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;
@ -139,6 +136,9 @@ CODE_SAMPLE
if (!$classReflection instanceof ClassReflection) {
return null;
}
if (!$this->makePropertyTypedGuard->isLegal($property, $classReflection, $this->inlinePublic)) {
continue;
}
$inferredType = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection);
if (!$inferredType instanceof Type) {
continue;

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'a2c12ab0db6bb7d6207ea5a5c63ef5e6426cf0f0';
public const PACKAGE_VERSION = '141a94aa49a3f64e78e3dd2f2a4b1d91de425b8b';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-20 07:54:14';
public const RELEASE_DATE = '2023-07-20 07:20:17';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e::getLoader();
return ComposerAutoloaderInit99187f9671ed6291ffb5ad29a6b50ae0::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e
class ComposerAutoloaderInit99187f9671ed6291ffb5ad29a6b50ae0
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit99187f9671ed6291ffb5ad29a6b50ae0', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit21c4afe8327ea31dba51e0a196af480e', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit99187f9671ed6291ffb5ad29a6b50ae0', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit21c4afe8327ea31dba51e0a196af480e
class ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3023,9 +3023,9 @@ class ComposerStaticInit21c4afe8327ea31dba51e0a196af480e
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit21c4afe8327ea31dba51e0a196af480e::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit99187f9671ed6291ffb5ad29a6b50ae0::$classMap;
}, null, ClassLoader::class);
}