Updated Rector to commit 09fe7f5c1ebe6b6eacddc9765284dce921bd9191

09fe7f5c1e [TypeDeclaration] Deprecate ArrayShapeFromConstantArrayReturnRector as uses docblocks that we move away from since 0.15 (#4571)
This commit is contained in:
Tomas Votruba 2023-07-21 14:06:17 +00:00
parent 1721b7d304
commit 773600ee65
8 changed files with 15 additions and 156 deletions

View File

@ -13,7 +13,6 @@ use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProv
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromPropertyTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector;
use Rector\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarReturnsRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamAnnotationIncorrectNullableRector;
@ -42,5 +41,5 @@ use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodRe
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;
use Rector\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNullableRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rules([AddClosureReturnTypeRector::class, AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, ReturnAnnotationIncorrectNullableRector::class, VarAnnotationIncorrectNullableRector::class, ParamAnnotationIncorrectNullableRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ArrayShapeFromConstantArrayReturnRector::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]);
$rectorConfig->rules([AddClosureReturnTypeRector::class, AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, ReturnAnnotationIncorrectNullableRector::class, VarAnnotationIncorrectNullableRector::class, ParamAnnotationIncorrectNullableRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::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]);
};

View File

@ -1,32 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\PhpDocParser\TypeAnalyzer;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionVariant;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
/**
* @api
*/
final class ClassMethodReturnTypeResolver
{
public function resolve(ClassMethod $classMethod, Scope $scope) : Type
{
$methodName = $classMethod->name->toString();
$classReflection = $scope->getClassReflection();
if (!$classReflection instanceof ClassReflection) {
return new MixedType();
}
$extendedMethodReflection = $classReflection->getMethod($methodName, $scope);
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($extendedMethodReflection, $classMethod, $scope);
if (!$parametersAcceptor instanceof FunctionVariant) {
return new MixedType();
}
return $parametersAcceptor->getReturnType();
}
}

View File

@ -4,48 +4,16 @@ declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\NeverType;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Util\StringUtils;
use Rector\PhpDocParser\TypeAnalyzer\ClassMethodReturnTypeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\ArrayShapeFromConstantArrayReturnRectorTest
* @deprecated As we're moving from docblock types as unreliable and work only with native type declarations. See https://getrector.com/blog/new-in-rector-015-complete-safe-and-known-type-declarations
*/
final class ArrayShapeFromConstantArrayReturnRector extends AbstractScopeAwareRector
{
/**
* @readonly
* @var \Rector\PhpDocParser\TypeAnalyzer\ClassMethodReturnTypeResolver
*/
private $classMethodReturnTypeResolver;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger;
/**
* @see https://regex101.com/r/WvUD0m/2
* @var string
*/
private const SKIPPED_CHAR_REGEX = '#\\W#u';
public function __construct(ClassMethodReturnTypeResolver $classMethodReturnTypeResolver, PhpDocTypeChanger $phpDocTypeChanger)
{
$this->classMethodReturnTypeResolver = $classMethodReturnTypeResolver;
$this->phpDocTypeChanger = $phpDocTypeChanger;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add array shape exact types based on constant keys of array', [new CodeSample(<<<'CODE_SAMPLE'
@ -83,80 +51,6 @@ CODE_SAMPLE
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
if ($this->isInTestCase($scope)) {
return null;
}
/** @var Return_[] $returns */
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($node, Return_::class);
// exact one shape only
if (\count($returns) !== 1) {
return null;
}
$return = $returns[0];
if (!$return->expr instanceof Expr) {
return null;
}
$returnExprType = $this->getType($return->expr);
if (!$returnExprType instanceof ConstantArrayType) {
return null;
}
if ($this->shouldSkip($returnExprType)) {
return null;
}
$returnType = $this->classMethodReturnTypeResolver->resolve($node, $scope);
if ($returnType instanceof ConstantArrayType) {
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$returnExprTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($returnExprType);
if ($returnExprTypeNode instanceof GenericTypeNode) {
return null;
}
if ($returnExprTypeNode instanceof SpacingAwareArrayTypeNode) {
return null;
}
$hasChanged = $this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $returnExprType);
if (!$hasChanged) {
return null;
}
return $node;
}
private function shouldSkip(ConstantArrayType $constantArrayType) : bool
{
$keyType = $constantArrayType->getKeyType();
// empty array
if ($keyType instanceof NeverType) {
return \true;
}
$types = $keyType instanceof UnionType ? $keyType->getTypes() : [$keyType];
foreach ($types as $type) {
if (!$type instanceof ConstantStringType) {
continue;
}
$value = $type->getValue();
if (\trim($value) === '') {
return \true;
}
if (StringUtils::isMatch($value, self::SKIPPED_CHAR_REGEX)) {
return \true;
}
}
$itemType = $constantArrayType->getItemType();
if ($itemType instanceof ConstantArrayType) {
return $this->shouldSkip($itemType);
}
return \false;
}
/**
* Skip test case, as return methods there are usually with test data only.
* Those arrays are hand made and return types are getting complex and messy, so this rule should skip it.
*/
private function isInTestCase(Scope $scope) : bool
{
$classReflection = $scope->getClassReflection();
if (!$classReflection instanceof ClassReflection) {
return \false;
}
return $classReflection->isSubclassOf('PHPUnit\\Framework\\TestCase');
return null;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'bff797fe086caa492f421e74370add4ca78fd191';
public const PACKAGE_VERSION = '09fe7f5c1ebe6b6eacddc9765284dce921bd9191';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-21 12:19:50';
public const RELEASE_DATE = '2023-07-21 15:02: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 ComposerAutoloaderInitaf07ce2d5379a3d57393cfdea2017acd::getLoader();
return ComposerAutoloaderInit06cb2dba81e4322cc21547273306baaf::getLoader();

View File

@ -2315,7 +2315,6 @@ return array(
'Rector\\PhpDocParser\\PhpDocParser\\ValueObject\\PhpDocAttributeKey' => $baseDir . '/packages/PhpDocParser/PhpDocParser/ValueObject/PhpDocAttributeKey.php',
'Rector\\PhpDocParser\\PhpParser\\SmartPhpParser' => $baseDir . '/packages/PhpDocParser/PhpParser/SmartPhpParser.php',
'Rector\\PhpDocParser\\PhpParser\\SmartPhpParserFactory' => $baseDir . '/packages/PhpDocParser/PhpParser/SmartPhpParserFactory.php',
'Rector\\PhpDocParser\\TypeAnalyzer\\ClassMethodReturnTypeResolver' => $baseDir . '/packages/PhpDocParser/TypeAnalyzer/ClassMethodReturnTypeResolver.php',
'Rector\\PhpDocParser\\ValueObject\\AttributeKey' => $baseDir . '/packages/PhpDocParser/ValueObject/AttributeKey.php',
'Rector\\PostRector\\Application\\PostFileProcessor' => $baseDir . '/packages/PostRector/Application/PostFileProcessor.php',
'Rector\\PostRector\\Collector\\UseNodesToAddCollector' => $baseDir . '/packages/PostRector/Collector/UseNodesToAddCollector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitaf07ce2d5379a3d57393cfdea2017acd
class ComposerAutoloaderInit06cb2dba81e4322cc21547273306baaf
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitaf07ce2d5379a3d57393cfdea2017acd
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitaf07ce2d5379a3d57393cfdea2017acd', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit06cb2dba81e4322cc21547273306baaf', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitaf07ce2d5379a3d57393cfdea2017acd', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit06cb2dba81e4322cc21547273306baaf', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitaf07ce2d5379a3d57393cfdea2017acd::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit06cb2dba81e4322cc21547273306baaf::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitaf07ce2d5379a3d57393cfdea2017acd::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit06cb2dba81e4322cc21547273306baaf::$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 ComposerStaticInitaf07ce2d5379a3d57393cfdea2017acd
class ComposerStaticInit06cb2dba81e4322cc21547273306baaf
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2569,7 +2569,6 @@ class ComposerStaticInitaf07ce2d5379a3d57393cfdea2017acd
'Rector\\PhpDocParser\\PhpDocParser\\ValueObject\\PhpDocAttributeKey' => __DIR__ . '/../..' . '/packages/PhpDocParser/PhpDocParser/ValueObject/PhpDocAttributeKey.php',
'Rector\\PhpDocParser\\PhpParser\\SmartPhpParser' => __DIR__ . '/../..' . '/packages/PhpDocParser/PhpParser/SmartPhpParser.php',
'Rector\\PhpDocParser\\PhpParser\\SmartPhpParserFactory' => __DIR__ . '/../..' . '/packages/PhpDocParser/PhpParser/SmartPhpParserFactory.php',
'Rector\\PhpDocParser\\TypeAnalyzer\\ClassMethodReturnTypeResolver' => __DIR__ . '/../..' . '/packages/PhpDocParser/TypeAnalyzer/ClassMethodReturnTypeResolver.php',
'Rector\\PhpDocParser\\ValueObject\\AttributeKey' => __DIR__ . '/../..' . '/packages/PhpDocParser/ValueObject/AttributeKey.php',
'Rector\\PostRector\\Application\\PostFileProcessor' => __DIR__ . '/../..' . '/packages/PostRector/Application/PostFileProcessor.php',
'Rector\\PostRector\\Collector\\UseNodesToAddCollector' => __DIR__ . '/../..' . '/packages/PostRector/Collector/UseNodesToAddCollector.php',
@ -3022,9 +3021,9 @@ class ComposerStaticInitaf07ce2d5379a3d57393cfdea2017acd
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitaf07ce2d5379a3d57393cfdea2017acd::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitaf07ce2d5379a3d57393cfdea2017acd::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitaf07ce2d5379a3d57393cfdea2017acd::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit06cb2dba81e4322cc21547273306baaf::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit06cb2dba81e4322cc21547273306baaf::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit06cb2dba81e4322cc21547273306baaf::$classMap;
}, null, ClassLoader::class);
}