Updated Rector to commit 4a5616878c580138a26e1eedf63c15fa8e0dcda8

4a5616878c Make ReturnTypeFromStrictNewArrayRector handle only single level arrays (#4575)
This commit is contained in:
Tomas Votruba 2023-07-21 21:08:31 +00:00
parent ddde48c977
commit 2d8f90877a
8 changed files with 42 additions and 59 deletions

View File

@ -18,15 +18,12 @@ use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -41,20 +38,14 @@ final class ReturnTypeFromStrictNewArrayRector extends AbstractScopeAwareRector
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
*/
private $phpDocTypeChanger;
/**
* @readonly
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
*/
private $typeComparator;
/**
* @readonly
* @var \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard
*/
private $classMethodReturnTypeOverrideGuard;
public function __construct(PhpDocTypeChanger $phpDocTypeChanger, TypeComparator $typeComparator, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard)
public function __construct(PhpDocTypeChanger $phpDocTypeChanger, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard)
{
$this->phpDocTypeChanger = $phpDocTypeChanger;
$this->typeComparator = $typeComparator;
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
}
public function getRuleDefinition() : RuleDefinition
@ -124,7 +115,7 @@ CODE_SAMPLE
return null;
}
$returnType = $this->nodeTypeResolver->getType($onlyReturn->expr);
if (!$returnType->isArray()->yes()) {
if (!$returnType instanceof ArrayType) {
return null;
}
if (!$this->nodeNameResolver->areNamesEqual($onlyReturn->expr, $variable)) {
@ -132,10 +123,9 @@ CODE_SAMPLE
}
// 3. always returns array
$node->returnType = new Identifier('array');
// 4. add more precise type if suitable
$exprType = $this->getType($onlyReturn->expr);
if ($this->shouldAddReturnArrayDocType($exprType)) {
$this->changeReturnType($node, $exprType);
// 4. add more precise array type if suitable
if ($this->shouldAddReturnArrayDocType($returnType)) {
$this->changeReturnType($node, $returnType);
}
return $node;
}
@ -156,13 +146,18 @@ CODE_SAMPLE
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $node
*/
private function changeReturnType($node, Type $exprType) : void
private function changeReturnType($node, ArrayType $arrayType) : void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$exprType = $this->narrowConstantArrayType($exprType);
if (!$this->typeComparator->isSubtype($phpDocInfo->getReturnType(), $exprType)) {
$this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $exprType);
// skip already filled type, on purpose
if (!$phpDocInfo->getReturnType() instanceof MixedType) {
return;
}
// can handle only exactly 1-type array
if ($arrayType instanceof ConstantArrayType && \count($arrayType->getValueTypes()) !== 1) {
return;
}
$this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $arrayType);
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
@ -208,29 +203,17 @@ CODE_SAMPLE
}
return null;
}
private function shouldAddReturnArrayDocType(Type $exprType) : bool
private function shouldAddReturnArrayDocType(ArrayType $arrayType) : bool
{
if ($exprType instanceof ConstantArrayType) {
// sign of empty array, keep empty
return !$exprType->getItemType() instanceof NeverType;
}
return $exprType->isArray()->yes();
}
private function narrowConstantArrayType(Type $type) : Type
{
if (!$type instanceof ConstantArrayType) {
return $type;
}
if (\count($type->getValueTypes()) === 1) {
$singleValueType = $type->getValueTypes()[0];
if ($singleValueType instanceof ObjectType) {
return $type;
if ($arrayType instanceof ConstantArrayType) {
if ($arrayType->getItemType() instanceof NeverType) {
return \false;
}
// handle only simple arrays
if (!$arrayType->getKeyType() instanceof IntegerType) {
return \false;
}
}
$printedDescription = $type->describe(VerbosityLevel::precise());
if (\strlen($printedDescription) > 50) {
return new ArrayType(new MixedType(), new MixedType());
}
return $type;
return \true;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '3ff83e52db13fd98dcde85b1d2d53f7db80b875b';
public const PACKAGE_VERSION = '4a5616878c580138a26e1eedf63c15fa8e0dcda8';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-21 15:20:13';
public const RELEASE_DATE = '2023-07-21 22:04:23';
/**
* @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 ComposerAutoloaderInit978a84f91f7d9c390e02e0746cd635ac::getLoader();
return ComposerAutoloaderInit0bd702157337d6816a8a4f640272d70d::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit978a84f91f7d9c390e02e0746cd635ac
class ComposerAutoloaderInit0bd702157337d6816a8a4f640272d70d
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit978a84f91f7d9c390e02e0746cd635ac
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit978a84f91f7d9c390e02e0746cd635ac', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit0bd702157337d6816a8a4f640272d70d', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit978a84f91f7d9c390e02e0746cd635ac', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit0bd702157337d6816a8a4f640272d70d', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit978a84f91f7d9c390e02e0746cd635ac::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit0bd702157337d6816a8a4f640272d70d::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit978a84f91f7d9c390e02e0746cd635ac::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit0bd702157337d6816a8a4f640272d70d::$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 ComposerStaticInit978a84f91f7d9c390e02e0746cd635ac
class ComposerStaticInit0bd702157337d6816a8a4f640272d70d
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3021,9 +3021,9 @@ class ComposerStaticInit978a84f91f7d9c390e02e0746cd635ac
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit978a84f91f7d9c390e02e0746cd635ac::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit978a84f91f7d9c390e02e0746cd635ac::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit978a84f91f7d9c390e02e0746cd635ac::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit0bd702157337d6816a8a4f640272d70d::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit0bd702157337d6816a8a4f640272d70d::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit0bd702157337d6816a8a4f640272d70d::$classMap;
}, null, ClassLoader::class);
}

View File

@ -1985,12 +1985,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-downgrade-php.git",
"reference": "26e8bb4921fa8685dee87f5bdb8be481961236f3"
"reference": "6794ce3d49e91cf888b17f3308318c73c1fdf425"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-downgrade-php\/zipball\/26e8bb4921fa8685dee87f5bdb8be481961236f3",
"reference": "26e8bb4921fa8685dee87f5bdb8be481961236f3",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-downgrade-php\/zipball\/6794ce3d49e91cf888b17f3308318c73c1fdf425",
"reference": "6794ce3d49e91cf888b17f3308318c73c1fdf425",
"shasum": ""
},
"require": {
@ -2015,7 +2015,7 @@
"tomasvotruba\/type-coverage": "^0.2",
"tomasvotruba\/unused-public": "^0.1"
},
"time": "2023-07-21T10:25:40+00:00",
"time": "2023-07-21T14:22:55+00:00",
"default-branch": true,
"type": "rector-extension",
"extra": {

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,7 @@ namespace Rector\RectorInstaller;
*/
final class GeneratedConfig
{
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 2d14418'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 26e8bb4'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 87b593f'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main a0af12a'));
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 2d14418'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 6794ce3'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 87b593f'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main a0af12a'));
private function __construct()
{
}