Updated Rector to commit 49f1596404278c63a37841eb720aabf97601311e

2eb244c116 Add failing test fixture for AddArrayReturnDocTypeRector 9102b49763 add dump_with_depth helper function 679be3801a add strict return type based return doc 8fc175912f check for iterable type before adding return type d8e153153f remove non-array test fixtures ff5f80c568 keep nullable array type b45ff7a562 remove unused test 49f1596404 Merge pull request #1639 from rectorphp/tv-make-array-return-doc-work-only-with-arrays
This commit is contained in:
Tomas Votruba 2022-01-05 11:55:03 +00:00
parent 88a06d884f
commit 8e36a87120
9 changed files with 108 additions and 20 deletions

View File

@ -24,6 +24,7 @@ use Rector\Privatization\TypeManipulator\NormalizeTypeToRespectArrayScalarType;
use Rector\Privatization\TypeManipulator\TypeNormalizer;
use Rector\TypeDeclaration\NodeTypeAnalyzer\DetailedTypeAnalyzer;
use Rector\TypeDeclaration\TypeAnalyzer\AdvancedArrayAnalyzer;
use Rector\TypeDeclaration\TypeAnalyzer\IterableTypeAnalyzer;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer\ReturnTypeDeclarationReturnTypeInfererTypeInferer;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
@ -74,7 +75,12 @@ final class AddArrayReturnDocTypeRector extends \Rector\Core\Rector\AbstractRect
* @var \Rector\Privatization\TypeManipulator\TypeNormalizer
*/
private $typeNormalizer;
public function __construct(\Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer $returnTypeInferer, \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, \Rector\TypeDeclaration\TypeAnalyzer\AdvancedArrayAnalyzer $advancedArrayAnalyzer, \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger, \Rector\Privatization\TypeManipulator\NormalizeTypeToRespectArrayScalarType $normalizeTypeToRespectArrayScalarType, \Rector\DeadCode\PhpDoc\TagRemover\ReturnTagRemover $returnTagRemover, \Rector\TypeDeclaration\NodeTypeAnalyzer\DetailedTypeAnalyzer $detailedTypeAnalyzer, \Rector\Privatization\TypeManipulator\TypeNormalizer $typeNormalizer)
/**
* @readonly
* @var \Rector\TypeDeclaration\TypeAnalyzer\IterableTypeAnalyzer
*/
private $iterableTypeAnalyzer;
public function __construct(\Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer $returnTypeInferer, \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, \Rector\TypeDeclaration\TypeAnalyzer\AdvancedArrayAnalyzer $advancedArrayAnalyzer, \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger $phpDocTypeChanger, \Rector\Privatization\TypeManipulator\NormalizeTypeToRespectArrayScalarType $normalizeTypeToRespectArrayScalarType, \Rector\DeadCode\PhpDoc\TagRemover\ReturnTagRemover $returnTagRemover, \Rector\TypeDeclaration\NodeTypeAnalyzer\DetailedTypeAnalyzer $detailedTypeAnalyzer, \Rector\Privatization\TypeManipulator\TypeNormalizer $typeNormalizer, \Rector\TypeDeclaration\TypeAnalyzer\IterableTypeAnalyzer $iterableTypeAnalyzer)
{
$this->returnTypeInferer = $returnTypeInferer;
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
@ -84,6 +90,7 @@ final class AddArrayReturnDocTypeRector extends \Rector\Core\Rector\AbstractRect
$this->returnTagRemover = $returnTagRemover;
$this->detailedTypeAnalyzer = $detailedTypeAnalyzer;
$this->typeNormalizer = $typeNormalizer;
$this->iterableTypeAnalyzer = $iterableTypeAnalyzer;
}
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
@ -177,6 +184,9 @@ CODE_SAMPLE
}
private function shouldSkipType(\PHPStan\Type\Type $newType, \PHPStan\Type\Type $currentType, \PhpParser\Node\Stmt\ClassMethod $classMethod, \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo $phpDocInfo) : bool
{
if (!$this->iterableTypeAnalyzer->isIterableType($newType)) {
return \true;
}
if ($newType instanceof \PHPStan\Type\ArrayType && $this->shouldSkipArrayType($newType, $classMethod, $phpDocInfo)) {
return \true;
}

View File

@ -0,0 +1,62 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\TypeAnalyzer;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IterableType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
final class IterableTypeAnalyzer
{
/**
* @readonly
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
public function __construct(\PHPStan\Reflection\ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}
public function isIterableType(\PHPStan\Type\Type $type) : bool
{
if ($this->isUnionOfIterableTypes($type)) {
return \true;
}
if ($type instanceof \PHPStan\Type\ArrayType) {
return \true;
}
if ($type instanceof \PHPStan\Type\IterableType) {
return \true;
}
if ($type instanceof \PHPStan\Type\Generic\GenericObjectType) {
if (!$this->reflectionProvider->hasClass($type->getClassName())) {
return \false;
}
$genericObjectTypeClassReflection = $this->reflectionProvider->getClass($type->getClassName());
if ($genericObjectTypeClassReflection->implementsInterface('Traversable')) {
return \true;
}
}
return \false;
}
private function isUnionOfIterableTypes(\PHPStan\Type\Type $type) : bool
{
if (!$type instanceof \PHPStan\Type\UnionType) {
return \false;
}
foreach ($type->getTypes() as $unionedType) {
// nullable union is allowed
if ($unionedType instanceof \PHPStan\Type\NullType) {
continue;
}
if (!$this->isIterableType($unionedType)) {
return \false;
}
}
return \true;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '840bd5e3e6af1fd5474ec8717c75c6b5400e1737';
public const PACKAGE_VERSION = '49f1596404278c63a37841eb720aabf97601311e';
/**
* @var string
*/
public const RELEASE_DATE = '2022-01-05 12:23:16';
public const RELEASE_DATE = '2022-01-05 12:45:14';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20220105\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

View File

@ -6,6 +6,15 @@ namespace RectorPrefix20220105;
use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard;
use RectorPrefix20220105\Tracy\Dumper;
if (!\function_exists('dump_with_depth')) {
/**
* @param mixed $value
*/
function dump_with_depth($value, int $depth = 2) : void
{
\RectorPrefix20220105\Tracy\Dumper::dump($value, [\RectorPrefix20220105\Tracy\Dumper::DEPTH => $depth]);
}
}
if (!\function_exists('dn')) {
function dn(\PhpParser\Node $node, int $depth = 2) : void
{

2
vendor/autoload.php vendored
View File

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

View File

@ -3105,6 +3105,7 @@ return array(
'Rector\\TypeDeclaration\\TypeAlreadyAddedChecker\\ReturnTypeAlreadyAddedChecker' => $baseDir . '/rules/TypeDeclaration/TypeAlreadyAddedChecker/ReturnTypeAlreadyAddedChecker.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\AdvancedArrayAnalyzer' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/AdvancedArrayAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\GenericClassStringTypeNormalizer' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/GenericClassStringTypeNormalizer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\IterableTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/IterableTypeAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\ObjectTypeComparator' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/ObjectTypeComparator.php',
'Rector\\TypeDeclaration\\TypeInferer\\AssignToPropertyTypeInferer' => $baseDir . '/rules/TypeDeclaration/TypeInferer/AssignToPropertyTypeInferer.php',
'Rector\\TypeDeclaration\\TypeInferer\\ParamTypeInferer' => $baseDir . '/rules/TypeDeclaration/TypeInferer/ParamTypeInferer.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f
class ComposerAutoloaderInitd2c47e9b1c523de03caede4ae2ea2722
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitd2c47e9b1c523de03caede4ae2ea2722', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitd2c47e9b1c523de03caede4ae2ea2722', '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\ComposerStaticInit4786b9bf3ccba2bd289627c76c84f87f::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitd2c47e9b1c523de03caede4ae2ea2722::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,12 +42,12 @@ class ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit4786b9bf3ccba2bd289627c76c84f87f::$files;
$includeFiles = Composer\Autoload\ComposerStaticInitd2c47e9b1c523de03caede4ae2ea2722::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire4786b9bf3ccba2bd289627c76c84f87f($fileIdentifier, $file);
composerRequired2c47e9b1c523de03caede4ae2ea2722($fileIdentifier, $file);
}
return $loader;
@ -59,7 +59,7 @@ class ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f
* @param string $file
* @return void
*/
function composerRequire4786b9bf3ccba2bd289627c76c84f87f($fileIdentifier, $file)
function composerRequired2c47e9b1c523de03caede4ae2ea2722($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 ComposerStaticInit4786b9bf3ccba2bd289627c76c84f87f
class ComposerStaticInitd2c47e9b1c523de03caede4ae2ea2722
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -3500,6 +3500,7 @@ class ComposerStaticInit4786b9bf3ccba2bd289627c76c84f87f
'Rector\\TypeDeclaration\\TypeAlreadyAddedChecker\\ReturnTypeAlreadyAddedChecker' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAlreadyAddedChecker/ReturnTypeAlreadyAddedChecker.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\AdvancedArrayAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/AdvancedArrayAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\GenericClassStringTypeNormalizer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/GenericClassStringTypeNormalizer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\IterableTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/IterableTypeAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\ObjectTypeComparator' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/ObjectTypeComparator.php',
'Rector\\TypeDeclaration\\TypeInferer\\AssignToPropertyTypeInferer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeInferer/AssignToPropertyTypeInferer.php',
'Rector\\TypeDeclaration\\TypeInferer\\ParamTypeInferer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeInferer/ParamTypeInferer.php',
@ -3850,9 +3851,9 @@ class ComposerStaticInit4786b9bf3ccba2bd289627c76c84f87f
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit4786b9bf3ccba2bd289627c76c84f87f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit4786b9bf3ccba2bd289627c76c84f87f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit4786b9bf3ccba2bd289627c76c84f87f::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitd2c47e9b1c523de03caede4ae2ea2722::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitd2c47e9b1c523de03caede4ae2ea2722::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitd2c47e9b1c523de03caede4ae2ea2722::$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('RectorPrefix20220105\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f', false) && !interface_exists('ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f', false) && !trait_exists('ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f', false)) {
spl_autoload_call('RectorPrefix20220105\ComposerAutoloaderInit4786b9bf3ccba2bd289627c76c84f87f');
if (!class_exists('ComposerAutoloaderInitd2c47e9b1c523de03caede4ae2ea2722', false) && !interface_exists('ComposerAutoloaderInitd2c47e9b1c523de03caede4ae2ea2722', false) && !trait_exists('ComposerAutoloaderInitd2c47e9b1c523de03caede4ae2ea2722', false)) {
spl_autoload_call('RectorPrefix20220105\ComposerAutoloaderInitd2c47e9b1c523de03caede4ae2ea2722');
}
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('RectorPrefix20220105\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -51,6 +51,11 @@ if (!class_exists('Symplify\SmartFileSystem\SmartFileInfo', false) && !interface
// Functions whitelisting. For more information see:
// https://github.com/humbug/php-scoper/blob/master/README.md#functions-whitelisting
if (!function_exists('dump_with_depth')) {
function dump_with_depth() {
return \RectorPrefix20220105\dump_with_depth(...func_get_args());
}
}
if (!function_exists('dn')) {
function dn() {
return \RectorPrefix20220105\dn(...func_get_args());
@ -66,9 +71,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20220105\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire4786b9bf3ccba2bd289627c76c84f87f')) {
function composerRequire4786b9bf3ccba2bd289627c76c84f87f() {
return \RectorPrefix20220105\composerRequire4786b9bf3ccba2bd289627c76c84f87f(...func_get_args());
if (!function_exists('composerRequired2c47e9b1c523de03caede4ae2ea2722')) {
function composerRequired2c47e9b1c523de03caede4ae2ea2722() {
return \RectorPrefix20220105\composerRequired2c47e9b1c523de03caede4ae2ea2722(...func_get_args());
}
}
if (!function_exists('scanPath')) {