Updated Rector to commit 650a8d6a13ee2edf5d9aa2b3aa0cfada95589fe4

650a8d6a13 [DowngradePhp80] Add #[\ReturnTypeWillChange] on DowngradeMixedTypeDeclarationRector on implements ArrayAccess (#1544)
This commit is contained in:
Tomas Votruba 2021-12-22 14:55:49 +00:00
parent c2bad56285
commit 163ad0b1a3
6 changed files with 89 additions and 21 deletions

View File

@ -3,24 +3,39 @@
declare (strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocParser;
use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\PhpAttribute\Printer\PhpAttributeGroupFactory;
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
use Rector\StaticTypeMapper\StaticTypeMapper;
use RectorPrefix20211222\ReturnTypeWillChange;
final class PhpDocFromTypeDeclarationDecorator
{
/**
* @var class-string<ReturnTypeWillChange>
*/
private const RETURN_TYPE_WILL_CHANGE_ATTRIBUTE = 'ReturnTypeWillChange';
/**
* @var array<string, array<string, string[]>>
*/
private const ADD_RETURN_TYPE_WILL_CHANGE = ['PHPStan\\Type\\MixedType' => ['ArrayAccess' => ['offsetGet']]];
/**
* @readonly
* @var \Rector\StaticTypeMapper\StaticTypeMapper
@ -46,13 +61,31 @@ final class PhpDocFromTypeDeclarationDecorator
* @var \Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper
*/
private $typeUnwrapper;
public function __construct(\Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper, \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory $phpDocInfoFactory, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger, \Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper $typeUnwrapper)
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
/**
* @readonly
* @var \Rector\PhpAttribute\Printer\PhpAttributeGroupFactory
*/
private $phpAttributeGroupFactory;
/**
* @readonly
* @var \Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer
*/
private $phpAttributeAnalyzer;
public function __construct(\Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper, \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory $phpDocInfoFactory, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger, \Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper $typeUnwrapper, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\PhpAttribute\Printer\PhpAttributeGroupFactory $phpAttributeGroupFactory, \Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer $phpAttributeAnalyzer)
{
$this->staticTypeMapper = $staticTypeMapper;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->nodeNameResolver = $nodeNameResolver;
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->typeUnwrapper = $typeUnwrapper;
$this->betterNodeFinder = $betterNodeFinder;
$this->phpAttributeGroupFactory = $phpAttributeGroupFactory;
$this->phpAttributeAnalyzer = $phpAttributeAnalyzer;
}
/**
* @param \PhpParser\Node\Expr\ArrowFunction|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
@ -66,6 +99,18 @@ final class PhpDocFromTypeDeclarationDecorator
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);
$this->phpDocTypeChanger->changeReturnType($phpDocInfo, $type);
$functionLike->returnType = null;
if (!$functionLike instanceof \PhpParser\Node\Stmt\ClassMethod) {
return;
}
$classLike = $this->betterNodeFinder->findParentByTypes($functionLike, [\PhpParser\Node\Stmt\Class_::class, \PhpParser\Node\Stmt\Interface_::class]);
if (!$classLike instanceof \PhpParser\Node\Stmt\ClassLike) {
return;
}
if (!$this->isRequireReturnTypeWillChange(\get_class($type), $classLike, $functionLike)) {
return;
}
$attributeGroup = $this->phpAttributeGroupFactory->createFromClass(self::RETURN_TYPE_WILL_CHANGE_ATTRIBUTE);
$functionLike->attrGroups[] = $attributeGroup;
}
/**
* @param array<class-string<Type>> $requiredTypes
@ -111,6 +156,29 @@ final class PhpDocFromTypeDeclarationDecorator
$this->decorate($functionLike);
return \true;
}
private function isRequireReturnTypeWillChange(string $type, \PhpParser\Node\Stmt\ClassLike $classLike, \PhpParser\Node\Stmt\ClassMethod $classMethod) : bool
{
if (!\array_key_exists($type, self::ADD_RETURN_TYPE_WILL_CHANGE)) {
return \false;
}
$className = (string) $this->nodeNameResolver->getName($classLike);
$objectClass = new \PHPStan\Type\ObjectType($className);
$methodName = $this->nodeNameResolver->getName($classMethod);
foreach (self::ADD_RETURN_TYPE_WILL_CHANGE[$type] as $class => $methods) {
$objectClassConfig = new \PHPStan\Type\ObjectType($class);
if (!$objectClassConfig->isSuperTypeOf($objectClass)->yes()) {
continue;
}
if (!\in_array($methodName, $methods, \true)) {
continue;
}
if ($this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, self::RETURN_TYPE_WILL_CHANGE_ATTRIBUTE)) {
continue;
}
return \true;
}
return \false;
}
/**
* @param \PhpParser\Node\ComplexType|\PhpParser\Node\Identifier|\PhpParser\Node\Name $typeNode
*/

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = 'd1e885a03af4a695e3471bb50c5c4784d678e3ee';
public const PACKAGE_VERSION = '650a8d6a13ee2edf5d9aa2b3aa0cfada95589fe4';
/**
* @var string
*/
public const RELEASE_DATE = '2021-12-22 15:35:50';
public const RELEASE_DATE = '2021-12-22 15:39:11';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20211222\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3::getLoader();
return ComposerAutoloaderInitcceed78b06956f016e55e75d9e36f8fa::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3
class ComposerAutoloaderInitcceed78b06956f016e55e75d9e36f8fa
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitcceed78b06956f016e55e75d9e36f8fa', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitcceed78b06956f016e55e75d9e36f8fa', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitfcb4b38e24b26dc522fd5383429607c3::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitcceed78b06956f016e55e75d9e36f8fa::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,12 +42,12 @@ class ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInitfcb4b38e24b26dc522fd5383429607c3::$files;
$includeFiles = Composer\Autoload\ComposerStaticInitcceed78b06956f016e55e75d9e36f8fa::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequirefcb4b38e24b26dc522fd5383429607c3($fileIdentifier, $file);
composerRequirecceed78b06956f016e55e75d9e36f8fa($fileIdentifier, $file);
}
return $loader;
@ -59,7 +59,7 @@ class ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3
* @param string $file
* @return void
*/
function composerRequirefcb4b38e24b26dc522fd5383429607c3($fileIdentifier, $file)
function composerRequirecceed78b06956f016e55e75d9e36f8fa($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 ComposerStaticInitfcb4b38e24b26dc522fd5383429607c3
class ComposerStaticInitcceed78b06956f016e55e75d9e36f8fa
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -3836,9 +3836,9 @@ class ComposerStaticInitfcb4b38e24b26dc522fd5383429607c3
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitfcb4b38e24b26dc522fd5383429607c3::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitfcb4b38e24b26dc522fd5383429607c3::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitfcb4b38e24b26dc522fd5383429607c3::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitcceed78b06956f016e55e75d9e36f8fa::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitcceed78b06956f016e55e75d9e36f8fa::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitcceed78b06956f016e55e75d9e36f8fa::$classMap;
}, null, ClassLoader::class);
}

View File

@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20211222\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3', false) && !interface_exists('ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3', false) && !trait_exists('ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3', false)) {
spl_autoload_call('RectorPrefix20211222\ComposerAutoloaderInitfcb4b38e24b26dc522fd5383429607c3');
if (!class_exists('ComposerAutoloaderInitcceed78b06956f016e55e75d9e36f8fa', false) && !interface_exists('ComposerAutoloaderInitcceed78b06956f016e55e75d9e36f8fa', false) && !trait_exists('ComposerAutoloaderInitcceed78b06956f016e55e75d9e36f8fa', false)) {
spl_autoload_call('RectorPrefix20211222\ComposerAutoloaderInitcceed78b06956f016e55e75d9e36f8fa');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20211222\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -78,9 +78,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20211222\print_node(...func_get_args());
}
}
if (!function_exists('composerRequirefcb4b38e24b26dc522fd5383429607c3')) {
function composerRequirefcb4b38e24b26dc522fd5383429607c3() {
return \RectorPrefix20211222\composerRequirefcb4b38e24b26dc522fd5383429607c3(...func_get_args());
if (!function_exists('composerRequirecceed78b06956f016e55e75d9e36f8fa')) {
function composerRequirecceed78b06956f016e55e75d9e36f8fa() {
return \RectorPrefix20211222\composerRequirecceed78b06956f016e55e75d9e36f8fa(...func_get_args());
}
}
if (!function_exists('scanPath')) {