mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
Updated Rector to commit fe88fbb630
fe88fbb630
[PHP 8.1] Add FirstClassCallableRector (#2544)
This commit is contained in:
parent
bb396cb5b1
commit
f638f7a12a
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
|||
namespace RectorPrefix202206;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
|
||||
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
|
||||
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;
|
||||
use Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector;
|
||||
|
@ -25,4 +26,5 @@ return static function (RectorConfig $rectorConfig) : void {
|
|||
$rectorConfig->rule(NewInInitializerRector::class);
|
||||
$rectorConfig->rule(IntersectionTypesRector::class);
|
||||
$rectorConfig->rule(NullToStrictStringFuncCallArgRector::class);
|
||||
$rectorConfig->rule(FirstClassCallableRector::class);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# 517 Rules Overview
|
||||
# 518 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -74,7 +74,7 @@
|
|||
|
||||
- [Php80](#php80) (17)
|
||||
|
||||
- [Php81](#php81) (10)
|
||||
- [Php81](#php81) (11)
|
||||
|
||||
- [Php82](#php82) (1)
|
||||
|
||||
|
@ -8567,6 +8567,29 @@ Add final to constants that does not have children
|
|||
|
||||
<br>
|
||||
|
||||
### FirstClassCallableRector
|
||||
|
||||
Upgrade array callable to first class callable
|
||||
|
||||
- class: [`Rector\Php81\Rector\Array_\FirstClassCallableRector`](../rules/Php81/Rector/Array_/FirstClassCallableRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- $name = [$this, 'name'];
|
||||
+ $name = $this->name(...);
|
||||
}
|
||||
|
||||
public function name()
|
||||
{
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### IntersectionTypesRector
|
||||
|
||||
Change docs to intersection types, where possible (properties are covered by TypedPropertyRector (@todo))
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Rector\NodeCollector\NodeAnalyzer;
|
|||
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Attribute;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\ArrayItem;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
|
@ -17,6 +18,8 @@ use PHPStan\Reflection\ParametersAcceptorSelector;
|
|||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\ThisType;
|
||||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\TypeWithClassName;
|
||||
use Rector\Core\Enum\ObjectReference;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
|
@ -81,8 +84,8 @@ final class ArrayCallableMethodMatcher
|
|||
$items = $array->items;
|
||||
// $this, self, static, FQN
|
||||
$firstItemValue = $items[0]->value;
|
||||
$calleeType = $firstItemValue instanceof ClassConstFetch ? $this->resolveClassConstFetchType($firstItemValue) : $this->nodeTypeResolver->getType($firstItemValue);
|
||||
if (!$calleeType instanceof TypeWithClassName) {
|
||||
$callerType = $this->resolveCallerType($firstItemValue);
|
||||
if (!$callerType instanceof TypeWithClassName) {
|
||||
return null;
|
||||
}
|
||||
$isInAttribute = (bool) $this->betterNodeFinder->findParentType($array, Attribute::class);
|
||||
|
@ -90,7 +93,7 @@ final class ArrayCallableMethodMatcher
|
|||
return null;
|
||||
}
|
||||
$values = $this->valueResolver->getValue($array);
|
||||
$className = $calleeType->getClassName();
|
||||
$className = $callerType->getClassName();
|
||||
$secondItemValue = $items[1]->value;
|
||||
if ($values === null) {
|
||||
return new ArrayCallableDynamicMethod($firstItemValue, $className, $secondItemValue);
|
||||
|
@ -108,6 +111,10 @@ final class ArrayCallableMethodMatcher
|
|||
if ($methodName === MethodName::CONSTRUCT) {
|
||||
return null;
|
||||
}
|
||||
// skip non-existing methods
|
||||
if (!$callerType->hasMethod($methodName)->yes()) {
|
||||
return null;
|
||||
}
|
||||
return new ArrayCallable($firstItemValue, $className, $methodName);
|
||||
}
|
||||
private function shouldSkipNullItems(Array_ $array) : bool
|
||||
|
@ -181,4 +188,17 @@ final class ArrayCallableMethodMatcher
|
|||
}
|
||||
return new ObjectType($classConstantReference, null, $classReflection);
|
||||
}
|
||||
private function resolveCallerType(Expr $expr) : Type
|
||||
{
|
||||
if ($expr instanceof ClassConstFetch) {
|
||||
// static ::class reference?
|
||||
$callerType = $this->resolveClassConstFetchType($expr);
|
||||
} else {
|
||||
$callerType = $this->nodeTypeResolver->getType($expr);
|
||||
}
|
||||
if ($callerType instanceof ThisType) {
|
||||
return $callerType->getStaticObjectType();
|
||||
}
|
||||
return $callerType;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,8 +15,6 @@ use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\DowngradeUnionTypeDeclarationRectorTest
|
||||
*
|
||||
* @requires PHP 8.0
|
||||
*/
|
||||
final class DowngradeUnionTypeDeclarationRector extends AbstractRector
|
||||
{
|
||||
|
|
97
rules/Php81/Rector/Array_/FirstClassCallableRector.php
Normal file
97
rules/Php81/Rector/Array_/FirstClassCallableRector.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php81\Rector\Array_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\VariadicPlaceholder;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
|
||||
use Rector\NodeCollector\ValueObject\ArrayCallable;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @changelog https://php.watch/versions/8.1/first-class-callable-syntax
|
||||
*
|
||||
* @see \Rector\Tests\Php81\Rector\Array_\FirstClassCallableRector\FirstClassCallableRectorTest
|
||||
*/
|
||||
final class FirstClassCallableRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher
|
||||
*/
|
||||
private $arrayCallableMethodMatcher;
|
||||
public function __construct(ArrayCallableMethodMatcher $arrayCallableMethodMatcher)
|
||||
{
|
||||
$this->arrayCallableMethodMatcher = $arrayCallableMethodMatcher;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Upgrade array callable to first class callable', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$name = [$this, 'name'];
|
||||
}
|
||||
|
||||
public function name()
|
||||
{
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$name = $this->name(...);
|
||||
}
|
||||
|
||||
public function name()
|
||||
{
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Array_::class];
|
||||
}
|
||||
/**
|
||||
* @param Array_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$arrayCallable = $this->arrayCallableMethodMatcher->match($node);
|
||||
if (!$arrayCallable instanceof ArrayCallable) {
|
||||
return null;
|
||||
}
|
||||
$callerExpr = $arrayCallable->getCallerExpr();
|
||||
if (!$callerExpr instanceof Variable && !$callerExpr instanceof PropertyFetch && !$callerExpr instanceof ClassConstFetch) {
|
||||
return null;
|
||||
}
|
||||
$args = [new VariadicPlaceholder()];
|
||||
if ($callerExpr instanceof ClassConstFetch) {
|
||||
return new StaticCall($callerExpr->class, $arrayCallable->getMethod(), $args);
|
||||
}
|
||||
return new MethodCall($callerExpr, $arrayCallable->getMethod(), $args);
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersion::PHP_81;
|
||||
}
|
||||
}
|
|
@ -16,11 +16,11 @@ final class VersionResolver
|
|||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '6c713a776c54053537fcca97630d2da154d95514';
|
||||
public const PACKAGE_VERSION = 'fe88fbb6306cec3728b9158fd13b1cfd23b9f0f6';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-06-21 08:29:08';
|
||||
public const RELEASE_DATE = '2022-06-21 07:33:30';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055::getLoader();
|
||||
return ComposerAutoloaderInitebf48716d1452b3c30b2ef68cd893b66::getLoader();
|
||||
|
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
|
@ -2561,6 +2561,7 @@ return array(
|
|||
'Rector\\Php81\\NodeAnalyzer\\EnumConstListClassDetector' => $baseDir . '/rules/Php81/NodeAnalyzer/EnumConstListClassDetector.php',
|
||||
'Rector\\Php81\\NodeFactory\\ClassFromEnumFactory' => $baseDir . '/rules/Php81/NodeFactory/ClassFromEnumFactory.php',
|
||||
'Rector\\Php81\\NodeFactory\\EnumFactory' => $baseDir . '/rules/Php81/NodeFactory/EnumFactory.php',
|
||||
'Rector\\Php81\\Rector\\Array_\\FirstClassCallableRector' => $baseDir . '/rules/Php81/Rector/Array_/FirstClassCallableRector.php',
|
||||
'Rector\\Php81\\Rector\\ClassConst\\FinalizePublicClassConstantRector' => $baseDir . '/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php',
|
||||
'Rector\\Php81\\Rector\\ClassMethod\\NewInInitializerRector' => $baseDir . '/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php',
|
||||
'Rector\\Php81\\Rector\\Class_\\ConstantListClassToEnumRector' => $baseDir . '/rules/Php81/Rector/Class_/ConstantListClassToEnumRector.php',
|
||||
|
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055
|
||||
class ComposerAutoloaderInitebf48716d1452b3c30b2ef68cd893b66
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,19 +22,19 @@ class ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInitebf48716d1452b3c30b2ef68cd893b66', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitebf48716d1452b3c30b2ef68cd893b66', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit73d382890f12bde41f2d8fcab5746055::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitebf48716d1452b3c30b2ef68cd893b66::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit73d382890f12bde41f2d8fcab5746055::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInitebf48716d1452b3c30b2ef68cd893b66::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire73d382890f12bde41f2d8fcab5746055($fileIdentifier, $file);
|
||||
composerRequireebf48716d1452b3c30b2ef68cd893b66($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
|
@ -46,7 +46,7 @@ class ComposerAutoloaderInit73d382890f12bde41f2d8fcab5746055
|
|||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequire73d382890f12bde41f2d8fcab5746055($fileIdentifier, $file)
|
||||
function composerRequireebf48716d1452b3c30b2ef68cd893b66($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
9
vendor/composer/autoload_static.php
vendored
9
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit73d382890f12bde41f2d8fcab5746055
|
||||
class ComposerStaticInitebf48716d1452b3c30b2ef68cd893b66
|
||||
{
|
||||
public static $files = array (
|
||||
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
|
||||
|
@ -2862,6 +2862,7 @@ class ComposerStaticInit73d382890f12bde41f2d8fcab5746055
|
|||
'Rector\\Php81\\NodeAnalyzer\\EnumConstListClassDetector' => __DIR__ . '/../..' . '/rules/Php81/NodeAnalyzer/EnumConstListClassDetector.php',
|
||||
'Rector\\Php81\\NodeFactory\\ClassFromEnumFactory' => __DIR__ . '/../..' . '/rules/Php81/NodeFactory/ClassFromEnumFactory.php',
|
||||
'Rector\\Php81\\NodeFactory\\EnumFactory' => __DIR__ . '/../..' . '/rules/Php81/NodeFactory/EnumFactory.php',
|
||||
'Rector\\Php81\\Rector\\Array_\\FirstClassCallableRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/Array_/FirstClassCallableRector.php',
|
||||
'Rector\\Php81\\Rector\\ClassConst\\FinalizePublicClassConstantRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php',
|
||||
'Rector\\Php81\\Rector\\ClassMethod\\NewInInitializerRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/ClassMethod/NewInInitializerRector.php',
|
||||
'Rector\\Php81\\Rector\\Class_\\ConstantListClassToEnumRector' => __DIR__ . '/../..' . '/rules/Php81/Rector/Class_/ConstantListClassToEnumRector.php',
|
||||
|
@ -3400,9 +3401,9 @@ class ComposerStaticInit73d382890f12bde41f2d8fcab5746055
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit73d382890f12bde41f2d8fcab5746055::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit73d382890f12bde41f2d8fcab5746055::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit73d382890f12bde41f2d8fcab5746055::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitebf48716d1452b3c30b2ef68cd893b66::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitebf48716d1452b3c30b2ef68cd893b66::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitebf48716d1452b3c30b2ef68cd893b66::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user