mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-20 07:22:43 +02:00
Updated Rector to commit bb3d37ab214177a4e2877a5e0b383d055c62b6ad
bb3d37ab21
Remove ChangeReflectionTypeToStringToGetNameRector as niche and overly detailed, better refactor reflectio na as a whole (#3976)
This commit is contained in:
parent
1539fc211a
commit
ce521dbd34
@ -14,7 +14,6 @@ use Rector\Php74\Rector\FuncCall\FilterVarToAddSlashesRector;
|
||||
use Rector\Php74\Rector\FuncCall\MbStrrposEncodingArgumentPositionRector;
|
||||
use Rector\Php74\Rector\FuncCall\MoneyFormatToNumberFormatRector;
|
||||
use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector;
|
||||
use Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector;
|
||||
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
|
||||
use Rector\Php74\Rector\StaticCall\ExportToReflectionFunctionRector;
|
||||
use Rector\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector;
|
||||
@ -29,5 +28,5 @@ return static function (RectorConfig $rectorConfig) : void {
|
||||
# https://wiki.php.net/rfc/deprecations_php_7_4
|
||||
'apache_request_headers' => 'getallheaders',
|
||||
]);
|
||||
$rectorConfig->rules([ArrayKeyExistsOnPropertyRector::class, FilterVarToAddSlashesRector::class, ExportToReflectionFunctionRector::class, MbStrrposEncodingArgumentPositionRector::class, RealToFloatTypeCastRector::class, NullCoalescingOperatorRector::class, ClosureToArrowFunctionRector::class, ArraySpreadInsteadOfArrayMergeRector::class, AddLiteralSeparatorToNumberRector::class, ChangeReflectionTypeToStringToGetNameRector::class, RestoreDefaultNullToNullableTypePropertyRector::class, CurlyToSquareBracketArrayStringRector::class, MoneyFormatToNumberFormatRector::class, ParenthesizeNestedTernaryRector::class, TypedPropertyFromAssignsRector::class]);
|
||||
$rectorConfig->rules([ArrayKeyExistsOnPropertyRector::class, FilterVarToAddSlashesRector::class, ExportToReflectionFunctionRector::class, MbStrrposEncodingArgumentPositionRector::class, RealToFloatTypeCastRector::class, NullCoalescingOperatorRector::class, ClosureToArrowFunctionRector::class, ArraySpreadInsteadOfArrayMergeRector::class, AddLiteralSeparatorToNumberRector::class, RestoreDefaultNullToNullableTypePropertyRector::class, CurlyToSquareBracketArrayStringRector::class, MoneyFormatToNumberFormatRector::class, ParenthesizeNestedTernaryRector::class, TypedPropertyFromAssignsRector::class]);
|
||||
};
|
||||
|
@ -42,7 +42,7 @@
|
||||
|
||||
- [Php73](#php73) (9)
|
||||
|
||||
- [Php74](#php74) (14)
|
||||
- [Php74](#php74) (13)
|
||||
|
||||
- [Php80](#php80) (20)
|
||||
|
||||
@ -5345,33 +5345,6 @@ Change `array_merge()` to spread operator
|
||||
|
||||
<br>
|
||||
|
||||
### ChangeReflectionTypeToStringToGetNameRector
|
||||
|
||||
Change string calls on ReflectionType
|
||||
|
||||
- class: [`Rector\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector`](../rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function go(ReflectionFunction $reflectionFunction)
|
||||
{
|
||||
$parameterReflection = $reflectionFunction->getParameters()[0];
|
||||
|
||||
- $paramType = (string) $parameterReflection->getType();
|
||||
+ $paramType = (string) ($parameterReflection->getType() ? $parameterReflection->getType()->getName() : null);
|
||||
|
||||
- $stringValue = 'hey' . $reflectionFunction->getReturnType();
|
||||
+ $stringValue = 'hey' . ($reflectionFunction->getReturnType() ? $reflectionFunction->getReturnType()->getName() : null);
|
||||
|
||||
// keep
|
||||
return $reflectionFunction->getReturnType();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### ClosureToArrowFunctionRector
|
||||
|
||||
Change closure to arrow function
|
||||
|
@ -1,209 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Php74\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Expr\Cast\String_;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Rector\Core\Rector\AbstractScopeAwareRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @changelog https://www.reddit.com/r/PHP/comments/apikof/whats_the_deal_with_reflectiontype/ https://www.php.net/manual/en/reflectiontype.tostring.php
|
||||
*
|
||||
* @changelog https://3v4l.org/fYeif
|
||||
* @changelog https://3v4l.org/QeM6U
|
||||
*
|
||||
* @see \Rector\Tests\Php74\Rector\MethodCall\ChangeReflectionTypeToStringToGetNameRector\ChangeReflectionTypeToStringToGetNameRectorTest
|
||||
*/
|
||||
final class ChangeReflectionTypeToStringToGetNameRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const GET_NAME = 'getName';
|
||||
/**
|
||||
* Possibly extract node decorator with scope breakers (Function_, If_) to respect node flow
|
||||
* @var string[][]
|
||||
*/
|
||||
private $callsByVariable = [];
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::REFLECTION_TYPE_GETNAME;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Change string calls on ReflectionType', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function go(ReflectionFunction $reflectionFunction)
|
||||
{
|
||||
$parameterReflection = $reflectionFunction->getParameters()[0];
|
||||
|
||||
$paramType = (string) $parameterReflection->getType();
|
||||
|
||||
$stringValue = 'hey' . $reflectionFunction->getReturnType();
|
||||
|
||||
// keep
|
||||
return $reflectionFunction->getReturnType();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function go(ReflectionFunction $reflectionFunction)
|
||||
{
|
||||
$parameterReflection = $reflectionFunction->getParameters()[0];
|
||||
|
||||
$paramType = (string) ($parameterReflection->getType() ? $parameterReflection->getType()->getName() : null);
|
||||
|
||||
$stringValue = 'hey' . ($reflectionFunction->getReturnType() ? $reflectionFunction->getReturnType()->getName() : null);
|
||||
|
||||
// keep
|
||||
return $reflectionFunction->getReturnType();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [MethodCall::class, String_::class];
|
||||
}
|
||||
/**
|
||||
* @param MethodCall|String_ $node
|
||||
*/
|
||||
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
||||
{
|
||||
if ($node instanceof MethodCall) {
|
||||
return $this->refactorMethodCall($node);
|
||||
}
|
||||
if ($node->expr instanceof MethodCall) {
|
||||
return $this->refactorIfHasReturnTypeWasCalled($node->expr);
|
||||
}
|
||||
if (!$node->expr instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isObjectType($node->expr, new ObjectType('ReflectionType'))) {
|
||||
return null;
|
||||
}
|
||||
$type = $this->nodeTypeResolver->getType($node->expr);
|
||||
if (!$type instanceof UnionType) {
|
||||
return $this->nodeFactory->createMethodCall($node->expr, self::GET_NAME);
|
||||
}
|
||||
if (!$this->isWithReflectionType($type)) {
|
||||
return $this->nodeFactory->createMethodCall($node->expr, self::GET_NAME);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private function isWithReflectionType(UnionType $unionType) : bool
|
||||
{
|
||||
foreach ($unionType->getTypes() as $type) {
|
||||
if (!$type instanceof ObjectType) {
|
||||
continue;
|
||||
}
|
||||
if ($type->getClassName() !== 'ReflectionType') {
|
||||
continue;
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function refactorMethodCall(MethodCall $methodCall) : ?Node
|
||||
{
|
||||
$this->collectCallByVariable($methodCall);
|
||||
if ($this->shouldSkipMethodCall($methodCall)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->isReflectionParameterGetTypeMethodCall($methodCall)) {
|
||||
return $this->refactorReflectionParameterGetName($methodCall);
|
||||
}
|
||||
if ($this->isReflectionFunctionAbstractGetReturnTypeMethodCall($methodCall)) {
|
||||
return $this->refactorReflectionFunctionGetReturnType($methodCall);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private function refactorIfHasReturnTypeWasCalled(MethodCall $methodCall) : ?Node
|
||||
{
|
||||
if (!$methodCall->var instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
$variableName = $this->getName($methodCall->var);
|
||||
$callsByVariable = $this->callsByVariable[$variableName] ?? [];
|
||||
// we already know it has return type
|
||||
if (\in_array('hasReturnType', $callsByVariable, \true)) {
|
||||
return $this->nodeFactory->createMethodCall($methodCall, self::GET_NAME);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private function collectCallByVariable(MethodCall $methodCall) : void
|
||||
{
|
||||
// bit workaround for now
|
||||
if ($methodCall->var instanceof Variable) {
|
||||
$variableName = $this->getName($methodCall->var);
|
||||
$methodName = $this->getName($methodCall->name);
|
||||
if (!\is_string($variableName)) {
|
||||
return;
|
||||
}
|
||||
if (!\is_string($methodName)) {
|
||||
return;
|
||||
}
|
||||
$this->callsByVariable[$variableName][] = $methodName;
|
||||
}
|
||||
}
|
||||
private function shouldSkipMethodCall(MethodCall $methodCall) : bool
|
||||
{
|
||||
// is to string retype?
|
||||
$parentNode = $methodCall->getAttribute(AttributeKey::PARENT_NODE);
|
||||
if ($parentNode instanceof String_) {
|
||||
return \false;
|
||||
}
|
||||
// probably already converted
|
||||
return !$parentNode instanceof Concat;
|
||||
}
|
||||
private function isReflectionParameterGetTypeMethodCall(MethodCall $methodCall) : bool
|
||||
{
|
||||
if (!$this->isName($methodCall->name, 'getType')) {
|
||||
return \false;
|
||||
}
|
||||
return $this->isObjectType($methodCall->var, new ObjectType('ReflectionParameter'));
|
||||
}
|
||||
private function refactorReflectionParameterGetName(MethodCall $methodCall) : Ternary
|
||||
{
|
||||
$getNameMethodCall = $this->nodeFactory->createMethodCall($methodCall, self::GET_NAME);
|
||||
return new Ternary($methodCall, $getNameMethodCall, $this->nodeFactory->createNull());
|
||||
}
|
||||
private function isReflectionFunctionAbstractGetReturnTypeMethodCall(MethodCall $methodCall) : bool
|
||||
{
|
||||
if (!$this->isName($methodCall->name, 'getReturnType')) {
|
||||
return \false;
|
||||
}
|
||||
return $this->isObjectType($methodCall->var, new ObjectType('ReflectionFunctionAbstract'));
|
||||
}
|
||||
/**
|
||||
* @return \PhpParser\Node|\PhpParser\Node\Expr\Ternary
|
||||
*/
|
||||
private function refactorReflectionFunctionGetReturnType(MethodCall $methodCall)
|
||||
{
|
||||
$refactoredMethodCall = $this->refactorIfHasReturnTypeWasCalled($methodCall);
|
||||
if ($refactoredMethodCall instanceof Node) {
|
||||
return $refactoredMethodCall;
|
||||
}
|
||||
$getNameMethodCall = $this->nodeFactory->createMethodCall($methodCall, self::GET_NAME);
|
||||
return new Ternary($methodCall, $getNameMethodCall, $this->nodeFactory->createNull());
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'c1cb710c872d25f5daa3fd085fc24640716a80f4';
|
||||
public const PACKAGE_VERSION = 'bb3d37ab214177a4e2877a5e0b383d055c62b6ad';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-05-26 13:10:17';
|
||||
public const RELEASE_DATE = '2023-05-26 14:19:59';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitfddc3b52116436b11ff580f5f26c1af4::getLoader();
|
||||
return ComposerAutoloaderInit7eb270c59c5094c5b30576afe1545268::getLoader();
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -2233,7 +2233,6 @@ return array(
|
||||
'Rector\\Php74\\Rector\\FuncCall\\MbStrrposEncodingArgumentPositionRector' => $baseDir . '/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php',
|
||||
'Rector\\Php74\\Rector\\FuncCall\\MoneyFormatToNumberFormatRector' => $baseDir . '/rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php',
|
||||
'Rector\\Php74\\Rector\\LNumber\\AddLiteralSeparatorToNumberRector' => $baseDir . '/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php',
|
||||
'Rector\\Php74\\Rector\\MethodCall\\ChangeReflectionTypeToStringToGetNameRector' => $baseDir . '/rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php',
|
||||
'Rector\\Php74\\Rector\\Property\\RestoreDefaultNullToNullableTypePropertyRector' => $baseDir . '/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php',
|
||||
'Rector\\Php74\\Rector\\StaticCall\\ExportToReflectionFunctionRector' => $baseDir . '/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php',
|
||||
'Rector\\Php74\\Rector\\Ternary\\ParenthesizeNestedTernaryRector' => $baseDir . '/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php',
|
||||
|
10
vendor/composer/autoload_real.php
vendored
10
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitfddc3b52116436b11ff580f5f26c1af4
|
||||
class ComposerAutoloaderInit7eb270c59c5094c5b30576afe1545268
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -22,17 +22,17 @@ class ComposerAutoloaderInitfddc3b52116436b11ff580f5f26c1af4
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitfddc3b52116436b11ff580f5f26c1af4', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit7eb270c59c5094c5b30576afe1545268', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitfddc3b52116436b11ff580f5f26c1af4', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit7eb270c59c5094c5b30576afe1545268', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitfddc3b52116436b11ff580f5f26c1af4::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit7eb270c59c5094c5b30576afe1545268::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitfddc3b52116436b11ff580f5f26c1af4::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit7eb270c59c5094c5b30576afe1545268::$files;
|
||||
$requireFile = \Closure::bind(static function ($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 ComposerStaticInitfddc3b52116436b11ff580f5f26c1af4
|
||||
class ComposerStaticInit7eb270c59c5094c5b30576afe1545268
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
@ -2475,7 +2475,6 @@ class ComposerStaticInitfddc3b52116436b11ff580f5f26c1af4
|
||||
'Rector\\Php74\\Rector\\FuncCall\\MbStrrposEncodingArgumentPositionRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/MbStrrposEncodingArgumentPositionRector.php',
|
||||
'Rector\\Php74\\Rector\\FuncCall\\MoneyFormatToNumberFormatRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/FuncCall/MoneyFormatToNumberFormatRector.php',
|
||||
'Rector\\Php74\\Rector\\LNumber\\AddLiteralSeparatorToNumberRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/LNumber/AddLiteralSeparatorToNumberRector.php',
|
||||
'Rector\\Php74\\Rector\\MethodCall\\ChangeReflectionTypeToStringToGetNameRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php',
|
||||
'Rector\\Php74\\Rector\\Property\\RestoreDefaultNullToNullableTypePropertyRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/Property/RestoreDefaultNullToNullableTypePropertyRector.php',
|
||||
'Rector\\Php74\\Rector\\StaticCall\\ExportToReflectionFunctionRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/StaticCall/ExportToReflectionFunctionRector.php',
|
||||
'Rector\\Php74\\Rector\\Ternary\\ParenthesizeNestedTernaryRector' => __DIR__ . '/../..' . '/rules/Php74/Rector/Ternary/ParenthesizeNestedTernaryRector.php',
|
||||
@ -3094,9 +3093,9 @@ class ComposerStaticInitfddc3b52116436b11ff580f5f26c1af4
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitfddc3b52116436b11ff580f5f26c1af4::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitfddc3b52116436b11ff580f5f26c1af4::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitfddc3b52116436b11ff580f5f26c1af4::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit7eb270c59c5094c5b30576afe1545268::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit7eb270c59c5094c5b30576afe1545268::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit7eb270c59c5094c5b30576afe1545268::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user