mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-04 02:10:51 +00:00
Updated Rector to commit 1836de97836c78ae4c85c1fdce3664688fb1011f
1836de9783
AddReturnTypeDeclarationBasedOnParentClassMethodRector (#2666)
This commit is contained in:
parent
e383186790
commit
1d657845a7
|
@ -14,6 +14,8 @@ use Rector\Core\PhpParser\AstResolver;
|
|||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
|
||||
use Rector\StaticTypeMapper\StaticTypeMapper;
|
||||
use Rector\TypeDeclaration\TypeInferer\ParamTypeInferer;
|
||||
use RectorPrefix202207\Symplify\SmartFileSystem\Normalizer\PathNormalizer;
|
||||
final class ParentClassMethodTypeOverrideGuard
|
||||
|
@ -43,13 +45,25 @@ final class ParentClassMethodTypeOverrideGuard
|
|||
* @var \Rector\Core\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, PathNormalizer $pathNormalizer, AstResolver $astResolver, ParamTypeInferer $paramTypeInferer, ReflectionResolver $reflectionResolver)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
|
||||
*/
|
||||
private $typeComparator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
||||
*/
|
||||
private $staticTypeMapper;
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, PathNormalizer $pathNormalizer, AstResolver $astResolver, ParamTypeInferer $paramTypeInferer, ReflectionResolver $reflectionResolver, TypeComparator $typeComparator, StaticTypeMapper $staticTypeMapper)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->pathNormalizer = $pathNormalizer;
|
||||
$this->astResolver = $astResolver;
|
||||
$this->paramTypeInferer = $paramTypeInferer;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
$this->typeComparator = $typeComparator;
|
||||
$this->staticTypeMapper = $staticTypeMapper;
|
||||
}
|
||||
public function isReturnTypeChangeAllowed(ClassMethod $classMethod) : bool
|
||||
{
|
||||
|
@ -139,4 +153,15 @@ final class ParentClassMethodTypeOverrideGuard
|
|||
}
|
||||
return null;
|
||||
}
|
||||
public function shouldSkipReturnTypeChange(ClassMethod $classMethod, Type $parentType) : bool
|
||||
{
|
||||
if ($classMethod->returnType === null) {
|
||||
return \false;
|
||||
}
|
||||
$currentReturnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($classMethod->returnType);
|
||||
if ($this->typeComparator->isSubtype($currentReturnType, $parentType)) {
|
||||
return \true;
|
||||
}
|
||||
return $this->typeComparator->areTypesEqual($currentReturnType, $parentType);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,143 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Core\Php\PhpVersionProvider;
|
||||
use Rector\Core\PhpParser\AstResolver;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
||||
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @changelog https://wiki.php.net/rfc/lsp_errors
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\AddReturnTypeDeclarationBasedOnParentClassMethodRectorTest
|
||||
*/
|
||||
final class AddReturnTypeDeclarationBasedOnParentClassMethodRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard
|
||||
*/
|
||||
private $parentClassMethodTypeOverrideGuard;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\AstResolver
|
||||
*/
|
||||
private $astResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Php\PhpVersionProvider
|
||||
*/
|
||||
private $phpVersionProvider;
|
||||
public function __construct(ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, AstResolver $astResolver, PhpVersionProvider $phpVersionProvider)
|
||||
{
|
||||
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
|
||||
$this->astResolver = $astResolver;
|
||||
$this->phpVersionProvider = $phpVersionProvider;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::FATAL_ERROR_ON_INCOMPATIBLE_METHOD_SIGNATURE;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add missing return type declaration based on parent class method', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class A
|
||||
{
|
||||
public function execute(): int
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A{
|
||||
public function execute()
|
||||
{
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class A
|
||||
{
|
||||
public function execute(): int
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class B extends A{
|
||||
public function execute(): int
|
||||
{
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($this->nodeNameResolver->isName($node, MethodName::CONSTRUCT)) {
|
||||
return null;
|
||||
}
|
||||
$parentMethodReflection = $this->parentClassMethodTypeOverrideGuard->getParentClassMethod($node);
|
||||
if (!$parentMethodReflection instanceof MethodReflection) {
|
||||
return null;
|
||||
}
|
||||
$parentClassMethod = $this->astResolver->resolveClassMethodFromMethodReflection($parentMethodReflection);
|
||||
if (!$parentClassMethod instanceof ClassMethod) {
|
||||
return null;
|
||||
}
|
||||
if ($parentClassMethod->isPrivate()) {
|
||||
return null;
|
||||
}
|
||||
$parentClassMethodReturnType = $parentClassMethod->getReturnType();
|
||||
if ($parentClassMethodReturnType === null) {
|
||||
return null;
|
||||
}
|
||||
$parentClassMethodReturnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($parentClassMethodReturnType);
|
||||
return $this->processClassMethodReturnType($node, $parentClassMethodReturnType);
|
||||
}
|
||||
private function processClassMethodReturnType(ClassMethod $classMethod, Type $parentType) : ?ClassMethod
|
||||
{
|
||||
if ($parentType instanceof MixedType) {
|
||||
$class = $classMethod->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if (!$class instanceof Class_) {
|
||||
return null;
|
||||
}
|
||||
$className = (string) $this->nodeNameResolver->getName($class);
|
||||
$currentObjectType = new ObjectType($className);
|
||||
if (!$parentType->equals($currentObjectType) && $classMethod->returnType !== null) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if ($parentType instanceof MixedType && !$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) {
|
||||
return null;
|
||||
}
|
||||
// already set and sub type or equal → no change
|
||||
if ($this->parentClassMethodTypeOverrideGuard->shouldSkipReturnTypeChange($classMethod, $parentType)) {
|
||||
return null;
|
||||
}
|
||||
$classMethod->returnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($parentType, TypeKind::RETURN);
|
||||
return $classMethod;
|
||||
}
|
||||
}
|
|
@ -15,9 +15,9 @@ use Rector\Core\Php\PhpVersionProvider;
|
|||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
|
||||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
||||
use Rector\TypeDeclaration\ValueObject\AddReturnTypeDeclaration;
|
||||
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202207\Webmozart\Assert\Assert;
|
||||
|
@ -34,20 +34,20 @@ final class AddReturnTypeDeclarationRector extends AbstractRector implements Con
|
|||
* @var bool
|
||||
*/
|
||||
private $hasChanged = \false;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
|
||||
*/
|
||||
private $typeComparator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Php\PhpVersionProvider
|
||||
*/
|
||||
private $phpVersionProvider;
|
||||
public function __construct(TypeComparator $typeComparator, PhpVersionProvider $phpVersionProvider)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard
|
||||
*/
|
||||
private $parentClassMethodTypeOverrideGuard;
|
||||
public function __construct(PhpVersionProvider $phpVersionProvider, ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard)
|
||||
{
|
||||
$this->typeComparator = $typeComparator;
|
||||
$this->phpVersionProvider = $phpVersionProvider;
|
||||
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -125,21 +125,10 @@ CODE_SAMPLE
|
|||
return;
|
||||
}
|
||||
// already set and sub type or equal → no change
|
||||
if ($this->shouldSkipType($classMethod, $newType)) {
|
||||
if ($this->parentClassMethodTypeOverrideGuard->shouldSkipReturnTypeChange($classMethod, $newType)) {
|
||||
return;
|
||||
}
|
||||
$classMethod->returnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($newType, TypeKind::RETURN);
|
||||
$this->hasChanged = \true;
|
||||
}
|
||||
private function shouldSkipType(ClassMethod $classMethod, Type $newType) : bool
|
||||
{
|
||||
if ($classMethod->returnType === null) {
|
||||
return \false;
|
||||
}
|
||||
$currentReturnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($classMethod->returnType);
|
||||
if ($this->typeComparator->isSubtype($currentReturnType, $newType)) {
|
||||
return \true;
|
||||
}
|
||||
return $this->typeComparator->areTypesEqual($currentReturnType, $newType);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,12 +17,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '764416672a588c4bd80084a853973773f9835690';
|
||||
public const PACKAGE_VERSION = '1836de97836c78ae4c85c1fdce3664688fb1011f';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-07-21 21:39:52';
|
||||
public const RELEASE_DATE = '2022-07-22 16:13:06';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit6cd82dbd1ec4ee06c157449933d58d31::getLoader();
|
||||
return ComposerAutoloaderInit5d55a55c4e6fa83dabbbc1ca0728fb98::getLoader();
|
||||
|
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
|
@ -3003,6 +3003,7 @@ return array(
|
|||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddArrayReturnDocTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddArrayReturnDocTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddMethodCallBasedStrictParamTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationBasedOnParentClassMethodRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationBasedOnParentClassMethodRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ArrayShapeFromConstantArrayReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ArrayShapeFromConstantArrayReturnRector.php',
|
||||
|
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit6cd82dbd1ec4ee06c157449933d58d31
|
||||
class ComposerAutoloaderInit5d55a55c4e6fa83dabbbc1ca0728fb98
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,19 +22,19 @@ class ComposerAutoloaderInit6cd82dbd1ec4ee06c157449933d58d31
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit6cd82dbd1ec4ee06c157449933d58d31', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit5d55a55c4e6fa83dabbbc1ca0728fb98', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit6cd82dbd1ec4ee06c157449933d58d31', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit5d55a55c4e6fa83dabbbc1ca0728fb98', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit6cd82dbd1ec4ee06c157449933d58d31::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit5d55a55c4e6fa83dabbbc1ca0728fb98::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit6cd82dbd1ec4ee06c157449933d58d31::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit5d55a55c4e6fa83dabbbc1ca0728fb98::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire6cd82dbd1ec4ee06c157449933d58d31($fileIdentifier, $file);
|
||||
composerRequire5d55a55c4e6fa83dabbbc1ca0728fb98($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
|
@ -46,7 +46,7 @@ class ComposerAutoloaderInit6cd82dbd1ec4ee06c157449933d58d31
|
|||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequire6cd82dbd1ec4ee06c157449933d58d31($fileIdentifier, $file)
|
||||
function composerRequire5d55a55c4e6fa83dabbbc1ca0728fb98($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
9
vendor/composer/autoload_static.php
vendored
9
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit6cd82dbd1ec4ee06c157449933d58d31
|
||||
class ComposerStaticInit5d55a55c4e6fa83dabbbc1ca0728fb98
|
||||
{
|
||||
public static $files = array (
|
||||
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
|
||||
|
@ -3310,6 +3310,7 @@ class ComposerStaticInit6cd82dbd1ec4ee06c157449933d58d31
|
|||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddArrayReturnDocTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddArrayReturnDocTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddMethodCallBasedStrictParamTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationBasedOnParentClassMethodRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationBasedOnParentClassMethodRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ArrayShapeFromConstantArrayReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ArrayShapeFromConstantArrayReturnRector.php',
|
||||
|
@ -3402,9 +3403,9 @@ class ComposerStaticInit6cd82dbd1ec4ee06c157449933d58d31
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit6cd82dbd1ec4ee06c157449933d58d31::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit6cd82dbd1ec4ee06c157449933d58d31::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit6cd82dbd1ec4ee06c157449933d58d31::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit5d55a55c4e6fa83dabbbc1ca0728fb98::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit5d55a55c4e6fa83dabbbc1ca0728fb98::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit5d55a55c4e6fa83dabbbc1ca0728fb98::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user