Updated Rector to commit 1c76f252599680d1ea8d3cd934c0a47a0cdfcbb0

1c76f25259 [Privatization][TypeDeclaration] Invalid return type on use RepeatedLiteralToClassConstantRector + ReturnTypeDeclarationRector (#457)
This commit is contained in:
Tomas Votruba 2021-07-19 08:07:40 +00:00
parent 767752c16e
commit 550e40fa60
7 changed files with 114 additions and 23 deletions

View File

@ -4,26 +4,45 @@ declare (strict_types=1);
namespace Rector\TypeDeclaration\TypeAnalyzer; namespace Rector\TypeDeclaration\TypeAnalyzer;
use PHPStan\Reflection\ReflectionProvider; use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantStringType; use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType; use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType; use PHPStan\Type\ObjectType;
use PHPStan\Type\Type; use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser; use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use Rector\Core\Configuration\Option;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use RectorPrefix20210719\Symplify\PackageBuilder\Parameter\ParameterProvider;
final class GenericClassStringTypeNormalizer final class GenericClassStringTypeNormalizer
{ {
/** /**
* @var \PHPStan\Reflection\ReflectionProvider * @var \PHPStan\Reflection\ReflectionProvider
*/ */
private $reflectionProvider; private $reflectionProvider;
public function __construct(\PHPStan\Reflection\ReflectionProvider $reflectionProvider) /**
* @var \Symplify\PackageBuilder\Parameter\ParameterProvider
*/
private $parameterProvider;
public function __construct(\PHPStan\Reflection\ReflectionProvider $reflectionProvider, \RectorPrefix20210719\Symplify\PackageBuilder\Parameter\ParameterProvider $parameterProvider)
{ {
$this->reflectionProvider = $reflectionProvider; $this->reflectionProvider = $reflectionProvider;
$this->parameterProvider = $parameterProvider;
} }
public function normalize(\PHPStan\Type\Type $type) : \PHPStan\Type\Type public function normalize(\PHPStan\Type\Type $type) : \PHPStan\Type\Type
{ {
return \PHPStan\Type\TypeTraverser::map($type, function (\PHPStan\Type\Type $type, $callback) : Type { $isAutoImport = $this->parameterProvider->provideBoolParameter(\Rector\Core\Configuration\Option::AUTO_IMPORT_NAMES);
return \PHPStan\Type\TypeTraverser::map($type, function (\PHPStan\Type\Type $type, $callback) use($isAutoImport) : Type {
if (!$type instanceof \PHPStan\Type\Constant\ConstantStringType) { if (!$type instanceof \PHPStan\Type\Constant\ConstantStringType) {
return $callback($type); $callbackType = $callback($type);
if ($callbackType instanceof \PHPStan\Type\ArrayType) {
return $callbackType;
}
$typeWithFullyQualifiedObjectType = $this->verifyAutoImportedFullyQualifiedType($type, $isAutoImport);
if ($typeWithFullyQualifiedObjectType instanceof \PHPStan\Type\Type) {
return $typeWithFullyQualifiedObjectType;
}
return $callbackType;
} }
// skip string that look like classe // skip string that look like classe
if ($type->getValue() === 'error') { if ($type->getValue() === 'error') {
@ -35,4 +54,34 @@ final class GenericClassStringTypeNormalizer
return new \PHPStan\Type\Generic\GenericClassStringType(new \PHPStan\Type\ObjectType($type->getValue())); return new \PHPStan\Type\Generic\GenericClassStringType(new \PHPStan\Type\ObjectType($type->getValue()));
}); });
} }
private function verifyAutoImportedFullyQualifiedType(\PHPStan\Type\Type $type, bool $isAutoImport) : ?\PHPStan\Type\Type
{
if ($type instanceof \PHPStan\Type\UnionType) {
$unionTypes = $type->getTypes();
$types = [];
$hasFullyQualifiedObjectType = \false;
foreach ($unionTypes as $unionType) {
if ($this->isAutoImportFullyQualifiedObjectType($unionType, $isAutoImport)) {
/** @var FullyQualifiedObjectType $unionType */
$types[] = new \PHPStan\Type\Generic\GenericClassStringType(new \PHPStan\Type\ObjectType($unionType->getClassName()));
$hasFullyQualifiedObjectType = \true;
continue;
}
$types[] = $unionType;
}
if ($hasFullyQualifiedObjectType) {
return new \PHPStan\Type\UnionType($types);
}
return $type;
}
if ($this->isAutoImportFullyQualifiedObjectType($type, $isAutoImport)) {
/** @var FullyQualifiedObjectType $type */
return new \PHPStan\Type\Generic\GenericClassStringType(new \PHPStan\Type\ObjectType($type->getClassName()));
}
return null;
}
private function isAutoImportFullyQualifiedObjectType(\PHPStan\Type\Type $type, bool $isAutoImport) : bool
{
return $isAutoImport && $type instanceof \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType && \strpos($type->getClassName(), '\\') === \false;
}
} }

View File

@ -3,11 +3,16 @@
declare (strict_types=1); declare (strict_types=1);
namespace Rector\TypeDeclaration\TypeInferer; namespace Rector\TypeDeclaration\TypeInferer;
use PhpParser\Node;
use PhpParser\Node\FunctionLike; use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\Type\MixedType; use PHPStan\Type\MixedType;
use PHPStan\Type\ThisType; use PHPStan\Type\ThisType;
use PHPStan\Type\Type; use PHPStan\Type\Type;
use PHPStan\Type\UnionType; use PHPStan\Type\UnionType;
use Rector\Core\Configuration\Option;
use Rector\Core\Php\PhpVersionProvider; use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature; use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType; use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
@ -15,6 +20,7 @@ use Rector\TypeDeclaration\Contract\TypeInferer\ReturnTypeInfererInterface;
use Rector\TypeDeclaration\Sorter\TypeInfererSorter; use Rector\TypeDeclaration\Sorter\TypeInfererSorter;
use Rector\TypeDeclaration\TypeAnalyzer\GenericClassStringTypeNormalizer; use Rector\TypeDeclaration\TypeAnalyzer\GenericClassStringTypeNormalizer;
use Rector\TypeDeclaration\TypeNormalizer; use Rector\TypeDeclaration\TypeNormalizer;
use RectorPrefix20210719\Symplify\PackageBuilder\Parameter\ParameterProvider;
final class ReturnTypeInferer final class ReturnTypeInferer
{ {
/** /**
@ -33,14 +39,19 @@ final class ReturnTypeInferer
* @var \Rector\Core\Php\PhpVersionProvider * @var \Rector\Core\Php\PhpVersionProvider
*/ */
private $phpVersionProvider; private $phpVersionProvider;
/**
* @var \Symplify\PackageBuilder\Parameter\ParameterProvider
*/
private $parameterProvider;
/** /**
* @param ReturnTypeInfererInterface[] $returnTypeInferers * @param ReturnTypeInfererInterface[] $returnTypeInferers
*/ */
public function __construct(array $returnTypeInferers, \Rector\TypeDeclaration\TypeNormalizer $typeNormalizer, \Rector\TypeDeclaration\Sorter\TypeInfererSorter $typeInfererSorter, \Rector\TypeDeclaration\TypeAnalyzer\GenericClassStringTypeNormalizer $genericClassStringTypeNormalizer, \Rector\Core\Php\PhpVersionProvider $phpVersionProvider) public function __construct(array $returnTypeInferers, \Rector\TypeDeclaration\TypeNormalizer $typeNormalizer, \Rector\TypeDeclaration\Sorter\TypeInfererSorter $typeInfererSorter, \Rector\TypeDeclaration\TypeAnalyzer\GenericClassStringTypeNormalizer $genericClassStringTypeNormalizer, \Rector\Core\Php\PhpVersionProvider $phpVersionProvider, \RectorPrefix20210719\Symplify\PackageBuilder\Parameter\ParameterProvider $parameterProvider)
{ {
$this->typeNormalizer = $typeNormalizer; $this->typeNormalizer = $typeNormalizer;
$this->genericClassStringTypeNormalizer = $genericClassStringTypeNormalizer; $this->genericClassStringTypeNormalizer = $genericClassStringTypeNormalizer;
$this->phpVersionProvider = $phpVersionProvider; $this->phpVersionProvider = $phpVersionProvider;
$this->parameterProvider = $parameterProvider;
$this->returnTypeInferers = $typeInfererSorter->sort($returnTypeInferers); $this->returnTypeInferers = $typeInfererSorter->sort($returnTypeInferers);
} }
public function inferFunctionLike(\PhpParser\Node\FunctionLike $functionLike) : \PHPStan\Type\Type public function inferFunctionLike(\PhpParser\Node\FunctionLike $functionLike) : \PHPStan\Type\Type
@ -53,6 +64,11 @@ final class ReturnTypeInferer
public function inferFunctionLikeWithExcludedInferers(\PhpParser\Node\FunctionLike $functionLike, array $excludedInferers) : \PHPStan\Type\Type public function inferFunctionLikeWithExcludedInferers(\PhpParser\Node\FunctionLike $functionLike, array $excludedInferers) : \PHPStan\Type\Type
{ {
$isSupportedStaticReturnType = $this->phpVersionProvider->isAtLeastPhpVersion(\Rector\Core\ValueObject\PhpVersionFeature::STATIC_RETURN_TYPE); $isSupportedStaticReturnType = $this->phpVersionProvider->isAtLeastPhpVersion(\Rector\Core\ValueObject\PhpVersionFeature::STATIC_RETURN_TYPE);
$isAutoImport = $this->parameterProvider->provideBoolParameter(\Rector\Core\Configuration\Option::AUTO_IMPORT_NAMES);
$isAutoImportFullyQuafiedReturn = $this->isAutoImportWithFullyQualifiedReturn($isAutoImport, $functionLike);
if ($isAutoImportFullyQuafiedReturn) {
return new \PHPStan\Type\MixedType();
}
foreach ($this->returnTypeInferers as $returnTypeInferer) { foreach ($this->returnTypeInferers as $returnTypeInferer) {
if ($this->shouldSkipExcludedTypeInferer($returnTypeInferer, $excludedInferers)) { if ($this->shouldSkipExcludedTypeInferer($returnTypeInferer, $excludedInferers)) {
continue; continue;
@ -107,6 +123,32 @@ final class ReturnTypeInferer
} }
return new \PHPStan\Type\UnionType($types); return new \PHPStan\Type\UnionType($types);
} }
private function isAutoImportWithFullyQualifiedReturn(bool $isAutoImport, \PhpParser\Node\FunctionLike $functionLike) : bool
{
if (!$isAutoImport) {
return \false;
}
if (!$functionLike instanceof \PhpParser\Node\Stmt\ClassMethod) {
return \false;
}
if ($this->isNamespacedFullyQualified($functionLike->returnType)) {
return \true;
}
if (!$functionLike->returnType instanceof \PhpParser\Node\UnionType) {
return \false;
}
$types = $functionLike->returnType->types;
foreach ($types as $type) {
if ($this->isNamespacedFullyQualified($type)) {
return \true;
}
}
return \false;
}
private function isNamespacedFullyQualified(?\PhpParser\Node $node) : bool
{
return $node instanceof \PhpParser\Node\Name\FullyQualified && \strpos($node->toString(), '\\') !== \false;
}
private function isStaticType(\PHPStan\Type\Type $type) : bool private function isStaticType(\PHPStan\Type\Type $type) : bool
{ {
return $type instanceof \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType && $type->getClassName() === 'static'; return $type instanceof \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType && $type->getClassName() === 'static';

View File

@ -16,11 +16,11 @@ final class VersionResolver
/** /**
* @var string * @var string
*/ */
public const PACKAGE_VERSION = 'bb41d721161826eb0fc18373b1ec99cd2e01a25d'; public const PACKAGE_VERSION = '1c76f252599680d1ea8d3cd934c0a47a0cdfcbb0';
/** /**
* @var string * @var string
*/ */
public const RELEASE_DATE = '2021-07-19 08:24:11'; public const RELEASE_DATE = '2021-07-19 14:56:42';
public static function resolvePackageVersion() : string public static function resolvePackageVersion() : string
{ {
$process = new \RectorPrefix20210719\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__); $process = new \RectorPrefix20210719\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'; require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae::getLoader(); return ComposerAutoloaderInit02e8a54708336a15d0794a2b94d910b7::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer // autoload_real.php @generated by Composer
class ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae class ComposerAutoloaderInit02e8a54708336a15d0794a2b94d910b7
{ {
private static $loader; private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae
return self::$loader; return self::$loader;
} }
spl_autoload_register(array('ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae', 'loadClassLoader'), true, true); spl_autoload_register(array('ComposerAutoloaderInit02e8a54708336a15d0794a2b94d910b7', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__))); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae', 'loadClassLoader')); spl_autoload_unregister(array('ComposerAutoloaderInit02e8a54708336a15d0794a2b94d910b7', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) { if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php'; require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit37f8d5a7ba1c21ff17f8f75ac79c08ae::getInitializer($loader)); call_user_func(\Composer\Autoload\ComposerStaticInit02e8a54708336a15d0794a2b94d910b7::getInitializer($loader));
} else { } else {
$classMap = require __DIR__ . '/autoload_classmap.php'; $classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) { if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae
$loader->register(true); $loader->register(true);
if ($useStaticLoader) { if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit37f8d5a7ba1c21ff17f8f75ac79c08ae::$files; $includeFiles = Composer\Autoload\ComposerStaticInit02e8a54708336a15d0794a2b94d910b7::$files;
} else { } else {
$includeFiles = require __DIR__ . '/autoload_files.php'; $includeFiles = require __DIR__ . '/autoload_files.php';
} }
foreach ($includeFiles as $fileIdentifier => $file) { foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire37f8d5a7ba1c21ff17f8f75ac79c08ae($fileIdentifier, $file); composerRequire02e8a54708336a15d0794a2b94d910b7($fileIdentifier, $file);
} }
return $loader; return $loader;
} }
} }
function composerRequire37f8d5a7ba1c21ff17f8f75ac79c08ae($fileIdentifier, $file) function composerRequire02e8a54708336a15d0794a2b94d910b7($fileIdentifier, $file)
{ {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file; require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload; namespace Composer\Autoload;
class ComposerStaticInit37f8d5a7ba1c21ff17f8f75ac79c08ae class ComposerStaticInit02e8a54708336a15d0794a2b94d910b7
{ {
public static $files = array ( public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php', 'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -3839,9 +3839,9 @@ class ComposerStaticInit37f8d5a7ba1c21ff17f8f75ac79c08ae
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)
{ {
return \Closure::bind(function () use ($loader) { return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit37f8d5a7ba1c21ff17f8f75ac79c08ae::$prefixLengthsPsr4; $loader->prefixLengthsPsr4 = ComposerStaticInit02e8a54708336a15d0794a2b94d910b7::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit37f8d5a7ba1c21ff17f8f75ac79c08ae::$prefixDirsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInit02e8a54708336a15d0794a2b94d910b7::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit37f8d5a7ba1c21ff17f8f75ac79c08ae::$classMap; $loader->classMap = ComposerStaticInit02e8a54708336a15d0794a2b94d910b7::$classMap;
}, null, ClassLoader::class); }, 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)) { if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20210719\AutoloadIncluder'); spl_autoload_call('RectorPrefix20210719\AutoloadIncluder');
} }
if (!class_exists('ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae', false) && !interface_exists('ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae', false) && !trait_exists('ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae', false)) { if (!class_exists('ComposerAutoloaderInit02e8a54708336a15d0794a2b94d910b7', false) && !interface_exists('ComposerAutoloaderInit02e8a54708336a15d0794a2b94d910b7', false) && !trait_exists('ComposerAutoloaderInit02e8a54708336a15d0794a2b94d910b7', false)) {
spl_autoload_call('RectorPrefix20210719\ComposerAutoloaderInit37f8d5a7ba1c21ff17f8f75ac79c08ae'); spl_autoload_call('RectorPrefix20210719\ComposerAutoloaderInit02e8a54708336a15d0794a2b94d910b7');
} }
if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) { if (!class_exists('Doctrine\Inflector\Inflector', false) && !interface_exists('Doctrine\Inflector\Inflector', false) && !trait_exists('Doctrine\Inflector\Inflector', false)) {
spl_autoload_call('RectorPrefix20210719\Doctrine\Inflector\Inflector'); spl_autoload_call('RectorPrefix20210719\Doctrine\Inflector\Inflector');
@ -3308,9 +3308,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20210719\print_node(...func_get_args()); return \RectorPrefix20210719\print_node(...func_get_args());
} }
} }
if (!function_exists('composerRequire37f8d5a7ba1c21ff17f8f75ac79c08ae')) { if (!function_exists('composerRequire02e8a54708336a15d0794a2b94d910b7')) {
function composerRequire37f8d5a7ba1c21ff17f8f75ac79c08ae() { function composerRequire02e8a54708336a15d0794a2b94d910b7() {
return \RectorPrefix20210719\composerRequire37f8d5a7ba1c21ff17f8f75ac79c08ae(...func_get_args()); return \RectorPrefix20210719\composerRequire02e8a54708336a15d0794a2b94d910b7(...func_get_args());
} }
} }
if (!function_exists('parseArgs')) { if (!function_exists('parseArgs')) {