mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit ca08d86e3900556b2f389e4d983489535c21fdd7
ca08d86e39
Make use of Class_, Interface_ (#4193)
This commit is contained in:
parent
ff28aaca4c
commit
c3f48d13e3
|
@ -5,8 +5,6 @@ namespace Rector\Transform\Rector\ClassMethod;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Interface_;
|
||||
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
|
@ -49,8 +47,7 @@ final class ReturnTypeWillChangeRector extends AbstractRector implements AllowEm
|
|||
$this->phpAttributeAnalyzer = $phpAttributeAnalyzer;
|
||||
$this->phpAttributeGroupFactory = $phpAttributeGroupFactory;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
$this->returnTypeChangedClassMethodReferences[] = new ClassMethodReference('ArrayAccess', 'getIterator');
|
||||
$this->returnTypeChangedClassMethodReferences[] = new ClassMethodReference('ArrayAccess', 'offsetGet');
|
||||
$this->returnTypeChangedClassMethodReferences = [new ClassMethodReference('ArrayAccess', 'getIterator'), new ClassMethodReference('ArrayAccess', 'offsetGet')];
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -78,42 +75,39 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
return [Class_::class, Interface_::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
* @param Class_|Interface_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($this->phpAttributeAnalyzer->hasPhpAttribute($node, AttributeName::RETURN_TYPE_WILL_CHANGE)) {
|
||||
return null;
|
||||
}
|
||||
// the return type is known, no need to add attribute
|
||||
if ($node->returnType !== null) {
|
||||
return null;
|
||||
}
|
||||
$classLike = $this->betterNodeFinder->findParentByTypes($node, [Class_::class, Interface_::class]);
|
||||
if (!$classLike instanceof ClassLike) {
|
||||
return null;
|
||||
}
|
||||
$classReflection = $this->reflectionResolver->resolveClassAndAnonymousClass($classLike);
|
||||
$methodName = $node->name->toString();
|
||||
$hasChanged = \false;
|
||||
foreach ($this->returnTypeChangedClassMethodReferences as $returnTypeChangedClassMethodReference) {
|
||||
if (!$classReflection->isSubclassOf($returnTypeChangedClassMethodReference->getClass())) {
|
||||
$classReflection = $this->reflectionResolver->resolveClassAndAnonymousClass($node);
|
||||
foreach ($node->getMethods() as $classMethod) {
|
||||
if ($this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, AttributeName::RETURN_TYPE_WILL_CHANGE)) {
|
||||
continue;
|
||||
}
|
||||
if ($returnTypeChangedClassMethodReference->getMethod() !== $methodName) {
|
||||
// the return type is known, no need to add attribute
|
||||
if ($classMethod->returnType !== null) {
|
||||
continue;
|
||||
}
|
||||
$node->attrGroups[] = $this->phpAttributeGroupFactory->createFromClass(AttributeName::RETURN_TYPE_WILL_CHANGE);
|
||||
$hasChanged = \true;
|
||||
break;
|
||||
foreach ($this->returnTypeChangedClassMethodReferences as $returnTypeChangedClassMethodReference) {
|
||||
if (!$classReflection->isSubclassOf($returnTypeChangedClassMethodReference->getClass())) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->isName($classMethod, $returnTypeChangedClassMethodReference->getMethod())) {
|
||||
continue;
|
||||
}
|
||||
$classMethod->attrGroups[] = $this->phpAttributeGroupFactory->createFromClass(AttributeName::RETURN_TYPE_WILL_CHANGE);
|
||||
$hasChanged = \true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$hasChanged) {
|
||||
return null;
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
}
|
||||
return $node;
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param mixed[] $configuration
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '21f8014b30f4a4230f787ca1698a2c53d6d5f18a';
|
||||
public const PACKAGE_VERSION = 'ca08d86e3900556b2f389e4d983489535c21fdd7';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-06-12 10:40:54';
|
||||
public const RELEASE_DATE = '2023-06-12 11:08:14';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit3fab6f3576c517f4a735d8e59230bb55::getLoader();
|
||||
return ComposerAutoloaderInitea19f20ef52a4170b96e772b65acb0c1::getLoader();
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit3fab6f3576c517f4a735d8e59230bb55
|
||||
class ComposerAutoloaderInitea19f20ef52a4170b96e772b65acb0c1
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInit3fab6f3576c517f4a735d8e59230bb55
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit3fab6f3576c517f4a735d8e59230bb55', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitea19f20ef52a4170b96e772b65acb0c1', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit3fab6f3576c517f4a735d8e59230bb55', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitea19f20ef52a4170b96e772b65acb0c1', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit3fab6f3576c517f4a735d8e59230bb55::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitea19f20ef52a4170b96e772b65acb0c1::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit3fab6f3576c517f4a735d8e59230bb55::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitea19f20ef52a4170b96e772b65acb0c1::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit3fab6f3576c517f4a735d8e59230bb55
|
||||
class ComposerStaticInitea19f20ef52a4170b96e772b65acb0c1
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -3097,9 +3097,9 @@ class ComposerStaticInit3fab6f3576c517f4a735d8e59230bb55
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit3fab6f3576c517f4a735d8e59230bb55::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit3fab6f3576c517f4a735d8e59230bb55::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit3fab6f3576c517f4a735d8e59230bb55::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitea19f20ef52a4170b96e772b65acb0c1::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitea19f20ef52a4170b96e772b65acb0c1::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitea19f20ef52a4170b96e772b65acb0c1::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
|
@ -2125,12 +2125,12 @@
|
|||
"source": {
|
||||
"type": "git",
|
||||
"url": "https:\/\/github.com\/rectorphp\/rector-symfony.git",
|
||||
"reference": "9d59fcc509159fdeca278c06df6d56891e71ef70"
|
||||
"reference": "563f5555ca94405a4905cc219bfba04cbd99a8ca"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/9d59fcc509159fdeca278c06df6d56891e71ef70",
|
||||
"reference": "9d59fcc509159fdeca278c06df6d56891e71ef70",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/563f5555ca94405a4905cc219bfba04cbd99a8ca",
|
||||
"reference": "563f5555ca94405a4905cc219bfba04cbd99a8ca",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -2160,7 +2160,7 @@
|
|||
"tomasvotruba\/type-coverage": "^0.2",
|
||||
"tomasvotruba\/unused-public": "^0.1"
|
||||
},
|
||||
"time": "2023-06-12T10:36:57+00:00",
|
||||
"time": "2023-06-12T11:03:45+00:00",
|
||||
"default-branch": true,
|
||||
"type": "rector-extension",
|
||||
"extra": {
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -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 9f8970b'), '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 3a12c7f'), '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 8f78df1'), '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 9d59fcc'));
|
||||
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 9f8970b'), '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 3a12c7f'), '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 8f78df1'), '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 563f555'));
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ use PhpParser\Node\Expr\Variable;
|
|||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
|
@ -73,21 +74,25 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class, MethodCall::class];
|
||||
return [Class_::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod|MethodCall $node
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$this->controllerAnalyzer->isInsideController($node)) {
|
||||
return null;
|
||||
}
|
||||
if ($node instanceof ClassMethod) {
|
||||
return $this->refactorClassMethod($node);
|
||||
$hasChanged = \false;
|
||||
foreach ($node->getMethods() as $classMethod) {
|
||||
$changedClassMethod = $this->refactorClassMethod($classMethod);
|
||||
if ($changedClassMethod instanceof ClassMethod) {
|
||||
$hasChanged = \true;
|
||||
}
|
||||
}
|
||||
if ($this->isGetRequestInAction($node)) {
|
||||
return new Variable($this->getRequestVariableAndParamName());
|
||||
if ($hasChanged) {
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -130,7 +135,7 @@ CODE_SAMPLE
|
|||
}
|
||||
return \false;
|
||||
}
|
||||
private function isGetRequestInAction(MethodCall $methodCall) : bool
|
||||
private function isGetRequestInAction(ClassMethod $classMethod, MethodCall $methodCall) : bool
|
||||
{
|
||||
// must be $this->getRequest() in controller
|
||||
if (!$methodCall->var instanceof Variable) {
|
||||
|
@ -142,10 +147,6 @@ CODE_SAMPLE
|
|||
if (!$this->isName($methodCall->name, 'getRequest') && !$this->isGetMethodCallWithRequestParameters($methodCall)) {
|
||||
return \false;
|
||||
}
|
||||
$classMethod = $this->betterNodeFinder->findParentType($methodCall, ClassMethod::class);
|
||||
if (!$classMethod instanceof ClassMethod) {
|
||||
return \false;
|
||||
}
|
||||
return $this->controllerMethodAnalyzer->isAction($classMethod);
|
||||
}
|
||||
private function containsGetRequestMethod(ClassMethod $classMethod) : bool
|
||||
|
@ -193,6 +194,15 @@ CODE_SAMPLE
|
|||
}
|
||||
$fullyQualified = new FullyQualified(self::REQUEST_CLASS);
|
||||
$classMethod->params[] = new Param(new Variable($this->getRequestVariableAndParamName()), null, $fullyQualified);
|
||||
$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use($classMethod) {
|
||||
if (!$node instanceof MethodCall) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isGetRequestInAction($classMethod, $node)) {
|
||||
return new Variable($this->getRequestVariableAndParamName());
|
||||
}
|
||||
return null;
|
||||
});
|
||||
return $classMethod;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue