mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit 76611f69af76327be04538df7b8c568600ac58c0
76611f69af
Deprecate TypedPropertyFromStrictConstructorReadonlyClassRector as based on docblock and public elements; use private elements and property promotion instead (#5049)
This commit is contained in:
parent
09bdee3938
commit
db78ff1331
|
@ -36,10 +36,9 @@ use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector;
|
|||
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;
|
||||
use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
|
||||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
|
||||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorReadonlyClassRector;
|
||||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
|
||||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rules([AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorReadonlyClassRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, ReturnTypeFromStrictParamRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class, ReturnTypeFromStrictTernaryRector::class, BoolReturnTypeFromStrictScalarReturnsRector::class, NumericReturnTypeFromStrictScalarReturnsRector::class, StrictArrayParamDimFetchRector::class, ReturnUnionTypeRector::class, StrictStringParamConcatRector::class]);
|
||||
$rectorConfig->rules([AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, ReturnTypeFromStrictParamRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class, ReturnTypeFromStrictTernaryRector::class, BoolReturnTypeFromStrictScalarReturnsRector::class, NumericReturnTypeFromStrictScalarReturnsRector::class, StrictArrayParamDimFetchRector::class, ReturnUnionTypeRector::class, StrictStringParamConcatRector::class]);
|
||||
};
|
||||
|
|
|
@ -5,77 +5,19 @@ namespace Rector\TypeDeclaration\Rector\Property;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Reflection\Php\PhpPropertyReflection;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\Rector\AbstractScopeAwareRector;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
|
||||
use Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer;
|
||||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
||||
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
|
||||
use Rector\TypeDeclaration\Guard\PropertyTypeOverrideGuard;
|
||||
use Rector\TypeDeclaration\TypeAnalyzer\PropertyTypeDefaultValueAnalyzer;
|
||||
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorReadonlyClassRector\TypedPropertyFromStrictConstructorReadonlyClassRectorTest
|
||||
* @deprecated This rule is deprecated, as based on docblock and public elements,
|
||||
* use private properties and property promotion instead to use PHP language features.
|
||||
*/
|
||||
final class TypedPropertyFromStrictConstructorReadonlyClassRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer
|
||||
*/
|
||||
private $trustedClassMethodPropertyTypeInferer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover
|
||||
*/
|
||||
private $varTagRemover;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
|
||||
*/
|
||||
private $constructorAssignDetector;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\Guard\PropertyTypeOverrideGuard
|
||||
*/
|
||||
private $propertyTypeOverrideGuard;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PHPStanStaticTypeMapper\DoctrineTypeAnalyzer
|
||||
*/
|
||||
private $doctrineTypeAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\TypeAnalyzer\PropertyTypeDefaultValueAnalyzer
|
||||
*/
|
||||
private $propertyTypeDefaultValueAnalyzer;
|
||||
public function __construct(TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer, VarTagRemover $varTagRemover, ConstructorAssignDetector $constructorAssignDetector, PropertyTypeOverrideGuard $propertyTypeOverrideGuard, ReflectionResolver $reflectionResolver, DoctrineTypeAnalyzer $doctrineTypeAnalyzer, PropertyTypeDefaultValueAnalyzer $propertyTypeDefaultValueAnalyzer)
|
||||
{
|
||||
$this->trustedClassMethodPropertyTypeInferer = $trustedClassMethodPropertyTypeInferer;
|
||||
$this->varTagRemover = $varTagRemover;
|
||||
$this->constructorAssignDetector = $constructorAssignDetector;
|
||||
$this->propertyTypeOverrideGuard = $propertyTypeOverrideGuard;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
$this->doctrineTypeAnalyzer = $doctrineTypeAnalyzer;
|
||||
$this->propertyTypeDefaultValueAnalyzer = $propertyTypeDefaultValueAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add typed public properties based only on strict constructor types in readonly classes', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
|
@ -120,69 +62,11 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
||||
{
|
||||
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
|
||||
if (!$constructClassMethod instanceof ClassMethod || $node->getProperties() === []) {
|
||||
return null;
|
||||
}
|
||||
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
|
||||
if (!$classReflection instanceof ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
$hasChanged = \false;
|
||||
foreach ($node->getProperties() as $property) {
|
||||
if (!$this->propertyTypeOverrideGuard->isLegal($property, $classReflection)) {
|
||||
continue;
|
||||
}
|
||||
$propertyType = $this->trustedClassMethodPropertyTypeInferer->inferProperty($node, $property, $constructClassMethod);
|
||||
if ($this->shouldSkipProperty($property, $propertyType, $classReflection, $scope)) {
|
||||
continue;
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
|
||||
$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY);
|
||||
if (!$propertyTypeNode instanceof Node) {
|
||||
continue;
|
||||
}
|
||||
$propertyProperty = $property->props[0];
|
||||
$propertyName = $this->nodeNameResolver->getName($property);
|
||||
if ($this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) {
|
||||
$propertyProperty->default = null;
|
||||
$hasChanged = \true;
|
||||
}
|
||||
if ($this->propertyTypeDefaultValueAnalyzer->doesConflictWithDefaultValue($propertyProperty, $propertyType)) {
|
||||
continue;
|
||||
}
|
||||
$property->type = $propertyTypeNode;
|
||||
$this->varTagRemover->removeVarTagIfUseless($phpDocInfo, $property);
|
||||
$hasChanged = \true;
|
||||
}
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
}
|
||||
\trigger_error(\sprintf('The "%s" rule is deprecated as based on docblock and public elements. Use private properties and property promotion rules instead.', self::class), \E_USER_ERROR);
|
||||
return null;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::TYPED_PROPERTIES;
|
||||
}
|
||||
private function shouldSkipProperty(Property $property, Type $propertyType, ClassReflection $classReflection, Scope $scope) : bool
|
||||
{
|
||||
if (!$property->isPublic()) {
|
||||
return \true;
|
||||
}
|
||||
if ($propertyType instanceof MixedType) {
|
||||
return \true;
|
||||
}
|
||||
if ($this->doctrineTypeAnalyzer->isInstanceOfCollectionType($propertyType)) {
|
||||
return \true;
|
||||
}
|
||||
$isReadOnlyByPhpdoc = \false;
|
||||
$propertyName = $this->nodeNameResolver->getName($property);
|
||||
if ($classReflection->hasProperty($propertyName)) {
|
||||
$propertyReflection = $classReflection->getProperty($propertyName, $scope);
|
||||
if ($propertyReflection instanceof PhpPropertyReflection) {
|
||||
$isReadOnlyByPhpdoc = $propertyReflection->isReadOnlyByPhpDoc();
|
||||
}
|
||||
}
|
||||
return !$isReadOnlyByPhpdoc;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '765e849e612553708296dd501375b49784ad1ca8';
|
||||
public const PACKAGE_VERSION = '76611f69af76327be04538df7b8c568600ac58c0';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-09-19 13:36:51';
|
||||
public const RELEASE_DATE = '2023-09-19 13:41:52';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitb69c7fa0bff34fe0f24889d62131edd3::getLoader();
|
||||
return ComposerAutoloaderInitf9c52d911e61ea754f759891a6eaa5cd::getLoader();
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitb69c7fa0bff34fe0f24889d62131edd3
|
||||
class ComposerAutoloaderInitf9c52d911e61ea754f759891a6eaa5cd
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInitb69c7fa0bff34fe0f24889d62131edd3
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitb69c7fa0bff34fe0f24889d62131edd3', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitf9c52d911e61ea754f759891a6eaa5cd', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitb69c7fa0bff34fe0f24889d62131edd3', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitf9c52d911e61ea754f759891a6eaa5cd', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitb69c7fa0bff34fe0f24889d62131edd3::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitf9c52d911e61ea754f759891a6eaa5cd::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitb69c7fa0bff34fe0f24889d62131edd3::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitf9c52d911e61ea754f759891a6eaa5cd::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitb69c7fa0bff34fe0f24889d62131edd3
|
||||
class ComposerStaticInitf9c52d911e61ea754f759891a6eaa5cd
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -2601,9 +2601,9 @@ class ComposerStaticInitb69c7fa0bff34fe0f24889d62131edd3
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitb69c7fa0bff34fe0f24889d62131edd3::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitb69c7fa0bff34fe0f24889d62131edd3::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitb69c7fa0bff34fe0f24889d62131edd3::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitf9c52d911e61ea754f759891a6eaa5cd::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitf9c52d911e61ea754f759891a6eaa5cd::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitf9c52d911e61ea754f759891a6eaa5cd::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue