Updated Rector to commit c4b524e77a5eccd387d1ebe5b01ab3ae680049a3

c4b524e77a Remove GetAndSetToMethodCallRector, as not practical and used for single legacy job (#3953)
This commit is contained in:
Tomas Votruba 2023-05-24 13:47:55 +00:00
parent 7dcc1f5c95
commit 7117aa039a
10 changed files with 13 additions and 303 deletions

View File

@ -1,136 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Transform\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\NodeManipulator\MagicPropertyFetchAnalyzer;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Transform\ValueObject\GetAndSetToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202305\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\Assign\GetAndSetToMethodCallRector\GetAndSetToMethodCallRectorTest
*/
final class GetAndSetToMethodCallRector extends AbstractScopeAwareRector implements ConfigurableRectorInterface
{
/**
* @var GetAndSetToMethodCall[]
*/
private $getAndSetToMethodCalls = [];
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer
*/
private $propertyFetchAnalyzer;
/**
* @readonly
* @var \Rector\Core\NodeManipulator\MagicPropertyFetchAnalyzer
*/
private $magicPropertyFetchAnalyzer;
public function __construct(PropertyFetchAnalyzer $propertyFetchAnalyzer, MagicPropertyFetchAnalyzer $magicPropertyFetchAnalyzer)
{
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
$this->magicPropertyFetchAnalyzer = $magicPropertyFetchAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Turns defined `__get`/`__set` to specific method calls.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
$container = new SomeContainer;
$container->someService = $someService;
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$container = new SomeContainer;
$container->setService("someService", $someService);
CODE_SAMPLE
, [new GetAndSetToMethodCall('SomeContainer', 'addService', 'getService')])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Assign::class, PropertyFetch::class];
}
/**
* @param Assign|PropertyFetch $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
if ($node instanceof Assign) {
if ($node->var instanceof PropertyFetch) {
return $this->processMagicSet($node->expr, $node->var, $scope);
}
return null;
}
return $this->processPropertyFetch($node, $scope);
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
Assert::allIsAOf($configuration, GetAndSetToMethodCall::class);
$this->getAndSetToMethodCalls = $configuration;
}
private function processMagicSet(Expr $expr, PropertyFetch $propertyFetch, Scope $scope) : ?Node
{
foreach ($this->getAndSetToMethodCalls as $getAndSetToMethodCall) {
$objectType = $getAndSetToMethodCall->getObjectType();
if ($this->shouldSkipPropertyFetch($propertyFetch, $objectType, $scope)) {
continue;
}
return $this->createMethodCallNodeFromAssignNode($propertyFetch, $expr, $getAndSetToMethodCall->getSetMethod());
}
return null;
}
private function processPropertyFetch(PropertyFetch $propertyFetch, Scope $scope) : ?MethodCall
{
$parentNode = $propertyFetch->getAttribute(AttributeKey::PARENT_NODE);
foreach ($this->getAndSetToMethodCalls as $getAndSetToMethodCall) {
if ($this->shouldSkipPropertyFetch($propertyFetch, $getAndSetToMethodCall->getObjectType(), $scope)) {
continue;
}
// setter, skip
if (!$parentNode instanceof Assign) {
return $this->createMethodCallNodeFromPropertyFetchNode($propertyFetch, $getAndSetToMethodCall->getGetMethod());
}
if ($parentNode->var !== $propertyFetch) {
return $this->createMethodCallNodeFromPropertyFetchNode($propertyFetch, $getAndSetToMethodCall->getGetMethod());
}
}
return null;
}
private function shouldSkipPropertyFetch(PropertyFetch $propertyFetch, ObjectType $objectType, Scope $scope) : bool
{
if (!$this->isObjectType($propertyFetch->var, $objectType)) {
return \true;
}
if (!$this->magicPropertyFetchAnalyzer->isMagicOnType($propertyFetch, $objectType, $scope)) {
return \true;
}
return $this->propertyFetchAnalyzer->isPropertyToSelf($propertyFetch);
}
private function createMethodCallNodeFromAssignNode(PropertyFetch $propertyFetch, Expr $expr, string $method) : MethodCall
{
$propertyName = $this->getName($propertyFetch->name);
return $this->nodeFactory->createMethodCall($propertyFetch->var, $method, [$propertyName, $expr]);
}
private function createMethodCallNodeFromPropertyFetchNode(PropertyFetch $propertyFetch, string $method) : MethodCall
{
/** @var Variable $variableNode */
$variableNode = $propertyFetch->var;
return $this->nodeFactory->createMethodCall($variableNode, $method, [$this->getName($propertyFetch)]);
}
}

View File

@ -1,46 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class GetAndSetToMethodCall
{
/**
* @readonly
* @var string
*/
private $classType;
/**
* @readonly
* @var string
*/
private $getMethod;
/**
* @readonly
* @var string
*/
private $setMethod;
public function __construct(string $classType, string $getMethod, string $setMethod)
{
$this->classType = $classType;
$this->getMethod = $getMethod;
$this->setMethod = $setMethod;
RectorAssert::className($classType);
RectorAssert::methodName($getMethod);
RectorAssert::methodName($setMethod);
}
public function getGetMethod() : string
{
return $this->getMethod;
}
public function getSetMethod() : string
{
return $this->setMethod;
}
public function getObjectType() : ObjectType
{
return new ObjectType($this->classType);
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '7e9ee7afbf04ede850da1639b1b234e4e86730fb';
public const PACKAGE_VERSION = 'c4b524e77a5eccd387d1ebe5b01ab3ae680049a3';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-24 14:37:47';
public const RELEASE_DATE = '2023-05-24 14:43:55';
/**
* @var int
*/

View File

@ -15,7 +15,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v25';
private const CACHE_KEY = 'v26';
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface|null
*/

View File

@ -123,23 +123,6 @@ final class PropertyFetchAnalyzer
return $this->isLocalPropertyFetchName($node, $propertyName);
});
}
public function isPropertyToSelf(PropertyFetch $propertyFetch) : bool
{
if (!$this->nodeNameResolver->isName($propertyFetch->var, self::THIS)) {
return \false;
}
$class = $this->betterNodeFinder->findParentType($propertyFetch, Class_::class);
if (!$class instanceof Class_) {
return \false;
}
foreach ($class->getProperties() as $property) {
if (!$this->nodeNameResolver->areNamesEqual($property->props[0], $propertyFetch)) {
continue;
}
return \true;
}
return \false;
}
public function isPropertyFetch(Node $node) : bool
{
if ($node instanceof PropertyFetch) {

View File

@ -1,85 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Core\NodeManipulator;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
/**
* Utils for PropertyFetch Node:
* "$this->property"
*/
final class MagicPropertyFetchAnalyzer
{
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @readonly
* @var \Rector\NodeTypeResolver\NodeTypeResolver
*/
private $nodeTypeResolver;
/**
* @readonly
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
public function __construct(NodeNameResolver $nodeNameResolver, NodeTypeResolver $nodeTypeResolver, ReflectionProvider $reflectionProvider)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->reflectionProvider = $reflectionProvider;
}
public function isMagicOnType(PropertyFetch $propertyFetch, ObjectType $objectType, Scope $scope) : bool
{
$varNodeType = $this->nodeTypeResolver->getType($propertyFetch);
if ($varNodeType instanceof ErrorType) {
return \true;
}
if ($varNodeType instanceof MixedType) {
return \false;
}
if ($varNodeType->isSuperTypeOf($objectType)->yes()) {
return \false;
}
$nodeName = $this->nodeNameResolver->getName($propertyFetch->name);
if ($nodeName === null) {
return \false;
}
return !$this->hasPublicProperty($propertyFetch, $nodeName, $scope);
}
/**
* @param \PhpParser\Node\Expr\PropertyFetch|\PhpParser\Node\Expr\StaticPropertyFetch $expr
*/
private function hasPublicProperty($expr, string $propertyName, Scope $scope) : bool
{
if ($expr instanceof PropertyFetch) {
$propertyFetchType = $scope->getType($expr->var);
} else {
$propertyFetchType = $this->nodeTypeResolver->getType($expr->class);
}
if (!$propertyFetchType instanceof TypeWithClassName) {
return \false;
}
$propertyFetchType = $propertyFetchType->getClassName();
if (!$this->reflectionProvider->hasClass($propertyFetchType)) {
return \false;
}
$classReflection = $this->reflectionProvider->getClass($propertyFetchType);
if (!$classReflection->hasProperty($propertyName)) {
return \false;
}
$propertyReflection = $classReflection->getProperty($propertyName, $scope);
return $propertyReflection->isPublic();
}
}

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit03705db7587c5d5e30870552140b01a2::getLoader();
return ComposerAutoloaderInit5d020b628e489ed7fa420d2cee09c72d::getLoader();

View File

@ -1486,7 +1486,6 @@ return array(
'Rector\\Core\\NodeManipulator\\FuncCallManipulator' => $baseDir . '/src/NodeManipulator/FuncCallManipulator.php',
'Rector\\Core\\NodeManipulator\\FunctionLikeManipulator' => $baseDir . '/src/NodeManipulator/FunctionLikeManipulator.php',
'Rector\\Core\\NodeManipulator\\IfManipulator' => $baseDir . '/src/NodeManipulator/IfManipulator.php',
'Rector\\Core\\NodeManipulator\\MagicPropertyFetchAnalyzer' => $baseDir . '/src/NodeManipulator/MagicPropertyFetchAnalyzer.php',
'Rector\\Core\\NodeManipulator\\MethodCallManipulator' => $baseDir . '/src/NodeManipulator/MethodCallManipulator.php',
'Rector\\Core\\NodeManipulator\\PropertyFetchAssignManipulator' => $baseDir . '/src/NodeManipulator/PropertyFetchAssignManipulator.php',
'Rector\\Core\\NodeManipulator\\PropertyManipulator' => $baseDir . '/src/NodeManipulator/PropertyManipulator.php',
@ -2689,7 +2688,6 @@ return array(
'Rector\\Transform\\NodeAnalyzer\\FuncCallStaticCallToMethodCallAnalyzer' => $baseDir . '/rules/Transform/NodeAnalyzer/FuncCallStaticCallToMethodCallAnalyzer.php',
'Rector\\Transform\\NodeFactory\\PropertyFetchFactory' => $baseDir . '/rules/Transform/NodeFactory/PropertyFetchFactory.php',
'Rector\\Transform\\NodeTypeAnalyzer\\TypeProvidingExprFromClassResolver' => $baseDir . '/rules/Transform/NodeTypeAnalyzer/TypeProvidingExprFromClassResolver.php',
'Rector\\Transform\\Rector\\Assign\\GetAndSetToMethodCallRector' => $baseDir . '/rules/Transform/Rector/Assign/GetAndSetToMethodCallRector.php',
'Rector\\Transform\\Rector\\Assign\\PropertyAssignToMethodCallRector' => $baseDir . '/rules/Transform/Rector/Assign/PropertyAssignToMethodCallRector.php',
'Rector\\Transform\\Rector\\Assign\\PropertyFetchToMethodCallRector' => $baseDir . '/rules/Transform/Rector/Assign/PropertyFetchToMethodCallRector.php',
'Rector\\Transform\\Rector\\Attribute\\AttributeKeyToClassConstFetchRector' => $baseDir . '/rules/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector.php',
@ -2722,7 +2720,6 @@ return array(
'Rector\\Transform\\ValueObject\\ClassMethodReference' => $baseDir . '/rules/Transform/ValueObject/ClassMethodReference.php',
'Rector\\Transform\\ValueObject\\FuncCallToMethodCall' => $baseDir . '/rules/Transform/ValueObject/FuncCallToMethodCall.php',
'Rector\\Transform\\ValueObject\\FuncCallToStaticCall' => $baseDir . '/rules/Transform/ValueObject/FuncCallToStaticCall.php',
'Rector\\Transform\\ValueObject\\GetAndSetToMethodCall' => $baseDir . '/rules/Transform/ValueObject/GetAndSetToMethodCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToFuncCall' => $baseDir . '/rules/Transform/ValueObject/MethodCallToFuncCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToMethodCall' => $baseDir . '/rules/Transform/ValueObject/MethodCallToMethodCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToPropertyFetch' => $baseDir . '/rules/Transform/ValueObject/MethodCallToPropertyFetch.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit03705db7587c5d5e30870552140b01a2
class ComposerAutoloaderInit5d020b628e489ed7fa420d2cee09c72d
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit03705db7587c5d5e30870552140b01a2
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit03705db7587c5d5e30870552140b01a2', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit5d020b628e489ed7fa420d2cee09c72d', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit03705db7587c5d5e30870552140b01a2', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit5d020b628e489ed7fa420d2cee09c72d', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit03705db7587c5d5e30870552140b01a2::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit5d020b628e489ed7fa420d2cee09c72d::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit03705db7587c5d5e30870552140b01a2::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit5d020b628e489ed7fa420d2cee09c72d::$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 ComposerStaticInit03705db7587c5d5e30870552140b01a2
class ComposerStaticInit5d020b628e489ed7fa420d2cee09c72d
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1728,7 +1728,6 @@ class ComposerStaticInit03705db7587c5d5e30870552140b01a2
'Rector\\Core\\NodeManipulator\\FuncCallManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/FuncCallManipulator.php',
'Rector\\Core\\NodeManipulator\\FunctionLikeManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/FunctionLikeManipulator.php',
'Rector\\Core\\NodeManipulator\\IfManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/IfManipulator.php',
'Rector\\Core\\NodeManipulator\\MagicPropertyFetchAnalyzer' => __DIR__ . '/../..' . '/src/NodeManipulator/MagicPropertyFetchAnalyzer.php',
'Rector\\Core\\NodeManipulator\\MethodCallManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/MethodCallManipulator.php',
'Rector\\Core\\NodeManipulator\\PropertyFetchAssignManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/PropertyFetchAssignManipulator.php',
'Rector\\Core\\NodeManipulator\\PropertyManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/PropertyManipulator.php',
@ -2931,7 +2930,6 @@ class ComposerStaticInit03705db7587c5d5e30870552140b01a2
'Rector\\Transform\\NodeAnalyzer\\FuncCallStaticCallToMethodCallAnalyzer' => __DIR__ . '/../..' . '/rules/Transform/NodeAnalyzer/FuncCallStaticCallToMethodCallAnalyzer.php',
'Rector\\Transform\\NodeFactory\\PropertyFetchFactory' => __DIR__ . '/../..' . '/rules/Transform/NodeFactory/PropertyFetchFactory.php',
'Rector\\Transform\\NodeTypeAnalyzer\\TypeProvidingExprFromClassResolver' => __DIR__ . '/../..' . '/rules/Transform/NodeTypeAnalyzer/TypeProvidingExprFromClassResolver.php',
'Rector\\Transform\\Rector\\Assign\\GetAndSetToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Assign/GetAndSetToMethodCallRector.php',
'Rector\\Transform\\Rector\\Assign\\PropertyAssignToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Assign/PropertyAssignToMethodCallRector.php',
'Rector\\Transform\\Rector\\Assign\\PropertyFetchToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Assign/PropertyFetchToMethodCallRector.php',
'Rector\\Transform\\Rector\\Attribute\\AttributeKeyToClassConstFetchRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Attribute/AttributeKeyToClassConstFetchRector.php',
@ -2964,7 +2962,6 @@ class ComposerStaticInit03705db7587c5d5e30870552140b01a2
'Rector\\Transform\\ValueObject\\ClassMethodReference' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/ClassMethodReference.php',
'Rector\\Transform\\ValueObject\\FuncCallToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/FuncCallToMethodCall.php',
'Rector\\Transform\\ValueObject\\FuncCallToStaticCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/FuncCallToStaticCall.php',
'Rector\\Transform\\ValueObject\\GetAndSetToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/GetAndSetToMethodCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToFuncCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/MethodCallToFuncCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/MethodCallToMethodCall.php',
'Rector\\Transform\\ValueObject\\MethodCallToPropertyFetch' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/MethodCallToPropertyFetch.php',
@ -3105,9 +3102,9 @@ class ComposerStaticInit03705db7587c5d5e30870552140b01a2
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit03705db7587c5d5e30870552140b01a2::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit03705db7587c5d5e30870552140b01a2::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit03705db7587c5d5e30870552140b01a2::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit5d020b628e489ed7fa420d2cee09c72d::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit5d020b628e489ed7fa420d2cee09c72d::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit5d020b628e489ed7fa420d2cee09c72d::$classMap;
}, null, ClassLoader::class);
}