Updated Rector to commit d9e17ca2c6bfc750eade85131368c8a2c35a0ca1

d9e17ca2c6 [Transform] Remove FileGetContentsAndJsonDecodeToStaticCallRector as only for demo purposes (#3776)
This commit is contained in:
Tomas Votruba 2023-05-08 21:11:06 +00:00
parent dffd0b0f29
commit 4e94212010
7 changed files with 12 additions and 191 deletions

View File

@ -1,141 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Transform\Rector\FunctionLike;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\VariadicPlaceholder;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Transform\ValueObject\StaticCallRecipe;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202305\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\FunctionLike\FileGetContentsAndJsonDecodeToStaticCallRector\FileGetContentsAndJsonDecodeToStaticCallRectorTest
*/
final class FileGetContentsAndJsonDecodeToStaticCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var \Rector\Transform\ValueObject\StaticCallRecipe
*/
private $staticCallRecipe;
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Merge 2 function calls to static call', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
public function load($filePath)
{
$fileGetContents = file_get_contents($filePath);
return json_decode($fileGetContents, true);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
public function load($filePath)
{
return FileLoader::loadJson($filePath);
}
}
CODE_SAMPLE
, [new StaticCallRecipe('FileLoader', 'loadJson')])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [FunctionLike::class];
}
/**
* @param FunctionLike $node
*/
public function refactor(Node $node) : ?Node
{
$stmts = $node->getStmts();
if ($stmts === null) {
return null;
}
$hasChanged = \false;
$previousStmt = null;
foreach ($stmts as $stmt) {
if ($this->processStmt($previousStmt, $stmt)) {
$hasChanged = \true;
/** @var Stmt $previousStmt */
$this->removeNode($previousStmt);
}
$previousStmt = $stmt;
}
if ($hasChanged) {
return $node;
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
$staticCallRecipe = $configuration[0] ?? null;
Assert::isInstanceOf($staticCallRecipe, StaticCallRecipe::class);
$this->staticCallRecipe = $staticCallRecipe;
}
private function createStaticCall(FuncCall $fileGetContentsFuncCall) : StaticCall
{
$fullyQualified = new FullyQualified($this->staticCallRecipe->getClassName());
return new StaticCall($fullyQualified, $this->staticCallRecipe->getMethodName(), $fileGetContentsFuncCall->isFirstClassCallable() ? [new VariadicPlaceholder()] : $fileGetContentsFuncCall->getArgs());
}
private function processStmt(?Stmt $previousStmt, Stmt $currentStmt) : bool
{
if (!$previousStmt instanceof Expression) {
return \false;
}
$previousExpr = $previousStmt->expr;
if (!$previousExpr instanceof Assign) {
return \false;
}
$previousAssign = $previousExpr;
if (!$previousAssign->expr instanceof FuncCall) {
return \false;
}
if (!$this->isName($previousAssign->expr, 'file_get_contents')) {
return \false;
}
$fileGetContentsFuncCall = $previousAssign->expr;
if ($currentStmt instanceof Return_) {
return $this->refactorReturnAndAssign($currentStmt, $fileGetContentsFuncCall);
}
if (!$currentStmt instanceof Expression) {
return \false;
}
if (!$currentStmt->expr instanceof Assign) {
return \false;
}
return $this->refactorReturnAndAssign($currentStmt->expr, $fileGetContentsFuncCall);
}
/**
* @param \PhpParser\Node\Stmt\Return_|\PhpParser\Node\Expr\Assign $currentStmt
*/
private function refactorReturnAndAssign($currentStmt, FuncCall $fileGetContentsFuncCall) : bool
{
if (!$currentStmt->expr instanceof FuncCall) {
return \false;
}
if (!$this->isName($currentStmt->expr, 'json_decode')) {
return \false;
}
$currentStmt->expr = $this->createStaticCall($fileGetContentsFuncCall);
return \true;
}
}

View File

@ -1,34 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use Rector\Core\Validation\RectorAssert;
final class StaticCallRecipe
{
/**
* @readonly
* @var string
*/
private $className;
/**
* @readonly
* @var string
*/
private $methodName;
public function __construct(string $className, string $methodName)
{
$this->className = $className;
$this->methodName = $methodName;
RectorAssert::className($className);
RectorAssert::methodName($methodName);
}
public function getClassName() : string
{
return $this->className;
}
public function getMethodName() : string
{
return $this->methodName;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '26550ca939279bf296f72b2457545c1a0ade3f8e';
public const PACKAGE_VERSION = 'd9e17ca2c6bfc750eade85131368c8a2c35a0ca1';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-08 21:48:22';
public const RELEASE_DATE = '2023-05-08 21:06:06';
/**
* @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 ComposerAutoloaderInitd445e688caf886e87b208e42156171fb::getLoader();
return ComposerAutoloaderInitd5b423b8c5068bd673f374fa308743a1::getLoader();

View File

@ -2709,7 +2709,6 @@ return array(
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToNewRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToNewRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToStaticCallRector' => $baseDir . '/rules/Transform/Rector/FuncCall/FuncCallToStaticCallRector.php',
'Rector\\Transform\\Rector\\FunctionLike\\FileGetContentsAndJsonDecodeToStaticCallRector' => $baseDir . '/rules/Transform/Rector/FunctionLike/FileGetContentsAndJsonDecodeToStaticCallRector.php',
'Rector\\Transform\\Rector\\Isset_\\UnsetAndIssetToMethodCallRector' => $baseDir . '/rules/Transform/Rector/Isset_/UnsetAndIssetToMethodCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToAnotherMethodCallWithArgumentsRector' => $baseDir . '/rules/Transform/Rector/MethodCall/MethodCallToAnotherMethodCallWithArgumentsRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToFuncCallRector' => $baseDir . '/rules/Transform/Rector/MethodCall/MethodCallToFuncCallRector.php',
@ -2743,7 +2742,6 @@ return array(
'Rector\\Transform\\ValueObject\\PropertyAssignToMethodCall' => $baseDir . '/rules/Transform/ValueObject/PropertyAssignToMethodCall.php',
'Rector\\Transform\\ValueObject\\PropertyFetchToMethodCall' => $baseDir . '/rules/Transform/ValueObject/PropertyFetchToMethodCall.php',
'Rector\\Transform\\ValueObject\\ReplaceParentCallByPropertyCall' => $baseDir . '/rules/Transform/ValueObject/ReplaceParentCallByPropertyCall.php',
'Rector\\Transform\\ValueObject\\StaticCallRecipe' => $baseDir . '/rules/Transform/ValueObject/StaticCallRecipe.php',
'Rector\\Transform\\ValueObject\\StaticCallToFuncCall' => $baseDir . '/rules/Transform/ValueObject/StaticCallToFuncCall.php',
'Rector\\Transform\\ValueObject\\StaticCallToMethodCall' => $baseDir . '/rules/Transform/ValueObject/StaticCallToMethodCall.php',
'Rector\\Transform\\ValueObject\\StaticCallToNew' => $baseDir . '/rules/Transform/ValueObject/StaticCallToNew.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitd445e688caf886e87b208e42156171fb
class ComposerAutoloaderInitd5b423b8c5068bd673f374fa308743a1
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitd445e688caf886e87b208e42156171fb
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitd445e688caf886e87b208e42156171fb', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitd5b423b8c5068bd673f374fa308743a1', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitd445e688caf886e87b208e42156171fb', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitd5b423b8c5068bd673f374fa308743a1', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitd445e688caf886e87b208e42156171fb::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitd5b423b8c5068bd673f374fa308743a1::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitd445e688caf886e87b208e42156171fb::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitd5b423b8c5068bd673f374fa308743a1::$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 ComposerStaticInitd445e688caf886e87b208e42156171fb
class ComposerStaticInitd5b423b8c5068bd673f374fa308743a1
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2951,7 +2951,6 @@ class ComposerStaticInitd445e688caf886e87b208e42156171fb
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToNewRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToNewRector.php',
'Rector\\Transform\\Rector\\FuncCall\\FuncCallToStaticCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FuncCall/FuncCallToStaticCallRector.php',
'Rector\\Transform\\Rector\\FunctionLike\\FileGetContentsAndJsonDecodeToStaticCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/FunctionLike/FileGetContentsAndJsonDecodeToStaticCallRector.php',
'Rector\\Transform\\Rector\\Isset_\\UnsetAndIssetToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/Isset_/UnsetAndIssetToMethodCallRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToAnotherMethodCallWithArgumentsRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/MethodCallToAnotherMethodCallWithArgumentsRector.php',
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToFuncCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/MethodCallToFuncCallRector.php',
@ -2985,7 +2984,6 @@ class ComposerStaticInitd445e688caf886e87b208e42156171fb
'Rector\\Transform\\ValueObject\\PropertyAssignToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/PropertyAssignToMethodCall.php',
'Rector\\Transform\\ValueObject\\PropertyFetchToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/PropertyFetchToMethodCall.php',
'Rector\\Transform\\ValueObject\\ReplaceParentCallByPropertyCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/ReplaceParentCallByPropertyCall.php',
'Rector\\Transform\\ValueObject\\StaticCallRecipe' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/StaticCallRecipe.php',
'Rector\\Transform\\ValueObject\\StaticCallToFuncCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/StaticCallToFuncCall.php',
'Rector\\Transform\\ValueObject\\StaticCallToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/StaticCallToMethodCall.php',
'Rector\\Transform\\ValueObject\\StaticCallToNew' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/StaticCallToNew.php',
@ -3117,9 +3115,9 @@ class ComposerStaticInitd445e688caf886e87b208e42156171fb
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitd445e688caf886e87b208e42156171fb::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitd445e688caf886e87b208e42156171fb::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitd445e688caf886e87b208e42156171fb::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitd5b423b8c5068bd673f374fa308743a1::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitd5b423b8c5068bd673f374fa308743a1::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitd5b423b8c5068bd673f374fa308743a1::$classMap;
}, null, ClassLoader::class);
}