Updated Rector to commit 408674025f7fc5295b5e67d7adaf11eca4dc1f24

408674025f Remove NewToConstructorInjectionRector as unused and only for demo purposes (#4087)
This commit is contained in:
Tomas Votruba 2023-06-05 14:41:35 +00:00
parent 368e973125
commit 7b96363ba3
6 changed files with 12 additions and 186 deletions

View File

@ -1,172 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Transform\Rector\New_;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\PropertyNaming;
use Rector\Naming\ValueObject\ExpectedName;
use Rector\PostRector\Collector\PropertyToAddCollector;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\Transform\NodeFactory\PropertyFetchFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202306\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\New_\NewToConstructorInjectionRector\NewToConstructorInjectionRectorTest
*/
final class NewToConstructorInjectionRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var ObjectType[]
*/
private $constructorInjectionObjectTypes = [];
/**
* @readonly
* @var \Rector\Transform\NodeFactory\PropertyFetchFactory
*/
private $propertyFetchFactory;
/**
* @readonly
* @var \Rector\Naming\Naming\PropertyNaming
*/
private $propertyNaming;
/**
* @readonly
* @var \Rector\PostRector\Collector\PropertyToAddCollector
*/
private $propertyToAddCollector;
public function __construct(PropertyFetchFactory $propertyFetchFactory, PropertyNaming $propertyNaming, PropertyToAddCollector $propertyToAddCollector)
{
$this->propertyFetchFactory = $propertyFetchFactory;
$this->propertyNaming = $propertyNaming;
$this->propertyToAddCollector = $propertyToAddCollector;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change defined new type to constructor injection', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$validator = new Validator();
$validator->validate(1000);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var Validator
*/
private $validator;
public function __construct(Validator $validator)
{
$this->validator = $validator;
}
public function run()
{
$this->validator->validate(1000);
}
}
CODE_SAMPLE
, ['Validator'])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [New_::class, Assign::class, MethodCall::class, Expression::class];
}
/**
* @param New_|Assign|MethodCall|Expression $node
*/
public function refactor(Node $node) : ?Node
{
if ($node instanceof MethodCall) {
return $this->refactorMethodCall($node);
}
if ($node instanceof Expression) {
$nodeExpr = $node->expr;
if ($nodeExpr instanceof Assign) {
$this->refactorAssignExpression($node, $nodeExpr);
}
}
if ($node instanceof New_) {
$this->refactorNew($node);
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
$typesToConstructorInjections = $configuration;
Assert::allString($typesToConstructorInjections);
foreach ($typesToConstructorInjections as $typeToConstructorInjection) {
$this->constructorInjectionObjectTypes[] = new ObjectType($typeToConstructorInjection);
}
}
private function refactorMethodCall(MethodCall $methodCall) : ?MethodCall
{
foreach ($this->constructorInjectionObjectTypes as $constructorInjectionObjectType) {
if (!$methodCall->var instanceof Variable) {
continue;
}
if (!$this->isObjectType($methodCall->var, $constructorInjectionObjectType)) {
continue;
}
if (!$this->nodeTypeResolver->isObjectType($methodCall->var, $constructorInjectionObjectType)) {
continue;
}
$methodCall->var = $this->propertyFetchFactory->createFromType($constructorInjectionObjectType);
return $methodCall;
}
return null;
}
private function refactorAssignExpression(Expression $expression, Assign $assign) : void
{
$currentAssign = $assign->expr instanceof Assign ? $assign->expr : $assign;
if (!$currentAssign->expr instanceof New_) {
return;
}
foreach ($this->constructorInjectionObjectTypes as $constructorInjectionObjectType) {
if (!$this->isObjectType($currentAssign->expr, $constructorInjectionObjectType)) {
continue;
}
$this->removeNode($expression);
}
}
private function refactorNew(New_ $new) : void
{
foreach ($this->constructorInjectionObjectTypes as $constructorInjectionObjectType) {
if (!$this->isObjectType($new->class, $constructorInjectionObjectType)) {
continue;
}
$classLike = $this->betterNodeFinder->findParentType($new, Class_::class);
if (!$classLike instanceof Class_) {
continue;
}
$expectedPropertyName = $this->propertyNaming->getExpectedNameFromType($constructorInjectionObjectType);
if (!$expectedPropertyName instanceof ExpectedName) {
continue;
}
$propertyMetadata = new PropertyMetadata($expectedPropertyName->getName(), $constructorInjectionObjectType, Class_::MODIFIER_PRIVATE);
$this->propertyToAddCollector->addPropertyToClass($classLike, $propertyMetadata);
}
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'be31c8faec553f9247f3c0d810648ec22b91b827';
public const PACKAGE_VERSION = '408674025f7fc5295b5e67d7adaf11eca4dc1f24';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-06-05 14:33:09';
public const RELEASE_DATE = '2023-06-05 14:37:14';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -2657,7 +2657,6 @@ return array(
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToStaticCallRector' => $baseDir . '/rules/Transform/Rector/MethodCall/MethodCallToStaticCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\ReplaceParentCallByPropertyCallRector' => $baseDir . '/rules/Transform/Rector/MethodCall/ReplaceParentCallByPropertyCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewArgToMethodCallRector' => $baseDir . '/rules/Transform/Rector/New_/NewArgToMethodCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewToConstructorInjectionRector' => $baseDir . '/rules/Transform/Rector/New_/NewToConstructorInjectionRector.php',
'Rector\\Transform\\Rector\\New_\\NewToStaticCallRector' => $baseDir . '/rules/Transform/Rector/New_/NewToStaticCallRector.php',
'Rector\\Transform\\Rector\\StaticCall\\StaticCallToFuncCallRector' => $baseDir . '/rules/Transform/Rector/StaticCall/StaticCallToFuncCallRector.php',
'Rector\\Transform\\Rector\\StaticCall\\StaticCallToMethodCallRector' => $baseDir . '/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitfb0b90262d71df9386a64cc41fa2a02c
class ComposerAutoloaderInit8b63bbf1bdfbd7bd5f442ea39ed30845
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitfb0b90262d71df9386a64cc41fa2a02c
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitfb0b90262d71df9386a64cc41fa2a02c', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit8b63bbf1bdfbd7bd5f442ea39ed30845', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitfb0b90262d71df9386a64cc41fa2a02c', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit8b63bbf1bdfbd7bd5f442ea39ed30845', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitfb0b90262d71df9386a64cc41fa2a02c::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit8b63bbf1bdfbd7bd5f442ea39ed30845::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitfb0b90262d71df9386a64cc41fa2a02c::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit8b63bbf1bdfbd7bd5f442ea39ed30845::$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 ComposerStaticInitfb0b90262d71df9386a64cc41fa2a02c
class ComposerStaticInit8b63bbf1bdfbd7bd5f442ea39ed30845
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2899,7 +2899,6 @@ class ComposerStaticInitfb0b90262d71df9386a64cc41fa2a02c
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToStaticCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/MethodCallToStaticCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\ReplaceParentCallByPropertyCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/ReplaceParentCallByPropertyCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewArgToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/New_/NewArgToMethodCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewToConstructorInjectionRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/New_/NewToConstructorInjectionRector.php',
'Rector\\Transform\\Rector\\New_\\NewToStaticCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/New_/NewToStaticCallRector.php',
'Rector\\Transform\\Rector\\StaticCall\\StaticCallToFuncCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/StaticCall/StaticCallToFuncCallRector.php',
'Rector\\Transform\\Rector\\StaticCall\\StaticCallToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php',
@ -3048,9 +3047,9 @@ class ComposerStaticInitfb0b90262d71df9386a64cc41fa2a02c
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitfb0b90262d71df9386a64cc41fa2a02c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitfb0b90262d71df9386a64cc41fa2a02c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitfb0b90262d71df9386a64cc41fa2a02c::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit8b63bbf1bdfbd7bd5f442ea39ed30845::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit8b63bbf1bdfbd7bd5f442ea39ed30845::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit8b63bbf1bdfbd7bd5f442ea39ed30845::$classMap;
}, null, ClassLoader::class);
}