mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-25 04:12:36 +00:00
Updated Rector to commit 1fc2825e17
1fc2825e17
[Transform] Add FileGetContentsAndJsonDecodeToStaticCallRector (#2059)
This commit is contained in:
parent
27249773f0
commit
04f0c86bba
|
@ -0,0 +1,140 @@
|
|||
<?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 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 RectorPrefix20220411\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @see \Rector\Tests\Transform\Rector\FunctionLike\FileGetContentsAndJsonDecodeToStaticCallRector\FileGetContentsAndJsonDecodeToStaticCallRectorTest
|
||||
*/
|
||||
final class FileGetContentsAndJsonDecodeToStaticCallRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var \Rector\Transform\ValueObject\StaticCallRecipe
|
||||
*/
|
||||
private $staticCallRecipe;
|
||||
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
||||
{
|
||||
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Merge 2 function calls to static call', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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 \Rector\Transform\ValueObject\StaticCallRecipe('FileLoader', 'loadJson')])]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [\PhpParser\Node\FunctionLike::class];
|
||||
}
|
||||
/**
|
||||
* @param FunctionLike $node
|
||||
*/
|
||||
public function refactor(\PhpParser\Node $node) : ?\PhpParser\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;
|
||||
\RectorPrefix20220411\Webmozart\Assert\Assert::isInstanceOf($staticCallRecipe, \Rector\Transform\ValueObject\StaticCallRecipe::class);
|
||||
$this->staticCallRecipe = $staticCallRecipe;
|
||||
}
|
||||
private function createStaticCall(\PhpParser\Node\Expr\FuncCall $fileGetContentsFuncCall) : \PhpParser\Node\Expr\StaticCall
|
||||
{
|
||||
$fullyQualified = new \PhpParser\Node\Name\FullyQualified($this->staticCallRecipe->getClassName());
|
||||
return new \PhpParser\Node\Expr\StaticCall($fullyQualified, $this->staticCallRecipe->getMethodName(), $fileGetContentsFuncCall->getArgs());
|
||||
}
|
||||
private function processStmt(?\PhpParser\Node\Stmt $previousStmt, \PhpParser\Node\Stmt $currentStmt) : bool
|
||||
{
|
||||
if (!$previousStmt instanceof \PhpParser\Node\Stmt\Expression) {
|
||||
return \false;
|
||||
}
|
||||
$previousExpr = $previousStmt->expr;
|
||||
if (!$previousExpr instanceof \PhpParser\Node\Expr\Assign) {
|
||||
return \false;
|
||||
}
|
||||
$previousAssign = $previousExpr;
|
||||
if (!$previousAssign->expr instanceof \PhpParser\Node\Expr\FuncCall) {
|
||||
return \false;
|
||||
}
|
||||
if (!$this->isName($previousAssign->expr, 'file_get_contents')) {
|
||||
return \false;
|
||||
}
|
||||
$fileGetContentsFuncCall = $previousAssign->expr;
|
||||
if ($currentStmt instanceof \PhpParser\Node\Stmt\Return_) {
|
||||
return $this->refactorReturnAndAssign($currentStmt, $fileGetContentsFuncCall);
|
||||
}
|
||||
if (!$currentStmt instanceof \PhpParser\Node\Stmt\Expression) {
|
||||
return \false;
|
||||
}
|
||||
if (!$currentStmt->expr instanceof \PhpParser\Node\Expr\Assign) {
|
||||
return \false;
|
||||
}
|
||||
return $this->refactorReturnAndAssign($currentStmt->expr, $fileGetContentsFuncCall);
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\Assign|\PhpParser\Node\Stmt\Return_ $currentStmt
|
||||
*/
|
||||
private function refactorReturnAndAssign($currentStmt, \PhpParser\Node\Expr\FuncCall $fileGetContentsFuncCall) : bool
|
||||
{
|
||||
if (!$currentStmt->expr instanceof \PhpParser\Node\Expr\FuncCall) {
|
||||
return \false;
|
||||
}
|
||||
if (!$this->isName($currentStmt->expr, 'json_decode')) {
|
||||
return \false;
|
||||
}
|
||||
$currentStmt->expr = $this->createStaticCall($fileGetContentsFuncCall);
|
||||
return \true;
|
||||
}
|
||||
}
|
31
rules/Transform/ValueObject/StaticCallRecipe.php
Normal file
31
rules/Transform/ValueObject/StaticCallRecipe.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Transform\ValueObject;
|
||||
|
||||
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;
|
||||
}
|
||||
public function getClassName() : string
|
||||
{
|
||||
return $this->className;
|
||||
}
|
||||
public function getMethodName() : string
|
||||
{
|
||||
return $this->methodName;
|
||||
}
|
||||
}
|
|
@ -16,11 +16,11 @@ final class VersionResolver
|
|||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '05f07aab1889d84e8c4d18dbcf0d07c6128bc185';
|
||||
public const PACKAGE_VERSION = '1fc2825e1781c63fa8647e3a52f7bd679fb45952';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-04-11 18:01:17';
|
||||
public const RELEASE_DATE = '2022-04-11 19:58:59';
|
||||
public static function resolvePackageVersion() : string
|
||||
{
|
||||
$process = new \RectorPrefix20220411\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);
|
||||
|
|
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 ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf::getLoader();
|
||||
return ComposerAutoloaderInit064d84f5cbfe5c1a80ab647854581c5b::getLoader();
|
||||
|
|
2
vendor/composer/autoload_classmap.php
vendored
2
vendor/composer/autoload_classmap.php
vendored
|
@ -3029,6 +3029,7 @@ 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\\CallableInMethodCallToVariableRector' => $baseDir . '/rules/Transform/Rector/MethodCall/CallableInMethodCallToVariableRector.php',
|
||||
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToAnotherMethodCallWithArgumentsRector' => $baseDir . '/rules/Transform/Rector/MethodCall/MethodCallToAnotherMethodCallWithArgumentsRector.php',
|
||||
|
@ -3067,6 +3068,7 @@ return array(
|
|||
'Rector\\Transform\\ValueObject\\PropertyFetchToMethodCall' => $baseDir . '/rules/Transform/ValueObject/PropertyFetchToMethodCall.php',
|
||||
'Rector\\Transform\\ValueObject\\ReplaceParentCallByPropertyCall' => $baseDir . '/rules/Transform/ValueObject/ReplaceParentCallByPropertyCall.php',
|
||||
'Rector\\Transform\\ValueObject\\ServiceGetterToConstructorInjection' => $baseDir . '/rules/Transform/ValueObject/ServiceGetterToConstructorInjection.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',
|
||||
|
|
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 ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf
|
||||
class ComposerAutoloaderInit064d84f5cbfe5c1a80ab647854581c5b
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,19 +22,19 @@ class ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit064d84f5cbfe5c1a80ab647854581c5b', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit064d84f5cbfe5c1a80ab647854581c5b', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitc89d1340da3f491de02a64145948ccaf::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit064d84f5cbfe5c1a80ab647854581c5b::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInitc89d1340da3f491de02a64145948ccaf::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit064d84f5cbfe5c1a80ab647854581c5b::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequirec89d1340da3f491de02a64145948ccaf($fileIdentifier, $file);
|
||||
composerRequire064d84f5cbfe5c1a80ab647854581c5b($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
|
@ -46,7 +46,7 @@ class ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf
|
|||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequirec89d1340da3f491de02a64145948ccaf($fileIdentifier, $file)
|
||||
function composerRequire064d84f5cbfe5c1a80ab647854581c5b($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
10
vendor/composer/autoload_static.php
vendored
10
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitc89d1340da3f491de02a64145948ccaf
|
||||
class ComposerStaticInit064d84f5cbfe5c1a80ab647854581c5b
|
||||
{
|
||||
public static $files = array (
|
||||
'320cde22f66dd4f5d3fd621d3e88b98f' => __DIR__ . '/..' . '/symfony/polyfill-ctype/bootstrap.php',
|
||||
|
@ -3398,6 +3398,7 @@ class ComposerStaticInitc89d1340da3f491de02a64145948ccaf
|
|||
'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\\CallableInMethodCallToVariableRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/CallableInMethodCallToVariableRector.php',
|
||||
'Rector\\Transform\\Rector\\MethodCall\\MethodCallToAnotherMethodCallWithArgumentsRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/MethodCallToAnotherMethodCallWithArgumentsRector.php',
|
||||
|
@ -3436,6 +3437,7 @@ class ComposerStaticInitc89d1340da3f491de02a64145948ccaf
|
|||
'Rector\\Transform\\ValueObject\\PropertyFetchToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/PropertyFetchToMethodCall.php',
|
||||
'Rector\\Transform\\ValueObject\\ReplaceParentCallByPropertyCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/ReplaceParentCallByPropertyCall.php',
|
||||
'Rector\\Transform\\ValueObject\\ServiceGetterToConstructorInjection' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/ServiceGetterToConstructorInjection.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',
|
||||
|
@ -3859,9 +3861,9 @@ class ComposerStaticInitc89d1340da3f491de02a64145948ccaf
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitc89d1340da3f491de02a64145948ccaf::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitc89d1340da3f491de02a64145948ccaf::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitc89d1340da3f491de02a64145948ccaf::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit064d84f5cbfe5c1a80ab647854581c5b::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit064d84f5cbfe5c1a80ab647854581c5b::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit064d84f5cbfe5c1a80ab647854581c5b::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
10
vendor/scoper-autoload.php
vendored
10
vendor/scoper-autoload.php
vendored
|
@ -9,8 +9,8 @@ $loader = require_once __DIR__.'/autoload.php';
|
|||
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
|
||||
spl_autoload_call('RectorPrefix20220411\AutoloadIncluder');
|
||||
}
|
||||
if (!class_exists('ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf', false) && !interface_exists('ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf', false) && !trait_exists('ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf', false)) {
|
||||
spl_autoload_call('RectorPrefix20220411\ComposerAutoloaderInitc89d1340da3f491de02a64145948ccaf');
|
||||
if (!class_exists('ComposerAutoloaderInit064d84f5cbfe5c1a80ab647854581c5b', false) && !interface_exists('ComposerAutoloaderInit064d84f5cbfe5c1a80ab647854581c5b', false) && !trait_exists('ComposerAutoloaderInit064d84f5cbfe5c1a80ab647854581c5b', false)) {
|
||||
spl_autoload_call('RectorPrefix20220411\ComposerAutoloaderInit064d84f5cbfe5c1a80ab647854581c5b');
|
||||
}
|
||||
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
|
||||
spl_autoload_call('RectorPrefix20220411\Helmich\TypoScriptParser\Parser\AST\Statement');
|
||||
|
@ -59,9 +59,9 @@ if (!function_exists('print_node')) {
|
|||
return \RectorPrefix20220411\print_node(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('composerRequirec89d1340da3f491de02a64145948ccaf')) {
|
||||
function composerRequirec89d1340da3f491de02a64145948ccaf() {
|
||||
return \RectorPrefix20220411\composerRequirec89d1340da3f491de02a64145948ccaf(...func_get_args());
|
||||
if (!function_exists('composerRequire064d84f5cbfe5c1a80ab647854581c5b')) {
|
||||
function composerRequire064d84f5cbfe5c1a80ab647854581c5b() {
|
||||
return \RectorPrefix20220411\composerRequire064d84f5cbfe5c1a80ab647854581c5b(...func_get_args());
|
||||
}
|
||||
}
|
||||
if (!function_exists('scanPath')) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user